【发布时间】:2012-05-15 19:11:48
【问题描述】:
我实现了一种梯度下降算法来最小化成本函数,以获得用于确定图像是否具有良好质量的假设。我在八度做到了这一点。这个想法是基于 Andrew Ng 的 machine learning class 算法
因此,我有 880 个值“y”,其中包含从 0.5 到 ~12 的值。我在“X”中有 50 到 300 的 880 个值,应该可以预测图像的质量。
遗憾的是,该算法似乎失败了,经过一些迭代后,theta 的值非常小,以至于 theta0 和 theta1 变为“NaN”。而且我的线性回归曲线有奇怪的值...
这里是梯度下降算法的代码:
(theta = zeros(2, 1);, alpha= 0.01, 迭代次数=1500)
function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);
for iter = 1:num_iters
tmp_j1=0;
for i=1:m,
tmp_j1 = tmp_j1+ ((theta (1,1) + theta (2,1)*X(i,2)) - y(i));
end
tmp_j2=0;
for i=1:m,
tmp_j2 = tmp_j2+ (((theta (1,1) + theta (2,1)*X(i,2)) - y(i)) *X(i,2));
end
tmp1= theta(1,1) - (alpha * ((1/m) * tmp_j1))
tmp2= theta(2,1) - (alpha * ((1/m) * tmp_j2))
theta(1,1)=tmp1
theta(2,1)=tmp2
% ============================================================
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
这里是代价函数的计算:
function J = computeCost(X, y, theta) %
m = length(y); % number of training examples
J = 0;
tmp=0;
for i=1:m,
tmp = tmp+ (theta (1,1) + theta (2,1)*X(i,2) - y(i))^2; %differenzberechnung
end
J= (1/(2*m)) * tmp
end
【问题讨论】:
-
我猜你跳过了矢量化讲座 - 和我一样;)class.coursera.org/ml-007/lecture/30
标签: machine-learning octave gradient-descent