1
我想運行監督學習算法與一個指定的假設有一個參數theta在一個不尋常的位置。我想運行一個監督學習算法與一個指定的假設,其參數theta在一個不尋常的位置
Y = theta1 *(EXP(theta2 * X))+ theta0
我使用梯度下降用下面的函數嘗試:
代碼:
m = length(y);
num_iters = 500;
J_history = zeros(num_iters, 1);
alpha = 0.1;
theta = zeros(3, 1);
for q = 1:m
A(q,:) = [2, (2*exp(theta(3, 1) * X(q, 1))), (2*theta(2, 1)*X(q, 1)*exp(theta(3, 1) * X(q, 1)))];
end
for iter = 1:num_iters
num_theta = length(theta);
for j = 1:num_theta
inner_sum = 0;
for i = 1:m
inner_sum = inner_sum + (theta(2, 1)*(exp(X(i, 1)*theta(3, 1))) + theta(1, 1) - y(i, 1)) * A(i, j);
end
theta(j, 1) = theta(j, 1) - (alpha * inner_sum/m)
end
J_history(iter) = compute_cost(X, y);
end
% Save the cost J in every iteration
J_history(iter) = compute_cost(X, y);
end
其中compute_cost是我的成本功能是:
predictions = theta(2, 1)*(exp(X*theta(3, 1))) + theta(1, 1); %hypothesis
sqrErrors = (predictions - y).^2;
J = sum(sqrErrors)/(2*m);
現在,這是我到達了一個斷層,因爲我的theta (3,1)==當θ的初始值爲零時,theta2變爲零(3,1) ,當我的初始θ值爲1時,它取值無限(3,1)
因此,我可以使用這個假設進行線性迴歸,還是可以使用其他類似的假設函數來代替當前的假設。
這裏A是3 * 1矩陣,其是可變的,並且在梯度下降被用來證明J'(THETA),即相對於theta0,theta1,theta2偏微分 –