吴恩达机器学习第五周测验及编程作业

代码:https://github.com/LiuZhe6/AndrewNGMachineLearning

测验:Neural Networks: Learning

第一题

在这里插入图片描述
答案
B

第二题

在这里插入图片描述
答案
A

第三题

在这里插入图片描述
答案
D

第四题

在这里插入图片描述
答案
AD
分析:
A:使用梯度检验来检查反向传播是否正确,正确。
B:梯度检验要比反向传播计算损失函数的梯度慢的多,错误。
C:梯度检验对梯度下降算法来说非常有用,错误。
D:为了保证效率,在使用反向传播算法前关闭梯度检验,正确。

第五题

在这里插入图片描述
答案
BC
分析:
A:权重为1不能打破堆成,错误。
B:正确。
C:训练结果可能是到达局部最小值,而不是全局的,正确。
D:一层的权重都是一样的数字不能打破对称,错误。


编程练习:Neural Network Learning

作业一:Feedforward and Cost Function

Hint:根据文档一步一步来做,注意对y向量的转换

nnCostFunction.m

% part1
% 计算假设函数
a1 = [ones(m,1) X];
z2 = a1 * Theta1';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1) a2];
z3 = a2 * Theta2';
a3 = sigmoid(z3);
h = a3;     % 5000 x 10


% y是m x 1向量,需要变成m x 10矩阵
u = eye(num_labels);

% 这条语句有点难理解,大概意思是选出每一行的y值作为u的行标,将这行u替换对应行的y
y = u(y,:);         

J = 1/m * sum(sum(-y .* log(h) - (1 - y) .* log(1 - h)));

% 对X添加一列
X = [ones(m,1) X];

作业二:Regularized Cost Function

Hint:注意双重求和的向量化,注意Theta求和时的下标,不正则化0号!
nnCostFunction.m

下面采用 非向量化和向量化两种方式实现
先看一下效率比较

非向量化用时:ans = 0.0012472
向量化代码用时:ans = 0.00015712

可以看出非向量化代码用时是向量化代码用时的近十倍。

非向量形式的代码,不推荐

sum1 = 0;
sum2 = 0;
for i = 1 : size(Theta1,1)
    sum1 += Theta1(i,:) * Theta1'(:,i) - Theta1(i,1)^2;
end;

for i = 1 : size(Theta2,1)
    sum2 += Theta2(i,:) * Theta2'(:,i) - Theta2(i,1)^2;
end;

J += lambda/(2*m) * (sum1 + sum2);

正则化形式的代码,推荐

% 正则化 (向量化形式)
regularization = lambda / (2*m) * (sum(sum(Theta1(:, 2:end).^2))+ sum(sum(Theta2(: , 2:end).^2 )));
J += regularization;

作业三:Sigmoid Gradient

sigmoidGradient.m

g = sigmoid(z) .* (1 - sigmoid(z));

作业四:Neural Network Gradient (Backpropagation)

nnCostFunction.m
我是按照步骤一步一步来做的,老师上课说推荐第一次使用for循环,我实在没有get到这个意思,大概可能就是说一步一步来做吧!

% part2
delta3 = a3 - y;           % 5000 x 10
delta2 = delta3 * Theta2;  % 5000 x 26
delta2 = delta2(:,2:end);  % 5000 x 25
delta2 = delta2 .* sigmoidGradient(z2);    % 5000 x 25

Delta1 = zeros(size(Theta1));
Delta2 = zeros(size(Theta2));

Delta1 = Delta1 + delta2' * a1;    % 26 x 400
Delta2 = Delta2 + delta3' * a2;    % 10 x 25

作业五:Regularized Gradient

nnCostFunction.m

Theta1_grad = 1 / m * Delta1 + lambda / m * Theta1 ;
Theta2_grad = 1 / m * Delta2 + lambda /m  * Theta2 ;

% 0号元素不用正则化
Theta1_grad(:,1) -=  lambda / m * Theta1(:,1); 
Theta2_grad(:,1) -=  lambda / m * Theta2(:,1);
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值