当前位置:网站首页>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
边栏推荐
- [machine learning notes] univariate linear regression principle, formula and code implementation
- [detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
- [leetcode daily question] number of enclaves
- 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
- Modbus protocol communication exception
- 指针经典笔试题
- Force buckle 1189 Maximum number of "balloons"
- [mask requirements of OSPF and Isis in multi access network]
- 05. 博客项目之安全
- Using stopwatch to count code time
猜你喜欢
Self built DNS server, the client opens the web page slowly, the solution
Excel转换为Lua的配置文件
Vulhub vulnerability recurrence 73_ Webmin
Vulhub vulnerability recurrence 67_ Supervisor
js Array 列表 实战使用总结
01. Project introduction of blog development project
05. Security of blog project
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
Game push image / table /cv/nlp, multi-threaded start
04. 项目博客之日志
随机推荐
PDK工艺库安装-CSMC
Graduation design game mall
Codeforces Round #804 (Div. 2) Editorial(A-B)
Promotion hung up! The leader said it wasn't my poor skills
Nacos TC setup of highly available Seata (02)
SQLite add index
[leetcode daily question] number of enclaves
【经验】win11上安装visio
【经验】UltralSO制作启动盘时报错:磁盘/映像容量太小
P2802 回家
ARTS Week 25
[machine learning notes] univariate linear regression principle, formula and code implementation
【torch】|torch.nn.utils.clip_grad_norm_
Easy to understand IIC protocol explanation
SQLite queries the maximum value and returns the whole row of data
RustDesk 搭建一个自己的远程桌面中继服务器
Excel转换为Lua的配置文件
初识CDN
毕业设计游戏商城
In 2022, we must enter the big factory as soon as possible