吴恩达 coursera ML 第七课总结+作业答案
生活随笔
收集整理的這篇文章主要介紹了
吴恩达 coursera ML 第七课总结+作业答案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
學以致用,以學促用,通過筆記總結,鞏固學習成果,復習新學的概念。
目錄
文章目錄
- 前言
- 目錄
- 正文
- 模型引入
- 神經網絡
- 模型表示
- 模型表示2
- 例子和圖示
- 例子與圖示2
- 作業答案
正文
本節主要討論神經網絡及其強大的功能
模型引入
非線性分類問題會帶來超多的參數,也就是參數爆炸這一問題。
一個典型的例子,計算機視覺目標識別問題。
通過訓練分類器,我們可以很好的確定一個像素對應的是不是汽車。
神經網絡
神經網路的發展歷史。
神經網絡的誘因,大腦并沒有特定的神經元去處理特定的任務。
模型表示
神經元模型結構圖
神經元模型數學化,邏輯單元。
神經元模型,多層感知機。
多層神經網絡的數學表示和參數量。
模型表示2
神經網絡向量化實現方法,前向傳遞計算流程。
神經網絡學到了它的特征。
其他類似的神經網絡架構。
例子和圖示
非線性分類問題:異或。
簡化例子,邏輯與操作
簡化例子,或操作。
例子與圖示2
簡單例子,否操作的實現。
簡單例子:異或操作。
應用例子:手寫文本識別。
## 多分類問題
多目標識別問題,通過onehot編碼,輸出圖像里的各個類別。
輸出層也有多個神經元。
作業答案
多分類問題
ex3.m
lrCostFunction.m
function [J, grad] = lrCostFunction(theta, X, y, lambda) %LRCOSTFUNCTION Compute cost and gradient for logistic regression with %regularization % J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using % theta as the parameter for regularized logistic regression and the % gradient of the cost w.r.t. to the parameters. % Initialize some useful values m = length(y); % number of training examples% You need to return the following variables correctly J = 0; grad = zeros(size(theta));% ====================== YOUR CODE HERE ====================== % Instructions: Compute the cost of a particular choice of theta. % You should set J to the cost. % Compute the partial derivatives and set grad to the partial % derivatives of the cost w.r.t. each parameter in theta % % Hint: The computation of the cost function and gradients can be % efficiently vectorized. For example, consider the computation % % sigmoid(X * theta) % % Each row of the resulting matrix will contain the value of the % prediction for that example. You can make use of this to vectorize % the cost function and gradient computations. % % Hint: When computing the gradient of the regularized cost function, % there're many possible vectorized solutions, but one solution % looks like: % grad = (unregularized gradient for logistic regression) % temp = theta; % temp(1) = 0; % because we don't add anything for j = 0 % grad = grad + YOUR_CODE_HERE (using the temp variable) % J=1/m*(-y'*log(sigmoid(X*theta))-(1-y)'*log(1-sigmoid(X*theta)))+lambda*(theta'*theta-theta(1)^2)/2/m; grad=X'*(sigmoid(X*theta)-y)/m; temp=theta*lambda/m; temp(1)=0; grad=grad+temp;% =============================================================grad = grad(:);endonevsall.m
function [all_theta] = oneVsAll(X, y, num_labels, lambda) %ONEVSALL trains multiple logistic regression classifiers and returns all %the classifiers in a matrix all_theta, where the i-th row of all_theta %corresponds to the classifier for label i % [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels % logistic regression classifiers and returns each of these classifiers % in a matrix all_theta, where the i-th row of all_theta corresponds % to the classifier for label i% Some useful variables m = size(X, 1); n = size(X, 2);% You need to return the following variables correctly all_theta = zeros(num_labels, n + 1);% Add ones to the X data matrix X = [ones(m, 1) X];% ====================== YOUR CODE HERE ====================== % Instructions: You should complete the following code to train num_labels % logistic regression classifiers with regularization % parameter lambda. % % Hint: theta(:) will return a column vector. % % Hint: You can use y == c to obtain a vector of 1's and 0's that tell you % whether the ground truth is true/false for this class. % % Note: For this assignment, we recommend using fmincg to optimize the cost % function. It is okay to use a for-loop (for c = 1:num_labels) to % loop over the different classes. % % fmincg works similarly to fminunc, but is more efficient when we % are dealing with large number of parameters. % % Example Code for fmincg: %% Set Initial thetainitial_theta = zeros(n + 1, 1); % % % Set options for fminuncoptions = optimset('GradObj', 'on', 'MaxIter', 50); % % % Run fmincg to obtain the optimal theta % % This function will return theta and the cost for i=1:num_labels [all_theta(i,:)] = ...fmincg (@(t)(lrCostFunction(t, X, y==i, lambda)), ...initial_theta, options); end% =========================================================================endpredictonevsall.m
function p = predictOneVsAll(all_theta, X) %PREDICT Predict the label for a trained one-vs-all classifier. The labels %are in the range 1..K, where K = size(all_theta, 1). % p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions % for each example in the matrix X. Note that X contains the examples in % rows. all_theta is a matrix where the i-th row is a trained logistic % regression theta vector for the i-th class. You should set p to a vector % of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2 % for 4 examples) m = size(X, 1); num_labels = size(all_theta, 1);% You need to return the following variables correctly p = zeros(size(X, 1), 1);% Add ones to the X data matrix X = [ones(m, 1) X];% ====================== YOUR CODE HERE ====================== % Instructions: Complete the following code to make predictions using % your learned logistic regression parameters (one-vs-all). % You should set p to a vector of predictions (from 1 to % num_labels). % % Hint: This code can be done all vectorized using the max function. % In particular, the max function can also return the index of the % max element, for more information see 'help max'. If your examples % are in rows, then, you can use max(A, [], 2) to obtain the max % for each row. % for i=1:mtemp=all_theta*X(i,:)';p(i)=find(temp==max(temp)); end % =========================================================================end總結
以上是生活随笔為你收集整理的吴恩达 coursera ML 第七课总结+作业答案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吴恩达 coursera ML 第六课总
- 下一篇: 怎样在计算机中创建d盘,我在电脑D盘内新