利用torch.nn实现softmax回归Fashion-MNIST数据集上进行训练和测试:
(1)(2)(3)同上
(4)构建模型
- num_inputs = 784
- num_outputs = 10 # 共10类
-
- # 构建模型
- class softmaxnet(torch.nn.Module):
- def __init__(self, n_features, n_labels):
- super(softmaxnet, self).__init__()
- self.linear = torch.nn.Linear(n_features, n_labels)
-
- def softmax(self, X): # softmax计算
- X_exp = X.exp() # 对每个元素做指数运算
- partition = X_exp.sum(dim=1, keepdim=True) # 求列和,即对同行元素求和 n*1
- return X_exp / partition # broadcast
-
- def forward(self, x):
- x_ = x.view((-1, num_inputs))
- y_ = self.linear(x_)
- y_hat = self.softmax(y_)
- return y_hat
(5)损失函数和优化算法
- #损失函数和优化方法
- net = softmaxnet(num_inputs, num_outputs)
- lr = 0.3
- loss = torch.nn.CrossEntropyLoss()
- optimizer = optim.SGD(net.parameters(), lr=lr)
(6)构建测试集准确率函数
- #测试集的准确度与损失
- def get_test_info(data_iter, net):
- right_count, all_count = 0.0, 0
- for x, y in data_iter:
- y_ = net(x)
- l = loss(y_, y)
- right_count += (y_.argmax(dim=1)==y).sum().item()
- all_count += y.shape[0]
- return right_count/all_count, l.item()
(7)开始优化并分别输出训练集和测试集的损失和准确率
- num_epoch = 20
-
- for epoch in range(num_epoch):
- train_r_num, train_all_num = 0.0, 0
- for X, y in train_iter:
- y_ = net(X)
- l = loss(y_, y)
- l.backward()
- optimizer.step()
- optimizer.zero_grad()
- train_r_num += (y_.argmax(dim=1) == y).sum().item()
- train_all_num += y.shape[0]
- test_acc, test_ave_loss = get_test_info(test_iter, net)
- print('epoch %d, train loss %.4f, train acc %.3f' % (epoch+1, l.item(), train_r_num/train_all_num))
- print(' test loss %.4f, test acc %.3f' % (test_ave_loss, test_acc))