PyTorch - 04 - 使用PyTorch简洁实现softmax分类器
我的这篇博客:
是从零实现softmax回归,以熟悉PyTorch和相关函数的定义。
现在利用PyTorch来实现softmax分类器, 加深印象。
import torch
from torch import nn
from torchvision.datasets import FashionMNIST
from torchvision.transforms import transforms
from torch.utils.data.dataloader import DataLoader
数据加载
batch_size = 256
root_dir = "./torchvision/data/"
train_set = FashionMNIST(root_dir, train=True, transform=transforms.ToTensor(), download=False)
test_set = FashionMNIST(root_dir, train=False, transform=transforms.ToTensor(), download=False)
train_iter = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=0)
test_iter = DataLoader(test_set, batch_size=batch_size, shuffle=False, num_workers=0)
得到的 train_iter, test_iter 形状为\({R^{n*height*width*channel}=R^{256*28*28*1}}\),n代表batch_size。
定义网络
直接继承 torch.nn.Module
num_inputs, num_outputs = 1*28*28, 10 # 输入:一通道28*28的灰度图像, 输出:10个分类对应十个输出
class linearNet(nn.Module):
def __init__(self, num_inputs, num_outputs):
super(linearNet, self).__init__()
self.linear = nn.Linear(num_inputs, num_outputs)
def forward(self, x):
y = self.linear(x.view(x.shape[0], -1))
return y
net = linearNet(num_inputs, num_outputs)
nn.init.normal_(net.linear.weight, 0, 0.01) #初始化网络参数权值
nn.init.constant_(net.linear.bias, 0.0)
loss = nn.CrossEntropyLoss() #定义交叉熵损失函数, 这个方法集成了softmax计算
optimizer = torch.optim.SGD(net.parameters(), lr=0.1) #优化器
训练以及测试模型
# 测试集数据评估
def evaluate_accuracy(data_iter, net):
acc_sum, n = 0.0, 0
for X, y in data_iter:
acc_sum += (net(X).argmax(dim=1) == y).float().sum().item()
n += y.shape[0]
return acc_sum / n
epochs = 5
def train_model(optimizer):
for epoch in range(epochs):
train_loss_sum, train_acc_sum, n = 0.0, 0.0, 0
for inputs, y in train_iter:
y_hat = net(inputs)
cost = loss(y_hat, y).sum()
optimizer.zero_grad()
cost.backward()
optimizer.step()
train_loss_sum += cost.item()
train_acc_sum += (torch.argmax(y_hat, dim=1) == y).float().sum().item()
n += y.shape[0]
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.4f, test acc %.4f' % (epoch + 1, train_loss_sum / n, train_acc_sum / n, test_acc))
train_model(optimizer)
epoch 1, loss 0.0031, train acc 0.7466, test acc 0.7916
epoch 2, loss 0.0022, train acc 0.8134, test acc 0.8001
epoch 3, loss 0.0021, train acc 0.8257, test acc 0.8193
epoch 4, loss 0.0020, train acc 0.8332, test acc 0.8049
epoch 5, loss 0.0019, train acc 0.8364, test acc 0.8254
希望能一辈子学习,希望能学习一辈子。
(^ _ ^)