The component and implementation of a basic gradient descent in python
in my impression, the gradient descent is for finding the independent variable that can get the minimum/maximum value of an objective function. So we need an obj. function: \(\mathcal{L}\)
- an obj. function: \(\mathcal{L}\)
- The gradient of \(\mathcal{L}: 2x+2\)
- \(\Delta x\) , The value of idependent variable needs to be updated: \(x \leftarrow x+\Delta x\)
1. the \(\mathcal{L}\) is a context function: \(f(x)=x^2+2x+1\)
how to find the \(x_0\) that makes the \(f(x)\) has the minimum value, via gradient descent?
Start with an arbitrary \(x\), calculate the value of \(f(x)\) :
import random
def func(x):
return x*x + 2*x +1
def gred(x): # the gradient of f(x)
return 2*x + 2
x = random.uniform(-10.0,10.0) #randomly pick a float in interval of (-10, 10)
# x = 10
print('x starts at:', x)
y0 = func(x) #first cal
delta = 0.5 #the value of delta_x, each iteration
x = x + delta
# === interation ===
for i in range(100):
print('i=',i)
y1 = func(x)
delta = -0.08*gred(x)
print(' delta=',delta)
if y1 > y0:
print(' y1>y0')
# if gred(x) is positive, the x should decrease.
# if gred(x) is negative, the x should increase.
else:
print(' y1<=y0')
# if gred(x) is positive, the x should increase.
# if gred(x) is negative, the x should decrease.
x = x+delta
y0 = y1
print(' x=', x, 'f(x)=', y1)
Let's disscuss how to determin the some_value
in the psudo code above.
if \(y_1-y_0\) has a large positive difference, i.e. \(y1 >> y0\), the x should shift backward heavily. so the some_value
can be a ratio of \((y_1-y_0)\times(-gradient)\) , Let's say, some_value
: \(\lambda = r \times\) gred(x) , here, \(r=0.08\) is the step-size.
The basic gradient descent has many shortcomings which can be found by search the 'shortcoming of gd'.
Another problem of GD algorithm is , What if the \(\mathcal{L}\) does not have explicit expression of its gradient?
Stochastic Gradient Descent(SGD) is another GD algorithm.
The component and implementation of a basic gradient descent in python的更多相关文章
- (转)Introduction to Gradient Descent Algorithm (along with variants) in Machine Learning
Introduction Optimization is always the ultimate goal whether you are dealing with a real life probl ...
- Logistic Regression and Gradient Descent
Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...
- (转) An overview of gradient descent optimization algorithms
An overview of gradient descent optimization algorithms Table of contents: Gradient descent variants ...
- 机器学习-随机梯度下降(Stochastic gradient descent)
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- An overview of gradient descent optimization algorithms
原文地址:An overview of gradient descent optimization algorithms An overview of gradient descent optimiz ...
- 机器学习数学基础- gradient descent算法(上)
为什么要了解点数学基础 学习大数据分布式计算时多少会涉及到机器学习的算法,所以理解一些机器学习基础,有助于理解大数据分布式计算系统(比如spark)的设计.机器学习中一个常见的就是gradient d ...
- flink 批量梯度下降算法线性回归参数求解(Linear Regression with BGD(batch gradient descent) )
1.线性回归 假设线性函数如下: 假设我们有10个样本x1,y1),(x2,y2).....(x10,y10),求解目标就是根据多个样本求解theta0和theta1的最优值. 什么样的θ最好的呢?最 ...
- 梯度下降(Gradient Descent)小结
在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的方法是最小二乘法.这里就对梯度下降法做一个完整的总结. 1. 梯度 在微 ...
- 机器学习基础——梯度下降法(Gradient Descent)
机器学习基础--梯度下降法(Gradient Descent) 看了coursea的机器学习课,知道了梯度下降法.一开始只是对其做了下简单的了解.随着内容的深入,发现梯度下降法在很多算法中都用的到,除 ...
随机推荐
- app内嵌vue h5,安卓和ios拦截H5点击事件
安卓和ios拦截h5点击事件,这个函数事件必须是暴漏在window下的 安卓和ios拦截普通h5函数: <div onclick = "show(),window.android.sh ...
- informix 随笔
1.新建连接create new database connection 2.database Url: jdbc:informix-sqli://119.84.39.35:29999/gps_pr ...
- JDK新特性之可变个数的形参的方法
可变个数的形参的方法:1.格式:对于方法的形参来说:数据类型...形参名2.可变个数的形参的方法与同名的方法之间可以构成重载3.可变个数的形参在调用时,个数从0开始,到无穷都可以(在调用这个方法时,s ...
- day05-数据类型与操作
- day01代码
1. 使用while循环打印1,2,3,4,5,7,8,9 # 使用while循环打印1,2,3,4,5,7,8,9 count = 0 while count < 10: count += 1 ...
- 【C#】stream图像转byte的问题
Image xx = Image.FromStream(linkList[ii].stream); byte[] bytes = new Byte[linkList[ii].stream.Length ...
- 第一次使用mybatis
程序使用mybatis的步骤: 1.配置mybatis 涉及到的配置文件有conf.xml和与实体类对应的映射配置文件 (1) conf.xml:配置数据库信息和需要加载的映射文件 <confi ...
- python-实现3级菜单(作业课)
#任务: #显示3级菜单 #1级菜单#显示 3个城市 => 1北京 2上海 3广州 #2级菜单 #显示 选择1 北京 => B1 B2 B3 #2级菜单 #显示 选择2 上海 => ...
- Win10系统下Anaconda下安装多种Python函数库
建议直接安装Anaconda,这是一个包含Numpy,Pandas,Sklearn等函数库的计算机科学软件包,下面的软件可以在此环境下进行安装下载. 一.计算机视觉 1. OpenCV图像处理 在ht ...
- python基础知识10---算法
一.递归 程序本身自己调用自己称之为递归,类似于俄罗斯套娃,体现在代码中:用户执行最外(N)层函数,最外侧调用N-1层函数,N-1层函数调用N-2层函数... 利用函数编写如下数列: 斐波那契数列指的 ...