https://pytorch.org/docs/stable/nn.html

image.png

  1. import torch.nn as nn
  1. import torch, torchvision
  2. model = torchvision.models.resnet18(pretrained=True)
  3. data = torch.rand(1, 3, 64, 64)
  4. labels = torch.rand(1, 1000)
  5. # forward pass 正向传播
  6. prediction = model(data)
  7. # backward pass 反向传播
  8. loss = (prediction - labels).sum()
  9. loss.backward()
  10. # 优化器
  11. optim = torch.optim.SGD(model.parameters(), lr=1e-2, momentum=0.9)
  12. # gradient descent 梯度下降
  13. optim.step()

创建model

  1. class My_Model(nn.Module):
  2. def __init__(self, input_dim):
  3. super(My_Model, self).__init__()
  4. # 顺序容器,模块将按顺序执行
  5. self.layers = nn.Sequential(
  6. nn.Linear(input_dim, 16),
  7. nn.ReLU(),
  8. nn.Linear(16, 8),
  9. nn.ReLU(),
  10. nn.Linear(8, 1)
  11. )
  12. def forward(self, x):
  13. x = self.layers(x)
  14. x = x.squeeze(1) # (B, 1) -> (B)
  15. return x