赞
踩
上一篇文章中我们自己动手实现了一个多层感知机,下面我们使用PyTorch来实现一个多层感知机的训练,这种方式更加的方便快捷。
首先导入所需的包或模块。
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
import d2lzh_pytorch as d2l
和softmax回归唯一的不同在于,我们多加了一个全连接层作为隐藏层。它的隐藏单元个数为256,并使用ReLU函数作为激活函数。
num_inputs, num_outputs, num_hiddens = 784, 10, 256
net = nn.Sequential(
d2l.FlattenLayer(),
nn.Linear(num_inputs, num_hiddens),
nn.ReLU(),
nn.Linear(num_hiddens, num_outputs),
)
for params in net.parameters():
init.normal_(params, mean=0, std=0.01)
这里模型的训练步骤与之前训练softmax回归几乎相同。
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.5)
num_epochs = 5
d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)
输出:
epoch 1, loss 0.0030, train acc 0.712, test acc 0.744
epoch 2, loss 0.0019, train acc 0.823, test acc 0.821
epoch 3, loss 0.0017, train acc 0.844, test acc 0.842
epoch 4, loss 0.0015, train acc 0.856, test acc 0.842
epoch 5, loss 0.0014, train acc 0.864, test acc 0.818
import torch from torch import nn from torch.nn import init import numpy as np import sys import d2lzh_pytorch as d2l num_inputs, num_outputs, num_hiddens = 784, 10, 256 # 定义模型 net = nn.Sequential( d2l.FlattenLayer(), nn.Linear(num_inputs, num_hiddens), nn.ReLU(), nn.Linear(num_hiddens, num_outputs), ) # 初始化参数 for params in net.parameters(): init.normal_(params, mean=0, std=0.01) # 加载数据 batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) loss = torch.nn.CrossEntropyLoss() # 定义优化函数 optimizer = torch.optim.SGD(net.parameters(), lr=0.5) # 训练 num_epochs = 5 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, None, None, optimizer)
如果内容对你有帮助,感谢点赞+关注哦!
关注下方GZH,可获取更多干货内容~欢迎共同学习交流
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。