具体实例理解梯度下降

2次函数通过梯度下降法不断逼近临界值

# -*- coding: utf-8 -*-
"""
Created on Wed Dec 27 16:46:06 2017

@author: Administrator
"""

import numpy as np
import matplotlib.pyplot as plt 
import sympy as sym

def Create_Function(): 
    return x**2+3*x+9
#对创建的函数进行绘图
x=np.arange(-10, 10, 0.01)
y=Create_Function()
plt.plot(x,y)
plt.show()

#对创建的函数进行求导,并获取最后的导函数
x=sym.Symbol("x")
derivation = sym.diff(Create_Function(), x)
print derivation

#对生成的导函数进行赋值,通过f_func计算出最后的结果
f_func = sym.lambdify(x, derivation)
print f_func(10)


old = 20
new= 30
step = 0.01
precision = 0.000001


while abs(new - old) > precision:
    old = new
    new = new - step * f_func(new)
    print new

print new

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值