Pytorch 實現多層網絡
參考學習花書多層感知機的簡潔實現
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("..")
import d2lzh_pytorch as d2l
# 1 定義模型
num_inputs, num_outputs, num_hiddens = 784, 10, 256 #輸入個數爲784(Fashion-MNIST數據集中圖像),輸出個數爲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)
# 2 讀取數據並訓練模型
batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) #使用Fashion-MNIST數據集
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)
- output:
epoch 1, loss 0.0020, train acc 0.850, test acc 0.935
epoch 2, loss 0.0008, train acc 0.942, test acc 0.943
epoch 3, loss 0.0006, train acc 0.958, test acc 0.961
epoch 4, loss 0.0004, train acc 0.967, test acc 0.969
epoch 5, loss 0.0004, train acc 0.974, test acc 0.968