当前位置:网站首页>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
边栏推荐
猜你喜欢
Fluent implements a loadingbutton with loading animation
Huawei equipment is configured with OSPF and BFD linkage
B站刘二大人-线性回归 Pytorch
Steady, 35K, byte business data analysis post
[force buckle]43 String multiplication
nacos-高可用seata之TC搭建(02)
In 2022, we must enter the big factory as soon as possible
[leetcode] 18. Sum of four numbers
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
js Array 列表 实战使用总结
随机推荐
Configuration file converted from Excel to Lua
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
初识CDN
Closure, decorator
Nacos TC setup of highly available Seata (02)
Detailed summary of SQL injection
大型网站如何选择比较好的云主机服务商?
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
01. 开发博客项目之项目介绍
C Advanced - data storage (Part 1)
B站刘二大人-线性回归 Pytorch
Modbus协议通信异常
[cloud native] 3.1 kubernetes platform installation kubespher
[leetcode daily question] number of enclaves
Review of double pointer problems
【torch】|torch. nn. utils. clip_ grad_ norm_
Web Security (V) what is a session? Why do I need a session?
How to use PHP string query function
Unity gets the width and height of Sprite
02. 开发博客项目之数据存储