PyTorch 深度学习实践 第5讲(用PyTorch实现线性回归 )

  • 本实例是批量数据处理,小伙伴们不要被optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)误导了,以为见了SGD就是随机梯度下降。要看传进来的数据是单个的还是批量的。这里的x_data是3个数据,是一个batch,调用的PyTorch API是 torch.optim.SGD,但这里的SGD不是随机梯度下降,而是批量梯度下降。也就是说,梯度下降算法使用的是随机梯度下降,还是批量梯度下降,还是mini-batch梯度下降,用的API都是 torch.optim.SGD。
  • torch.nn.MSELoss也跟torch.nn.Module有关,参与计算图的构建,torch.optim.SGD与torch.nn.Module无关,不参与构建计算图。
  • 继承于torch.nn.Module的LinearModel是怎样执行线性运算的[0]
  • torch.nn.MSELoss的用法[0]
import torch
# prepare dataset
# x,y是矩阵,3行1列 也就是说总共有3个数据,每个数据只有1个特征
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.
member methods __init__() and forward() have to be implemented
class nn.linear contain two member Tensors: weight and bias
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):
    def __init__(self):
        super(LinearModel, self).__init__()
        # (1,1)是指输入x和输出y的特征维度,这里数据集中的x和y的特征都是1维的,特征是横着排的,样本数是竖着排的。
        # 该线性层需要学习的参数是w和b  获取w/b的方式分别是~linear.weight/linear.bias
        self.linear = torch.nn.Linear(1, 1)
 
    def forward(self, x):
        y_pred = self.linear(x)
        return y_pred
 
model = LinearModel()
 
# construct loss and optimizer
# criterion = torch.nn.MSELoss(size_average = False)
criterion = torch.nn.MSELoss(reduction = 'sum')
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01) # SGD里面的第一个参数是待优化参数。
#model.parameters()里面保存着权重和参数,LinearModel()实例化以后就已经有初始的权重和参数了
 
# training cycle forward, backward, update
for epoch in range(100):
    y_pred = model(x_data) # forward:predict
    loss = criterion(y_pred, y_data) # forward: loss
    print(epoch, loss.item())
 
    optimizer.zero_grad() # the grad computer by .backward() will be accumulated. so before backward, remember set the grad to zero
    loss.backward() # backward: autograd,自动计算梯度
    optimizer.step() # update 参数,即更新w和b的值,w=w-α*梯度;
 
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)
  • **model.parameters()**里面保存着权重和参数,LinearModel()实例化以后就已经有初始的权重和参数了
import torch

class LinearModel(torch.nn.Module):
    def __init__(self):
        super(LinearModel, self).__init__()

        ##构造一个对象,包含Weight(参数w) 和 Bias(参数b) ,之后就可以直接用Linear计算Wx+b
        self.linear = torch.nn.Linear(3, 2)
        print(self.linear.weight)
        print(self.linear.bias)

    def forward(self, x):  ##这里主要考虑的override,覆盖父类的默认函数
        y_pred = self.linear(x)
        return y_pred


model = LinearModel()  ##实例化一个对象
print(list(model.parameters()))

#output:先执行init,再执行print(list(model.parameters()))
'''Parameter containing:
tensor([[-0.5056, -0.4553, -0.2315],
        [-0.2567,  0.1765,  0.4324]], requires_grad=True)
Parameter containing:
tensor([ 0.4731, -0.3612], requires_grad=True)
[Parameter containing:
tensor([[-0.5056, -0.4553, -0.2315],
        [-0.2567,  0.1765,  0.4324]], requires_grad=True), Parameter containing:
tensor([ 0.4731, -0.3612], requires_grad=True)]'''

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年5月8日
下一篇 2022年5月8日

相关推荐