当前位置:网站首页>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
边栏推荐
- Summary of deep learning tuning tricks
- [Tang Laoshi] C -- encapsulation: classes and objects
- [machine learning notes] univariate linear regression principle, formula and code implementation
- [JVM] [Chapter 17] [garbage collector]
- Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
- Web Security (V) what is a session? Why do I need a session?
- How to download GB files from Google cloud hard disk
- Notes, continuation, escape and other symbols
- Please wait while Jenkins is getting ready to work
- In 2022, we must enter the big factory as soon as possible
猜你喜欢

flutter 实现一个有加载动画的按钮(loadingButton)

59. Spiral matrix

Huawei equipment is configured with OSPF and BFD linkage

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

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

Sword finger offer II 039 Maximum rectangular area of histogram

04. 项目博客之日志

图数据库ONgDB Release v-1.0.3

【OSPF 和 ISIS 在多路访问网络中对掩码的要求】

Unity Vector3. Use and calculation principle of reflect
随机推荐
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
B站刘二大人-线性回归 Pytorch
【LeetCode】18、四数之和
Problems encountered in installing mysql8 on MAC
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
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Sliding window problem review
Fluent implements a loadingbutton with loading animation
[cloud native] 3.1 kubernetes platform installation kubespher
How to get list length
Anti shake and throttling are easy to understand
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
Redis消息队列
【torch】|torch.nn.utils.clip_grad_norm_
29io stream, byte output stream continue write line feed
Tetris
初识CDN
Talking about the type and function of lens filter
ArcGIS应用基础4 专题图的制作
【SQL server速成之路】——身份验证及建立和管理用户账户