梯度下降代码:

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 [ theta_old, J_history ] = SVRG( X, y, theta, alpha )

theta_old = theta;

n = length(y);

J_history = zeros(20,1);

m = 2 * n;

for i = 1:20
     theta_ = theta_old;
     Mu = 1/n *  X' * (X*theta_ - y);
     theta_0 = theta_;
     for j = 1:m
         index = randi(n);
         GD_one = (X(index, :) * theta_0 - y(index)) * X(index, :)';
         GD_ = (X(index, :) * theta_ - y(index)) * X(index, :)';
         theta_t = theta_0 - alpha * (GD_one - GD_ + Mu);
         theta_0 = theta_t;
     end
     J_history(i) = ComputeCost(X, y, theta_t);
     theta_old = theta_t;

end

end

损失函数:

function J = ComputeCost( X, y, theta )

m = length(y);

J = sum((X*theta - y).^2) / (2*m);

end

主程序代码:

%% clean workspace

clc;

clear;

close all;

%% plot data

fprintf('plot data... \n');

X = load('ex2x.dat');

y = load('ex2y.dat');

m = length(y);

figure;

plot(X,y,'o');

%% gradient decent

fprintf('Runing gradient decent... \n');

X = [ones(m,1),X];

theta_SGD = zeros(2, 1);

theta_GD = zeros(2, 1);

theta_SVRG = zeros(2, 1);

Iteration = 2000;

alpha = 0.015;

alpha1 = 0.025;

[theta ,J]= StochasticGD(X, y, theta_SGD, alpha, Iteration);

[theta1 ,J1]= GradinentDecent(X, y, theta_GD, alpha, Iteration);

[theta2 ,J2]= SVRG(X, y, theta_SVRG, alpha1);

fprintf('SGD: %f %f\n',theta(1),theta(2));

fprintf('GD: %f %f\n',theta1(1),theta1(2));

fprintf('SVRG: %f %f\n',theta2(1),theta2(2));

hold on;

plot(X(:, 2), X*theta, 'r-');

plot(X(:, 2), X*theta1, 'g-');

plot(X(:, 2), X*theta2, 'b-');

legend('','SGD','GD','SVRG');

x_j = 1:1:20;

figure;

hold on;

plot(x_j, J, 'b-');

plot(x_j, J1, 'g-');

plot(x_j, J2, 'r-');

legend('SGD','GD','SVRG');

xlabel('epoch')

ylabel('loss')

实验结果:

梯度下降、随机梯度下降、方差减小的梯度下降(matlab实现)的更多相关文章

  1. 梯度下降算法对比(批量下降/随机下降/mini-batch)

    大规模机器学习: 线性回归的梯度下降算法:Batch gradient descent(每次更新使用全部的训练样本) 批量梯度下降算法(Batch gradient descent): 每计算一次梯度 ...

  2. 吴恩达机器学习笔记6-梯度下降II(Gradient descent intuition)--梯度下降的直观理解

    在之前的学习中,我们给出了一个数学上关于梯度下降的定义,本次视频我们更深入研究一下,更直观地感受一下这个算法是做什么的,以及梯度下降算法的更新过程有什么意义.梯度下降算法如下: 描述:对

  3. 梯度下降&随机梯度下降&批梯度下降

    梯度下降法 ​ 下面的h(x)是要拟合的函数,J(θ)损失函数,theta是参数,要迭代求解的值,theta求解出来了那最终要拟合的函数h(θ)就出来了.其中m是训练集的记录条数,j是参数的个数. 梯 ...

  4. 梯度下降优化算法综述与PyTorch实现源码剖析

    现代的机器学习系统均利用大量的数据,利用梯度下降算法或者相关的变体进行训练.传统上,最早出现的优化算法是SGD,之后又陆续出现了AdaGrad.RMSprop.ADAM等变体,那么这些算法之间又有哪些 ...

  5. NN优化方法对照:梯度下降、随机梯度下降和批量梯度下降

    1.前言 这几种方法呢都是在求最优解中常常出现的方法,主要是应用迭代的思想来逼近.在梯度下降算法中.都是环绕下面这个式子展开: 当中在上面的式子中hθ(x)代表.输入为x的时候的其当时θ參数下的输出值 ...

  6. 深度学习必备:随机梯度下降(SGD)优化算法及可视化

    补充在前:实际上在我使用LSTM为流量基线建模时候,发现有效的激活函数是elu.relu.linear.prelu.leaky_relu.softplus,对应的梯度算法是adam.mom.rmspr ...

  7. batch、随机、Mini-batch梯度下降

    batch梯度下降: 对所有m个训练样本执行一次梯度下降,每一次迭代时间较长: Cost function 总是向减小的方向下降. 随机梯度下降: 对每一个训练样本执行一次梯度下降,但是丢失了向量化带 ...

  8. L20 梯度下降、随机梯度下降和小批量梯度下降

    airfoil4755 下载 链接:https://pan.baidu.com/s/1YEtNjJ0_G9eeH6A6vHXhnA 提取码:dwjq 梯度下降 (Boyd & Vandenbe ...

  9. [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD

    一.回归函数及目标函数 以均方误差作为目标函数(损失函数),目的是使其值最小化,用于优化上式. 二.优化方式(Gradient Descent) 1.最速梯度下降法 也叫批量梯度下降法Batch Gr ...

随机推荐

  1. 为Linux设置IPTables防火墙

    我们 来讨论一下如何为你的CentOS 服务器来设置简单的防火墙. 这里我们以DigitalOcean的CentOS 6 VPS为基础来讨论的,同样也适用于 阿里云上其他类型的LINUX系统. (阿里 ...

  2. Redis数据的底层存储原理

    redis底层是用什么结构来存储数据的呢? 我们从源码上去理解就会容易的多:   redis底层是使用C语言来编写的,我们可以看到它的数据结构声明.一个 dict 有两个dictht,一个dictht ...

  3. jq双日历--最终版(功能兼容IE5,样式兼容IE6)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. The Struts dispatcher cannot be found. This is usually caused by using Struts

    对于struts2中的问题: org.apache.jasper.JasperException: The Struts dispatcher cannot be found. This is usu ...

  5. Kali-linux服务的指纹识别

    为了确保有一个成功的渗透测试,必须需要知道目标系统中服务的指纹信息.服务指纹信息包括服务端口.服务名和版本等.在Kali中,可以使用Nmap和Amap工具识别指纹信息.本节将介绍使用Nmap和Amap ...

  6. 推荐一个WPF仪表盘开源控件

    前段时间,做服务器端监控系统,为了界面好看,采用WPF.硬件相关监控,比如CPU.内存等,想用仪表盘控件.网上找了很多这种控件,基本上都是第三方商业控件(虽然很漂亮,不过得money...).最后在C ...

  7. [转]VC++获取文件大小集锦

    方法一: WIN32_FIND_DATA fileInfo; HANDLE hFind; DWORD fileSize; const char *fileName = 文件的路径及名字; hFind ...

  8. Linux 启动进程结束进程通用代码

    linux启动springboot项目 start.sh #!/bin/sh rm -f tpid nohup java -jar restDate--SNAPSHOT.jar --spring.pr ...

  9. Jenkins+Ant+Jmeter接口自动化集成测试实例

    Jenkins+Ant+Jmeter接口自动化集成测试实例 一.Jenkins安装配置 1.安装配置JDK1.6+环境变量: 2.下载jenkins.war,放入C:\jenkins目录下,目录位置随 ...

  10. Gradle Goodness: Add Incremental Build Support to Tasks

    Gradle has a very powerful incremental build feature. This means Gradle will not execute a task unle ...