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

该博客详细介绍了如何在MATLAB中实现三种梯度下降法:梯度下降法、随机梯度下降法(SGD)以及方差减小的梯度下降法(SVRG)。通过提供的代码示例,演示了如何用这些方法进行参数优化,并在数据集上运行以展示不同方法的损失函数变化和结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

梯度下降代码: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>=100

temp = 0;

n = n + 1;

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

end

end

end

方差减小的梯度下降(SVRG):function [ th

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值