SGD
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
List<float[]> inputs_x = new List<float[]>();
inputs_x.Add( new float[] { 0.9f, 0.6f});
inputs_x.Add(new float[] { 2f, 2.5f } );
inputs_x.Add(new float[] { 2.6f, 2.3f });
inputs_x.Add(new float[] { 2.7f, 1.9f }); List<float> inputs_y = new List<float>();
inputs_y.Add( 2.5f);
inputs_y.Add( 2.5f);
inputs_y.Add( 3.5f);
inputs_y.Add( 4.2f); float[] weights = new float[3];
for (var i= 0;i < weights.Length;i++)
weights[i] = (float)new Random().NextDouble(); int epoch = 30000;
float epsilon =0.00001f;
float lr = 0.01f; float lastCost=0; for (var epoch_i = 0; epoch_i <= epoch; epoch_i++)
{
//随机获取input
var batch = GetRandomBatch(inputs_x, inputs_y, 2); float[] weights_in_poch = new float[weights.Length]; foreach (var x_y in batch)
{
var x1 = x_y.Item1.First();
var x2 = x_y.Item1.Skip(1).Take(1).First();
var target_y = x_y.Item2; float diffWithTargetY = target_y - fun(x1, x2, weights[1], weights[2], weights[0]); weights_in_poch[0] += diffWithTargetY * dy_b(x1, x2);
weights_in_poch[1] += diffWithTargetY * dy_theta1(x1, x2);
weights_in_poch[2] += diffWithTargetY * dy_theta2(x1, x2);
} for(var i=0;i<weights.Length;i++)
weights[i] += lr * weights_in_poch[i]; float totalErrorCost = 0f;
foreach (var x_y in batch)
{
var x1 = x_y.Item1.First();
var x2 = x_y.Item1.Skip(1).Take(1).First();
var target_y = x_y.Item2; float diffWithTargetY = target_y - fun(x1, x2, weights[1], weights[2], weights[0]);
totalErrorCost += (float)System.Math.Pow(diffWithTargetY, 2)/2;
} float cost = totalErrorCost / batch.Count; if (System.Math.Abs(cost - lastCost) <= epsilon)
{
Console.WriteLine(string.Format("EPOCH {0}", epoch_i));
Console.WriteLine(string.Format("LAST MSE {0}", lastCost));
Console.WriteLine(string.Format("MSE {0}", cost));
break;
} lastCost = cost; if (epoch_i % 100 == 0|| epoch_i==epoch)
{
Console.WriteLine(string.Format("MSE {0}", cost));
}
} print(weights[1], weights[2], weights[0]); Console.ReadLine();
} private static List<Tuple<float[], float>> GetRandomBatch(List<float[]> inputs_x, List<float> inputs_y, int maxCount)
{
List<Tuple<float[], float>> lst = new List<Tuple<float[], float>>(); System.Random rnd = new Random((int)DateTime.Now.Ticks); int count = 0;
while (count<maxCount)
{
int rndIndex = rnd.Next(inputs_x.Count);
var item=Tuple.Create<float[], float>(inputs_x[rndIndex], inputs_y[rndIndex]);
lst.Add(item);
count++;
} return lst;
} private static void print(float theta1, float theta2, float b)
{
Console.WriteLine(string.Format("y={0}*x1+{1}*x2+{2}", theta1, theta2, b));
}
private static float fun(float x1, float x2, float theta1, float theta2, float b)
{
return theta1 * x1 + theta2 * x2 + b;
}
private static float dy_theta1(float x1, float x2)
{
return x1;
} private static float dy_theta2(float x1, float x2)
{
return x2;
} private static float dy_b(float x1, float x2)
{
return 1;
}
}
}
SGD的更多相关文章
- [Machine Learning] 梯度下降法的三种形式BGD、SGD以及MBGD
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...
- 为什么是梯度下降?SGD
在机器学习算法中,为了优化损失函数loss function ,我们往往采用梯度下降算法来进行优化.举个例子: 线性SVM的得分函数和损失函数分别为: ...
- 【原创】batch-GD, SGD, Mini-batch-GD, Stochastic GD, Online-GD -- 大数据背景下的梯度训练算法
机器学习中梯度下降(Gradient Descent, GD)算法只需要计算损失函数的一阶导数,计算代价小,非常适合训练数据非常大的应用. 梯度下降法的物理意义很好理解,就是沿着当前点的梯度方向进行线 ...
- 逻辑回归:使用SGD(Stochastic Gradient Descent)进行大规模机器学习
Mahout学习算法训练模型 mahout提供了许多分类算法,但许多被设计来处理非常大的数据集,因此可能会有点麻烦.另一方面,有些很容易上手,因为,虽然依然可扩展性,它们具有低开销小的数据集.这样一个 ...
- [Machine Learning] 梯度下降(BGD)、随机梯度下降(SGD)、Mini-batch Gradient Descent、带Mini-batch的SGD
一.回归函数及目标函数 以均方误差作为目标函数(损失函数),目的是使其值最小化,用于优化上式. 二.优化方式(Gradient Descent) 1.最速梯度下降法 也叫批量梯度下降法Batch Gr ...
- 监督学习:随机梯度下降算法(sgd)和批梯度下降算法(bgd)
线性回归 首先要明白什么是回归.回归的目的是通过几个已知数据来预测另一个数值型数据的目标值. 假设特征和结果满足线性关系,即满足一个计算公式h(x),这个公式的自变量就是已知的数据x,函数值h(x)就 ...
- tensorflow实现最基本的神经网络 + 对比GD、SGD、batch-GD的训练方法
参考博客:https://zhuanlan.zhihu.com/p/27853521 该代码默认是梯度下降法,可自行从注释中选择其他训练方法 在异或问题上,由于训练的样本数较少,神经网络简单,训练结果 ...
- 深度学习——优化器算法Optimizer详解(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)
在机器学习.深度学习中使用的优化算法除了常见的梯度下降,还有 Adadelta,Adagrad,RMSProp 等几种优化器,都是什么呢,又该怎么选择呢? 在 Sebastian Ruder 的这篇论 ...
- 【深度学习】深入理解优化器Optimizer算法(BGD、SGD、MBGD、Momentum、NAG、Adagrad、Adadelta、RMSprop、Adam)
在机器学习.深度学习中使用的优化算法除了常见的梯度下降,还有 Adadelta,Adagrad,RMSProp 等几种优化器,都是什么呢,又该怎么选择呢? 在 Sebastian Ruder 的这篇论 ...
- 【DeepLearning】优化算法:SGD、GD、mini-batch GD、Moment、RMSprob、Adam
优化算法 1 GD/SGD/mini-batch GD GD:Gradient Descent,就是传统意义上的梯度下降,也叫batch GD. SGD:随机梯度下降.一次只随机选择一个样本进行训练和 ...
随机推荐
- 深入理解CSS盒模型
如果你在面试的时候面试官让你谈谈对盒模型的理解,你是不是不知从何谈起.这种看似简单的题其实是最不好答的. 下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种 ...
- nuget.server搭建及实际测试
1.背景 由于所做项目越来越多,会积累一些公用组件,而每个项目中组件引用中如果组件有更新或者新增为了方便需要一个专门的工具进行管理,那么nuget就是不错的选择. 2.安装nuget.server 这 ...
- mysql主从同步+mycat读写分离+.NET程序连接mycat代理
背景 最近新项目需要用到mysql数据库,并且由于数据量大的原因,故打算采用1主1从(主数据库负责增.删.改操作:从数据库负责查操作)的数据库架构,在实现主从之后还要实现读写分离的代理,在网上搜寻了很 ...
- Shodan在渗透测试及漏洞挖掘中的一些用法
渗透测试中,第一阶段就是信息搜集,这一阶段完成的如何决定了你之后的进行是否顺利,是否更容易.而关于信息收集的文章网上也是有太多.今天我们来通过一些例子来讲解如何正确使用Shodan这一利器. 想要利用 ...
- 《Linux命令行与shell脚本编程大全》第十五章 呈现数据
15.1 理解输入和输出 现在知道两种显示脚本输出的方法 1)在显示器屏幕上显示 2)将输出文件重定向到文件中 15.1.1 标准文件描述符 Linux系统将每个对象当做文件处理.这包括输入和数出进程 ...
- 《java.util.concurrent 包源码阅读》23 Fork/Join框架之Fork的冰山一角
上篇文章一直追踪到了ForkJoinWorkerThread的pushTask方法,仍然没有办法解释Fork的原理,那么不妨来看看ForkJoinWorkerThread的run方法: public ...
- CM5(5.11.0)和CDH5(5.11.0)离线安装
概述 文件下载 系统环境搭建 日志查看 Q&A 参考 概述 CDH (Cloudera's Distribution, including Apache Hadoop),是Hadoop众多分支 ...
- Android - "cause failed to find target android-14" 问题
在导入别人的工程项目时经常会遇到各种问题,本文中的就是其中SDK不对导致的 在导入项目时已经修改了 两个build.gradle文件 错误的原因是后面中这两项没修改. compileSdkVers ...
- .Net Core 2.0 EntityFrameworkCore CodeFirst入门教程
最近难得有时间闲下来,研究了一下.net core 2.0,总的来说,目前除了一些第三方的库不支持外,基本上可以满足我们的项目需求了! 我们就以一个网站开发为例,搭建一个简单的三层架构,先熟悉一下.n ...
- eclipse中 web项目缺少tomcatl lib的解决办法
1.最近在搭建的项目中,将项目导入eclipse中突然报好多错误,查看后全是丢失tomcat的lib包的错误,莫名其妙的错误. 代码中缺少的也是这样的问题 很明显,我之前的包丢了,莫名其妙的丢了. 解 ...