吴恩达机器学习 - 神经网络及练习题ex3

2018-11-15  本文已影响0人  YANWeichuan

问题提出

随着特征的增多,线性回归已经难以解决问题。

仿生大脑

树突-> input, 轴突->output, 神经元-> h_θ(x)

神经网络模型

当前层系数θ和输入相乘运算后,通过sigmoid产生输出。θ_{ij}^{(l)}, 其中i对应的为K分类的序号,j对应的特征值X的序号,l则为网络层数的序号。

向前传播计算

按层运算,输入层->sigmoid->隐藏层->sigmoid-> 输出

多元分类

one-hot编码输出

ex3练习

神经网络的模型,注意每层都要加上bias

注意输入各个矩阵的维数:

实现代码
注意矩阵的乘法的维数,做适当的转置。最终通过max函数,产生每列的最大预测值的编号,即为我们预测的结果。

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.
%




alpha1 = [ones(size(X, 1), 1) X];
z2 = Theta1 * alpha1';
alpha2 = sigmoid(z2);
alpha2 = [ones(1, size(alpha2, 2)); alpha2];
z3 = Theta2 * alpha2;
alpha3 = sigmoid(z3);
[maxx, pp] = max(alpha3);
p = pp';


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


end

运行结果:
精度97.52%

数据展示

预测结果

上一篇 下一篇

猜你喜欢

热点阅读