我想用一个特定的假设运行一个有监督的学习算法,这个假设的参数θ在一个不寻常的位置。
y=θ1*(exp(θ2*X))+θ0
我试着用梯度下降来实现以下功能:
代码:

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

其中,计算成本是我的成本函数,即:
predictions = theta(2, 1)*(exp(X*theta(3, 1))) + theta(1, 1); %hypothesis
sqrErrors = (predictions - y).^2;
J = sum(sqrErrors)/(2*m);

当θ(3,1)==θ2变为0,当θ的初始值变为0(3,1)时,这就是我到达间断的地方。
当我的初始θ为1时,它的值为无穷大(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/

10-12 16:39