《PyTorch深度学习实践》Lecture_05 用Pytorch实现线性回归 Linear Regression with PyTorch

B站刘二大人老师的《PyTorch深度学习实践》Lecture_05 重点回顾+代码复现

Lecture_05 用Pytorch实现线性回归 Linear Regression with PyTorch

一、重点回顾

用Pytorch实现线性回归的四大步骤

(一)数据集准备 Prepare Dataset

import torch
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])

(二)建立回归模型 Design model using Class

Our model class should be inherit from nn.Module, which is Base class for all neural network modules.

Class nn.Linear has implemented the magic method __call__(), which enable the instance of the class can be called just like a function. Normally the forward() will be called.

class LinearModel(torch.nn.Module):
	# Member methods __init__() and forward() have to be implemented.
	def __init__(self):
		super(LinearModel, self).__init__()
		self.linear = torch.nn.Linear(1, 1)
		# torch.nn.Linear(in_features: int, out_features: int, bias: bool = True)
	def forward(self, x):
		y_pred = self.linear(x)
		return y_pred
		
model = LinearModel()
class Foobar:
	def __init__(self):
		pass
	def __call__(self,*args,**kwargs):
		pass

(三)构建损失函数和优化器 Construct loss and optimizer

criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)

torch.nn.MSELoss(size_average=None, reduce=None, reduction: str = ‘mean’)
torch.optim.SGD(params, lr=, momentum=0, dampening=0, weight_decay=0, nesterov=False)

(四)训练 Training cycle

三个阶段:forward、backward、Update

for epoch in range(50):
	# forward
	y_pred = model(x_data)
	loss = criterion(y_pred,y_data)
	print(epoch,loss.item())
	
	# 梯度清零
	optimizer.zero_grad()
	# backward
	loss.backward()
	# Update
	optimizer.step()

NOTICE:
The grad computed by .backward() will be accumulated. So before backward, remember set the grad to ZERO!!!

二、代码复现

import torch

# Prepare dataset
x_data = torch.Tensor([[1.0],[2.0],[3.0]])
y_data = torch.Tensor([[2.0],[4.0],[6.0]])

# Design Model
class LinearModel(torch.nn.Module):
    def __init__(self):#构造函数
        super(LinearModel,self).__init__()
        self.linear = torch.nn.Linear(1,1)

    def forward(self, x):
        y_pred = self.linear(x)
        return  y_pred

model = LinearModel()

# Construct Loss & Optimizer
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)

# Training Cycle
for epoch in range(1000):
	y_pred = model(x_data)
	loss = criterion(y_pred,y_data)
	print(epoch,loss.item())

	optimizer.zero_grad()
	loss.backward()
	optimizer.step()

# Test Model
print('w = ',model.linear.weight.item())
print('b = ',model.linear.bias.item())

x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ', y_test.data)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值