Machine Learning Week4 ex4.m

本文详细介绍了一个神经网络训练过程,包括数据展示、参数读取、损失计算、正则化处理、梯度验证、参数训练及预测等内容。通过使用fmincg函数进行训练,并利用数值梯度检查确保梯度计算正确。

这次作业中的文件都相对来说比较有意思。按照ex4的顺序来进行研究,那么可以看到,首先是displayData,也就是对于数据进行显示。

之后是对于参数的读取。

根据参数来计算损失,此时并不添加regularization。

下一步就是包含regularizatio的J。

simgoidGradient就是sigmoid函数的导函数。

初始化参数的选取是随机的,但是还有一个debug版本的初始化参数的选取,是定值。

Theta1

Theta2

checkNNGradients应该就是对于梯度的计算是否正确的判断了,第一次是lambda=0,第二次是lambda=3,不同的正则化参数下,结果是不一样的。

训练神经网络,使用的是fmincg.m函数。

训练好之后,可视化参数,可以看出,相对来说较为复杂的区域都在中间部分的。

最后是对于数据集合的预测。


computeNumericalGradient.m
这个函数是对于theta的数值的计算。就是更改一个小epislon,然后计算梯度。

function numgrad = computeNumericalGradient(J, theta)
%COMPUTENUMERICALGRADIENT Computes the gradient using "finite differences"
%and gives us a numerical estimate of the gradient.
%   numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical
%   gradient of the function J around theta. Calling y = J(theta) should
%   return the function value at theta.

% Notes: The following code implements numerical gradient checking, and 
%        returns the numerical gradient.It sets numgrad(i) to (a numerical 
%        approximation of) the partial derivative of J with respect to the 
%        i-th input argument, evaluated at theta. (i.e., numgrad(i) should 
%        be the (approximately) the partial derivative of J with respect 
%        to theta(i).)
%                

numgrad = zeros(size(theta));
perturb = zeros(size(theta));
e = 1e-4;
for p = 1:numel(theta)
    % Set perturbation vector
    perturb(p) = e;
    loss1 = J(theta - perturb);
    loss2 = J(theta + perturb);
    % Compute Numerical Gradient
    numgrad(p) = (loss2 - loss1) / (2*e);
    perturb(p) = 0;
end

end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值