自写梯度下降算法来求最合适的权值
import numpy as np
import matplotlib.pyplot as plt
本代码想要用的随机梯度(也就是随机某一个具体x值处的梯度,但实际上每一次更新了三次梯度值,并不是随机梯度)
x_data = [1,2,3]
y_data = [2,4,6]
w=1.0
def forward(x):
return x*w
def loss(x,y):
y_pred = forward(x)
return (y_pred-y)**2
def gradient(x,y):
return 2*x*(x*w-y)
for epoch in range(80):
for x,y in zip(x_data,y_data):
grad = gradient(x,y)
w = w-0.01*grad
l = loss(x,y)
print(f'epoch={epoch},w={w:.2f},loss={l:.2f}')
print(f'训练后x=4,y的预测值为:{forward(4):.2f}')
epoch=0,w=1.26,loss=4.92
epoch=1,w=1.45,loss=2.69
epoch=2,w=1.60,loss=1.47
epoch=3,w=1.70,loss=0.80
epoch=4,w=1.78,loss=0.44
epoch=5,w=1.84,loss=0.24
epoch=6,w=1.88,loss=0.13
epoch=7,w=1.91,loss=0.07
epoch=8,w=1.93,loss=0.04
epoch=9,w=1.95,loss=0.02
epoch=10,w=1.96,loss=0.01
epoch=11,w=1.97,loss=0.01
epoch=12,w=1.98,loss=0.00
epoch=13,w=1.99,loss=0.00
epoch=14,w=1.99,loss=0.00
epoch=15,w=1.99,loss=0.00
epoch=16,w=1.99,loss=0.00
epoch=17,w=2.00,loss=0.00
epoch=18,w=2.00,loss=0.00
epoch=19,w=2.00,loss=0.00
epoch=20,w=2.00,loss=0.00
epoch=21,w=2.00,loss=0.00
epoch=22,w=2.00,loss=0.00
epoch=23,w=2.00,loss=0.00
epoch=24,w=2.00,loss=0.00
epoch=25,w=2.00,loss=0.00
epoch=26,w=2.00,loss=0.00
epoch=27,w=2.00,loss=0.00
epoch=28,w=2.00,loss=0.00
epoch=29,w=2.00,loss=0.00
epoch=30,w=2.00,loss=0.00
epoch=31,w=2.00,loss=0.00
epoch=32,w=2.00,loss=0.00
epoch=33,w=2.00,loss=0.00
epoch=34,w=2.00,loss=0.00
epoch=35,w=2.00,loss=0.00
epoch=36,w=2.00,loss=0.00
epoch=37,w=2.00,loss=0.00
epoch=38,w=2.00,loss=0.00
epoch=39,w=2.00,loss=0.00
epoch=40,w=2.00,loss=0.00
epoch=41,w=2.00,loss=0.00
epoch=42,w=2.00,loss=0.00
epoch=43,w=2.00,loss=0.00
epoch=44,w=2.00,loss=0.00
epoch=45,w=2.00,loss=0.00
epoch=46,w=2.00,loss=0.00
epoch=47,w=2.00,loss=0.00
epoch=48,w=2.00,loss=0.00
epoch=49,w=2.00,loss=0.00
epoch=50,w=2.00,loss=0.00
epoch=51,w=2.00,loss=0.00
epoch=52,w=2.00,loss=0.00
epoch=53,w=2.00,loss=0.00
epoch=54,w=2.00,loss=0.00
epoch=55,w=2.00,loss=0.00
epoch=56,w=2.00,loss=0.00
epoch=57,w=2.00,loss=0.00
epoch=58,w=2.00,loss=0.00
epoch=59,w=2.00,loss=0.00
epoch=60,w=2.00,loss=0.00
epoch=61,w=2.00,loss=0.00
epoch=62,w=2.00,loss=0.00
epoch=63,w=2.00,loss=0.00
epoch=64,w=2.00,loss=0.00
epoch=65,w=2.00,loss=0.00
epoch=66,w=2.00,loss=0.00
epoch=67,w=2.00,loss=0.00
epoch=68,w=2.00,loss=0.00
epoch=69,w=2.00,loss=0.00
epoch=70,w=2.00,loss=0.00
epoch=71,w=2.00,loss=0.00
epoch=72,w=2.00,loss=0.00
epoch=73,w=2.00,loss=0.00
epoch=74,w=2.00,loss=0.00
epoch=75,w=2.00,loss=0.00
epoch=76,w=2.00,loss=0.00
epoch=77,w=2.00,loss=0.00
epoch=78,w=2.00,loss=0.00
epoch=79,w=2.00,loss=0.00
训练后x=4,y的预测值为:8.00