当前位置:   article > 正文

Pytorch 学习(五):Pytorch 实现多层感知机(MLP)_pytorch def __init__(self, n_i, n_h, n_o):

pytorch def __init__(self, n_i, n_h, n_o):

Pytorch 实现多层感知机(MLP)

本方法总结自《动手学深度学习》(Pytorch版)github项目

实现多层感知器(Multlayer Perceptron)同样遵循以下步骤:

  • 数据集读取
  • 模型搭建和参数初始化
  • 损失函数和下降器构建
  • 模型训练

方法一:从零开始实现

  1. import torch
  2. import torch.nn as nn
  3. import numpy as np
  4. import d2lzh_pytorch as d2l
  5. # 各层节点数
  6. num_i = 28 * 28
  7. num_h = 256
  8. num_o = 10
  9. # 构建数据
  10. batch_size = 256
  11. train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
  12. # 参数初始化
  13. w1 = torch.tensor(np.random.normal(0, 0.01, (num_i, num_h)), dtype=torch.float32, requires_grad=True)
  14. b1 = torch.zeros(num_h, requires_grad=True)
  15. w2 = torch.tensor(np.random.normal(0, 0.01, (num_h, num_o)), dtype=torch.float32, requires_grad=True)
  16. b2 = torch.zeros(num_o, requires_grad=True)
  17. params = [w1, b1, w2, b2]
  18. # 激活函数
  19. def relu(x):
  20. return torch.max(x, torch.tensor(0.0))
  21. # 模型构建
  22. def net(x):
  23. x = x.view(-1, num_i)
  24. h = relu(x.mm(w1) + b1)
  25. o = h.mm(w2) + b2
  26. return o
  27. # 损失函数
  28. loss = nn.CrossEntropyLoss()
  29. # 训练模型
  30. num_epochs = 5
  31. lr = 100.0
  32. d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)

方法二:能调包就不实现

  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.init as init
  4. import torch.optim as optim
  5. import d2lzh_pytorch as d2l
  6. # node number of MLP Layer
  7. num_i, num_h, num_o = 28 * 28, 256, 10
  8. # data load
  9. batch_size = 256
  10. train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
  11. # network build
  12. class MLP(nn.Module):
  13. def __init__(self, n_i, n_h, n_o):
  14. super(MLP, self).__init__()
  15. self.flatten = d2l.FlattenLayer()
  16. self.linear1 = nn.Linear(n_i, n_h)
  17. self.relu = nn.ReLU()
  18. self.linear2 = nn.Linear(n_h, n_o)
  19. def forward(self, input):
  20. return self.linear2(self.relu(self.linear1(self.flatten(input))))
  21. net = MLP(num_i, num_h, num_o)
  22. for param in net.parameters():
  23. init.normal_(param, mean=0, std=0.01)
  24. # loss
  25. loss = nn.CrossEntropyLoss()
  26. # optimizer
  27. optimizer = optim.SGD(net.parameters(), lr=0.5)
  28. # train
  29. num_epochs = 5
  30. d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, optimizer=optimizer)
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号