天天看點

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

前言

學以緻用,以學促用,通過筆記總結,鞏固學習成果,複習新學的概念。

目錄

文章目錄

  • 前言
  • 目錄
  • 正文
    • 模型引入
    • 神經網絡
  • 模型表示
    • 模型表示2
    • 例子和圖示
    • 例子與圖示2
  • 作業答案

正文

本節主要讨論神經網絡及其強大的功能

模型引入

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

非線性分類問題會帶來超多的參數,也就是參數爆炸這一問題。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

一個典型的例子,計算機視覺目辨別别問題。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

通過訓練分類器,我們可以很好的确定一個像素對應的是不是汽車。

神經網絡

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經網路的發展曆史。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經網絡的誘因,大腦并沒有特定的神經元去處理特定的任務。

模型表示

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經元模型結構圖

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經元模型數學化,邏輯單元。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經元模型,多層感覺機。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

多層神經網絡的數學表示和參數量。

模型表示2

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經網絡向量化實作方法,前向傳遞計算流程。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

神經網絡學到了它的特征。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

其他類似的神經網絡架構。

例子和圖示

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

非線性分類問題:異或。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

簡化例子,邏輯與操作

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

簡化例子,或操作。

例子與圖示2

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

簡單例子,否操作的實作。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

簡單例子:異或操作。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

 應用例子:手寫文本識别。

## 多分類問題

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

多目辨別别問題,通過onehot編碼,輸出圖像裡的各個類别。

吳恩達 coursera ML 第七課總結+作業答案前言目錄正文模型表示作業答案

輸出層也有多個神經元。

作業答案

多分類問題

ex3.m

%% Machine Learning Online Class - Exercise 3 | Part 1: One-vs-all

%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the
%  linear exercise. You will need to complete the following functions
%  in this exericse:
%
%     lrCostFunction.m (logistic regression cost function)
%     oneVsAll.m
%     predictOneVsAll.m
%     predict.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% Setup the parameters you will use for this part of the exercise
input_layer_size  = 400;  % 20x20 Input Images of Digits
num_labels = 10;          % 10 labels, from 1 to 10
                          % (note that we have mapped "0" to label 10)

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset.
%  You will be working with a dataset that contains handwritten digits.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

load('ex3data1.mat'); % training data stored in arrays X, y
m = size(X, 1);

% Randomly select 100 data points to display
rand_indices = randperm(m);
sel = X(rand_indices(1:100), :);

displayData(sel);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% ============ Part 2a: Vectorize Logistic Regression ============
%  In this part of the exercise, you will reuse your logistic regression
%  code from the last exercise. You task here is to make sure that your
%  regularized logistic regression implementation is vectorized. After
%  that, you will implement one-vs-all classification for the handwritten
%  digit dataset.
%

% Test case for lrCostFunction
fprintf('\nTesting lrCostFunction() with regularization');

theta_t = [-2; -1; 1; 2];
X_t = [ones(5,1) reshape(1:15,5,3)/10];
y_t = ([1;0;1;0;1] >= 0.5);
lambda_t = 3;
[J grad] = lrCostFunction(theta_t, X_t, y_t, lambda_t);

fprintf('\nCost: %f\n', J);
fprintf('Expected cost: 2.534819\n');
fprintf('Gradients:\n');
fprintf(' %f \n', grad);
fprintf('Expected gradients:\n');
fprintf(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n');

fprintf('Program paused. Press enter to continue.\n');
pause;
%% ============ Part 2b: One-vs-All Training ============
fprintf('\nTraining One-vs-All Logistic Regression...\n')

lambda = 0.1;
[all_theta] = oneVsAll(X, y, num_labels, lambda);

fprintf('Program paused. Press enter to continue.\n');
pause;


%% ================ Part 3: Predict for One-Vs-All ================


pred = predictOneVsAll(all_theta, X);
fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);



           

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(:);

end


           

onevsall.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 theta
     initial_theta = zeros(n + 1, 1);
%     
%     % Set options for fminunc
    options = 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












% =========================================================================


end

           

predictonevsall.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:m
    temp=all_theta*X(i,:)';
    p(i)=find(temp==max(temp));
end
% =========================================================================


end

           

繼續閱讀