当前位置:网站首页>Torch learning notes (3) -- univariate linear regression model (self training)
Torch learning notes (3) -- univariate linear regression model (self training)
2022-07-03 18:22:00 【ZRX_ GIS】
import torch
import numpy as np
import matplotlib.pyplot as plt
# ( One yuan ) Linear regression
# Linear regression is a method of analyzing the relationship between one variable and other variables
# Solving steps :1、 determine model(y = wx + b);2、 choice Loss function (MSE, Cross entropy );3、 Set up LR、 solve grad Used to update parameters w,b
# w = w-LR*W.grad b = b-LR*b.grad
# Case study
# Set training parameters
lr = 0.1 # Learning rate
x = torch.rand((20, 1)) * 10 # adopt torch Create arguments x,x by 20 Distributed in [0,10) The number tensor
print(x.shape)
y = 2 * x + (5 + torch.randn((20, 1))) # adopt x establish y, here ,w=2,b = 5,torch.randn((20, 1)) = noise
# Set linear regression parameters
w = torch.randn((1), requires_grad=True) # Initialize by normal distribution W, Turn on automatic derivation
b = torch.zeros((1), requires_grad=True) # adopt 0 initialization b, Turn on automatic derivation
# iteration
for iteration in range(200):
# forward y_pred = w*x + b
wx = torch.mul(w, x)
y_pred = torch.add(wx, b)
# Loss(MSE)
loss = (0.5 * (y - y_pred) ** 2).mean()
# back
loss.backward()
# up data
b.data.sub_(lr * b.grad)
w.data.sub_(lr * w.grad)
# visualization
if iteration % 20 == 0:
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), y_pred.data.numpy(), r'-', lw=5)
# plt.text(2, 20, 'loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.xlim(1.5, 10)
plt.pause(0.5)
# Stop conditions
if loss.data.numpy()<1:
break

边栏推荐
- [Godot] add menu button
- PHP MySQL where clause
- English語法_名詞 - 分類
- Solve the problem of inaccurate network traffic monitored by ZABBIX with SNMP
- CTO and programmer were both sentenced for losing control of the crawler
- Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
- Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
- PHP MySQL reads data
- Self executing function
- How to analyze the rising and falling rules of London gold trend chart
猜你喜欢

模块九作业

The second largest gay dating website in the world was exposed, and the status of programmers in 2022

2022-2028 global lithium battery copper foil industry research and trend analysis report

How to analyze the rising and falling rules of London gold trend chart

Nodejs (01) - introductory tutorial

2022-2028 global aircraft head up display (HUD) industry research and trend analysis report

What London Silver Trading software supports multiple languages

After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people

Win32: analyse du fichier dump pour la défaillance du tas

Grammaire anglaise Nom - Classification
随机推荐
Valentine's day, send you a little red flower~
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
ES7 - Optimization of promise
WebView module manages the application window interface to realize the logical control and management operation of multiple windows (Part 1)
[combinatorics] generating function (property summary | important generating function)*
How many convolution methods does deep learning have? (including drawings)
An academic paper sharing and approval system based on PHP for computer graduation design
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
MySQL duplicate check
English语法_形容词/副词3级 - 倍数表达
Mature port AI ceaspectus leads the world in the application of AI in terminals, CIMC Feitong advanced products go global, smart terminals, intelligent ports, intelligent terminals
Computer graduation design PHP campus address book telephone number inquiry system
Setinterval CPU intensive- Is setInterval CPU intensive?
解决Zabbix用snmp监控网络流量不准的问题
Sensor debugging process
win32:堆破坏的dump文件分析
A. Berland Poker &1000【简单数学思维】
2022-2028 global copper foil (thickness 12 μ M) industry research and trend analysis report