week4题目和作业

1.题目

图片.png

the above answer is 4X3 dimension but not 3X4

2.作业

display的理解

1.总体解释

图片.png

2.imagesec的解释:

https://www.cnblogs.com/liuke-note/p/10149631.html

3.中心化,标准化,归一化

https://www.jianshu.com/p/95a8f035c86c

displayData

function [h, display_array] = displayData(X, example_width)
%DISPLAYDATA Display 2D data in a nice grid
%   [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
%   stored in X in a nice grid. It returns the figure handle h and the 
%   displayed array if requested.
% Set example_width automatically if not passed in
if ~exist('example_width', 'var') || isempty(example_width) 
    %ex: make 40 become 20*20
    example_width = round(sqrt(size(X, 2)));
end
% Gray Image
colormap(gray);
% Compute rows, cols
[m n] = size(X);
example_height = (n / example_width);
% Compute number of items to display
% show all the image in square (ignore some extra data)
display_rows = floor(sqrt(m));
display_cols = ceil(m / display_rows);
% Between images padding
pad = 1;
% Setup blank display
display_array = - ones(pad + display_rows * (example_height + pad), ...
                       pad + display_cols * (example_width + pad));
% Copy each example into a patch on the display array
curr_ex = 1;
for j = 1:display_rows
    for i = 1:display_cols
        if curr_ex > m, 
            break; 
        end
        % Copy the patch
        % Get the max value of the patch
        max_val = max(abs(X(curr_ex, :)));
        display_array(pad + (j - 1) * (example_height + pad) + (1:example_height), ...
                      pad + (i - 1) * (example_width + pad) + (1:example_width)) = ...
                        reshape(X(curr_ex, :), example_height, example_width) / max_val;
        curr_ex = curr_ex + 1;
    end
    if curr_ex > m, 
        break; 
    end
end
% Display Image
h = imagesc(display_array, [-1 1]);
% Do not show axis
axis image off
drawnow;
end

sigmoid函数

function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.
g = 1.0 ./ (1.0 + exp(-z));
end

costfunction:

J = (-y' * log(sigmoid(X * theta)) - (1 - y)' * log(1 - sigmoid(X * theta))) / m ...
        + lambda / 2 / m * sum(theta(2 : end) .^ 2);
 
temp = theta;
temp(1) = 0;
%正则化不计入theta的第一项
grad = (X' * (sigmoid(X * theta) - y) + lambda * temp) / m;
grad = grad(:);

one-vs-all
这里使用到了逻辑矩阵,即只表示0,1的矩阵
A==const var表示

% 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];

initial_theta = zeros(n+1,1);
cost = zeros(n+1,1);
options = optimset('GradObj','on','MaxIter',50);
for i = 1:num_labels
%logical matrix used
    [all_theta(i,:),cost] = fmincg(@(t)lrCostFunction(t,X,y==i,lambda),initial_theta,options);
end

predict one-vs-all
这里向量化表示用到了max(A,[],2)函数,将矩阵中每一行的最大值取出

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];

% 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.
[p,index] = max(sigmoid(X*all_theta'),[],2);
p = index;

nerual network predict:
神经网络就是有无数个logistic回归组成的

图片.png

像图中,,其他的和第二行得到,第三行往后,以此类推。
下一层亦是如此。

m = size(X, 1);
num_labels = size(Theta2, 1);

% You need to return the following variables correctly 
p = zeros(size(X, 1), 1);

a1 = [ones(m,1) X];
z2 = a1*Theta1';
a2=sigmoid(z2);
a2 = [ones(size(a2,1),1) a2];
z3 = a2*Theta2';
[a3,index] = max(sigmoid(z3),[],2);
p = index;

你可能感兴趣的:(week4题目和作业)