当前位置:网站首页>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
边栏推荐
- Game push: image / table /cv/nlp, multi-threaded start!
- RustDesk 搭建一个自己的远程桌面中继服务器
- 图数据库ONgDB Release v-1.0.3
- 2022半年总结
- How to download GB files from Google cloud hard disk
- C Advanced - data storage (Part 1)
- 03. Login of development blog project
- Check the useful photo lossless magnification software on Apple computer
- Talking about the type and function of lens filter
- 注释、接续、转义等符号
猜你喜欢
JS array list actual use summary
How to use PHP string query function
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
网站进行服务器迁移前应做好哪些准备?
Application Security Series 37: log injection
js Array 列表 实战使用总结
Configuration file converted from Excel to Lua
Vulhub vulnerability recurrence 73_ Webmin
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
59. Spiral matrix
随机推荐
Summary of deep learning tuning tricks
In 2022, we must enter the big factory as soon as possible
Please wait while Jenkins is getting ready to work
改善Jpopup以实现动态控制disable
C AES encrypts strings
How to use PHP string query function
指針經典筆試題
Huawei equipment is configured with OSPF and BFD linkage
Vulhub vulnerability recurrence 71_ Unomi
Yyds dry inventory SSH Remote Connection introduction
移植InfoNES到STM32
[leetcode daily question] number of enclaves
(column 22) typical column questions of C language: delete the specified letters in the string.
【华为机试真题详解】统计射击比赛成绩
59. Spiral matrix
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
Cuda11.1 online installation
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Modbus协议通信异常