这次作业中的文件都相对来说比较有意思。按照ex4的顺序来进行研究,那么可以看到,首先是displayData,也就是对于数据进行显示。
之后是对于参数的读取。
根据参数来计算损失,此时并不添加regularization。
下一步就是包含regularizatio的J。
simgoidGradient就是sigmoid函数的导函数。
初始化参数的选取是随机的,但是还有一个debug版本的初始化参数的选取,是定值。
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