PyTorch深度学习实践03
生活随笔
收集整理的這篇文章主要介紹了
PyTorch深度学习实践03
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
梯度下降
-- codeing = utf-8 --
@Time :2021/4/12 20:25
@Author:sueong
@File:03.py
@Software:PyCharm
import numpy as np import matplotlib.pyplot as pltx_data=[1.0,2.0,3.0] y_data=[2.0,4.0,6.0] w=1.0#初始值假設為1.0 w=w-a*cost對w的偏導cost_list=[] epoch_list=[] def forword(x):return x*wdef cost(xs,ys):l_sum = 1for x,y in zip(x_data,y_data):y_pre = forword(x)l_sum+=(y_pre-y)**2return l_sum/len(xs)def gradient(xs,ys):grad=0for x, y in zip(x_data, y_data):y_pre = forword(x)grad+=2*x*(y_pre-y)return grad/len(xs)print('predict before traing',4,forword(4)) for epoch in range(100):epoch_list.append(epoch)cost_val=cost(x_data,y_data)cost_list.append(cost_val)grad_val=gradient(x_data,y_data)w-=0.01*grad_valprint('Epoch=',epoch,'w=',w,'loss',cost_val) print('predict(after training)',4,forword(4))plt.plot(epoch_list,cost_list) plt.ylabel('epoch') plt.xlabel('cost') plt.show()隨機梯度下降
從左邊變成右邊這樣
總結
以上是生活随笔為你收集整理的PyTorch深度学习实践03的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PyTorch深度学习实践02
- 下一篇: 梳理百年深度学习发展史-七月在线机器学习