当前位置:网站首页>B站刘二大人-线性回归及梯度下降
B站刘二大人-线性回归及梯度下降
2022-07-06 05:33:00 【宁然也】
系列文章目录
前言
Lecture 3
自己写的代码跑出来的图是直线,原因:学习率alpha设置的太大,由0.1设置为0.005就会变成曲线
一、代码
import matplotlib.pyplot as plt
import torch
import numpy as np
import csv
import os
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
def forward(x,w):
return x*w
def gradient(xs, ys,w):
grad = 0
for(x,y) in zip(xs,ys):
grad += (x*w-y)*x
grad = 2*grad/len(xs)
return grad
def loss(xs, ys, w):
los = 0
for(x,y) in zip(xs,ys):
los += (x*w-y)*(x*w-y)
los = los/len(xs)
return los
w_list = []
mse_list = []
alpha = 0.1
w = 1.0
ite = 100
for i in range(ite):
los = loss(x_data, y_data, w)
mse_list.append(los / 3.0)
w_list.append(w)
w = w - alpha*gradient(x_data, y_data, w)
plt.plot(w_list,mse_list)
plt.xlabel("w")
plt.ylabel("mse")
plt.show()
上图学习率0.1
上图:学习率0.005
边栏推荐
猜你喜欢
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
Sword finger offer II 039 Maximum rectangular area of histogram
01. 开发博客项目之项目介绍
PDK工艺库安装-CSMC
Vulhub vulnerability recurrence 73_ Webmin
B站刘二大人-数据集及数据加载 Lecture 8
04. 项目博客之日志
Yyds dry inventory SSH Remote Connection introduction
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
【经验】win11上安装visio
随机推荐
Easy to understand IIC protocol explanation
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
HAC集群修改管理员用户密码
[leetcode] 18. Sum of four numbers
改善Jpopup以实现动态控制disable
HAC cluster modifying administrator user password
How to download GB files from Google cloud hard disk
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
指針經典筆試題
[detailed explanation of Huawei machine test] statistics of shooting competition results
How to get list length
59. Spiral matrix
B站刘二大人-数据集及数据加载 Lecture 8
UCF(2022暑期团队赛一)
【torch】|torch. nn. utils. clip_ grad_ norm_
PDK工藝庫安裝-CSMC
Pytorch代码注意的细节,容易敲错的地方
[mask requirements of OSPF and Isis in multi access network]
2022 half year summary
剑指 Offer II 039. 直方图最大矩形面积