我想运行一个带有特定假设的监督学习算法,该假设的参数 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 的初始值设为 zeros(3, 1) 时,我的 theta(3, 1)==theta2 变为零,这就是我遇到的中断 当我的初始 theta 为 ones(3, 1) 时,它的值为无穷大
那么,我可以将这个假设用于线性回归,还是可以使用任何其他类似的假设函数来代替当前假设。
最佳答案
Python 函数 scipy.optimize.curve_fit
有一个示例,您的函数恰好适合!
查看:https://docs.scipy.org/doc/scipy-0.19.1/reference/generated/scipy.optimize.curve_fit.html
关于algorithm - 我想运行一个具有指定假设的监督学习算法,该假设的参数 theta 处于异常位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46088701/