ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

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

 

posted on 2022-10-22 09:50  哦哟这个怎么搞  阅读(3)  评论(0编辑  收藏  举报