2 Neural Networks
logistic regression是线性分类,对于一些复杂的类别,引入了非线性的Neural Networks。
一个疑问,Neural Networks是基于logistic regression的,神经元模型也是线性的,怎么NN是非线性的呢?
这周写feedforward propagation algorithm,下周写backpropagation algorithm。
2.1 Model representation
该模型的网络有三层,an input layer, a hidden layer and an output layer。因为X的大小是20×20,input layer units的数目定为400,直接读代码的时候,还以为是随便定的。
2.2 Feedforward Propagation and Prediction
function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
% p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
% trained weights of a neural network (Theta1, Theta2)
% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);
% You need to return the following variables correctly
p = zeros(size(X, 1), 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
% your learned neural network. You should set p to a
% vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. 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.
%
a2=sigmoid([ones(m,1) X]*Theta1');
a3=sigmoid([ones(m,1) a2]*Theta2');
[maxnum,p]=max(a3,[],2);
% =========================================================================
end
网友评论