梯度下降实现matlab,梯度下降、随机梯度下降、方差减小的梯度下降(matlab实现)...

本文介绍了在Matlab中实现梯度下降、随机梯度下降和方差减小的梯度下降(SVRG)的详细代码,并通过损失函数计算性能。主程序对比了三种方法在拟合数据上的效果,展示了不同迭代次数的损失变化趋势。
摘要由CSDN通过智能技术生成

梯度下降代码:

function [ theta, J_history ] = GradinentDecent( X, y, theta, alpha, num_iter )

m = length(y);

J_history = zeros(20, 1);

i = 0;

temp = 0;

for iter = 1:num_iter

temp = temp +1;

theta = theta - alpha / m * X' * (X*theta - y);

if temp>=100

temp = 0;

i = i + 1;

J_history(i) = ComputeCost(X, y, theta);

end

end

end

随机梯度下降代码:

function [ theta,J_history ] = StochasticGD( X, y, theta, alpha, num_iter )

m = length(y);

J_history = zeros(20, 1);

temp = 0;

n = 0;

for iter = 1:num_iter

temp = temp + 1;

index = randi(m);

theta = theta -alpha *  (X(index, :) * theta - y(index)) * X(index, :)';

if temp>=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值