PyTorch学习笔记1

  • 训练过程
import torch
# batch_size, input_dimension, hidden_dimension, output_dimension
N, D_in, H, D_out = 64, 1000, 100, 10
# 模拟一个训练集
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)
# 模型定义有多种方式,这里不提 model
# loss函数定义
loss_fn = torch.nn.MSELoss(reduction='sum')
# 优化器定义
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
for t in range(EPOCH):
   y_pred = model(x)  # 将x传入模型得到y_pred
   loss = loss_fn(y_pred, y)  # 根据标签计算loss
   optimizer.zero_grad()  # 在backward之前清零所有可更新变量的梯度,使用optimizer
   loss.backward()  # 根据模型参数计算loss梯度
   optimizer.step()  # 调用优化器的step()函数更新参数
  • 自定义模型
import torch
class TengNet(torch.nn.Module):
    def __init__(self, D_in, H, D_out):
        super(TengNet, self).__init__()
        self.linear1 = torch.nn.Linear(D_in, H)
        self.linear2 = torch.nn.Linear(H, D_out)
    def forward():
        # 串联tensors运算过程,返回一个预测值
        h_relu = self.linear1(x).clamp(min=0)
        y_pred = self.linear2(h_relu)
        return y_pred
  • 自定义激活函数
import torch
class MyReLU(torch.autograd.Function):
    @staticmethod
    def forward(ctx, input):
        ctx.save_for_backward(input)
        return input.clamp(min=0)
     @staticmethod
      def backward(ctx,  grad_output):
        input = ctx.saved_tensors
        grad_input = grad_output.clone()
        grad_input[input < 0] = 0
        return grad_input

relu = MyReLU.apply
         
  • LSTM参数说明 torch.nn.LSTM(*arg, **kwarg)
"""
input_size – The number of expected features in the input x
输入特征维数,即输入的一维向量的长度
hidden_size – The number of features in the hidden state h
隐藏层状态的维数,隐藏节点个数
num_layers – Number of recurrent layers. 
层数,上一层作为下一层的输入,执行方式与输入层到上一层一致,默认为1
bias – If False, then the layer does not use bias weights b_ih and b_hh. Default: True
batch_first – If True, then the input and output tensors are provided as (batch, seq, feature). Default: False
dropout – If non-zero, introduces a Dropout layer on the outputs of each LSTM layer except the last layer, with dropout probability equal to dropout. Default: 0
bidirectional – If True, becomes a bidirectional LSTM. Default: False
"""

你可能感兴趣的:(PyTorch学习笔记1)