import numpy as np import random x = np.array([0,1,2,3,4,5,6,7,8,9],dtype=np.float32) y = np.array([1.8,2.1,2.3,2.85,3.0,3.3,4.9,5.45,5.0,6.0]) k = random.random() b = 0 lr = 1e-2 for i in range(1000): prdict = x*k+b l = 0.5*np.sum((y-prdict)**2) if i%100==0: print(f"{i}的损失为{l}") delta_b = np.mean(prdict-y) delta_k = np.mean((prdict-y)*x) k-=lr*delta_k b-=lr*delta_b