梯度梯度法matlab实现
时间: 2023-11-04 13:06:59 浏览: 122
梯度下降法(Gradient Descent)是一种常用的优化算法,它通过迭代的方式不断调整参数,使得目标函数的值不断降低。Matlab中可以使用以下代码实现梯度下降法:
```matlab
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
% Perform a single gradient step on the parameter vector theta
% Compute hypothesis function
h = X * theta;
% Compute error
error = h - y;
% Compute gradient vector
gradient = X' * error / m;
% Update parameter vector
theta = theta - alpha * gradient;
% Save the cost J in every iteration
J_history(iter) = computeCost(X, y, theta);
end
end
```
其中,X和y是训练数据和标签,theta是模型参数,alpha是学习率(步长),num_iters是迭代次数。computeCost是计算代价函数(损失函数)的函数,具体实现可以参考下面的代码:
```matlab
function J = computeCost(X, y, theta)
m = length(y); % number of training examples
h = X * theta; % hypothesis function
squared_error = (h - y) .^ 2; % squared error
J = 1 / (2 * m) * sum(squared_error); % cost function
end
```
阅读全文