前兩篇文章微分盔粹、微分2解說(shuō)了關(guān)于微分的幾個(gè)基本概念,比如導(dǎo)數(shù)程癌、切線舷嗡、斜率什么的。這些是學(xué)習(xí)人工智能和機(jī)器學(xué)習(xí)技術(shù)的必要基礎(chǔ)知識(shí)嵌莉,你可以參照下面y=x3/1000函數(shù)圖像進(jìn)行回顧和理解进萄。
當(dāng)然,最好的辦法是用代碼把它實(shí)現(xiàn)一遍锐峭,下面的代碼利用plotly實(shí)現(xiàn)的代碼中鼠,可以參照理解這張圖的原理。
#生成微分示意圖
import plotly.offline as py
import plotly.graph_objs as go
import random
import math
py.init_notebook_mode()
#原函數(shù)
def func(n):
res = math.pow(n, 3) * 0.0001
#y=x^3*0.0001沿癞,0.0001僅用來(lái)縮小豎向值,控制顯示區(qū)域
return res
#計(jì)算某x值對(duì)應(yīng)點(diǎn)的斜率援雇,即dy/dx
def slope(x):
res = 3 * math.pow(x, 2) * 0.0001
#y=2x^2,從常見(jiàn)導(dǎo)數(shù)公式推導(dǎo)f(x)=x^n則f'(x)=(n-1)x^(n-1)
return res
#計(jì)算切線上點(diǎn)的位置
def tangent(x, n):
k = slope(x)
return k * n - (k * x - func(x))
#-----------------------------------
#生成曲線數(shù)據(jù)
def curve(start, end): #f(x)
data = go.Scatter(
x=[n for n in range(start, end)],
y=[func(n) for n in range(start, end)],
name='curve')
return data
#生成切線數(shù)據(jù)
NoTanLegend = True #只顯示第一條線的圖例
def tangentLine(x, ran=10):
global NoTanLegend
data = go.Scatter(
x=[n for n in range(x - ran, x + ran)],
y=[tangent(x, n) for n in range(x - ran, x + ran)],
showlegend=NoTanLegend,
name='tangent',
mode='lines',
line={
'color': 'red',
'width': 4
})
NoTanLegend = False
return data
#生成橫線斜率短線數(shù)據(jù)
NoSlopeLegend = True
def slopeLine(x, scale):
global NoSlopeLegend
k = slope(x)
data = go.Scatter(
x=[n for n in range(x, x + int(k * scale))],
y=[func(x) for n in range(x, x + int(k * scale))],
showlegend=NoSlopeLegend,
name='slope',
mode='lines',
line={
'color': 'green',
'width': 5
})
NoSlopeLegend = False
return data
#生成點(diǎn)數(shù)據(jù)
def points(li): #f(x)
data = go.Scatter(
x=[n for n in li],
y=[func(n) for n in li],
name='points',
mode='markers',
marker={
'size': 8,
'color': 'black'
})
return data
#準(zhǔn)備數(shù)據(jù)
pli = [20, 40, 60, 80] #數(shù)據(jù)點(diǎn)列表
datas = []
datas += [curve(0, 100)]
datas += [tangentLine(p, 8) for p in pli]
datas += [points(pli)]
datas += [slopeLine(p, 10) for p in pli]
#----------------------------------------
#繪圖
layout = go.Layout( #layout 網(wǎng)格坐標(biāo)布局
autosize=False,
width=800,
height=600,
xaxis=dict(range=(0, 100),title='x Axis'),
yaxis=dict(range=(0, 100),title='y Axis'),
)
fig = go.FigureWidget(datas, layout)
py.iplot(fig)
后面的文章會(huì)從這些數(shù)學(xué)知識(shí)的基礎(chǔ)上繼續(xù)介紹偏微分和梯度下降等基礎(chǔ)概念。
每個(gè)人的智能新時(shí)代
如果您發(fā)現(xiàn)文章錯(cuò)誤熊杨,請(qǐng)不吝留言指正;
如果您覺(jué)得有用盗舰,請(qǐng)點(diǎn)喜歡晶府;
如果您覺(jué)得很有用,歡迎轉(zhuǎn)載~
END