SOFTMAX简单实现
import torch
from torch import nn
from torch.nn import init
import numpy as np
import sys
sys.path.append("../3.5 图像分类数据集(FASHION-MNIST)")
import d2lzh_pytorch as d2l
batch_size=256
train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size)
'''
在3.4节(softmax回归)中提到,softmax回归的输出层是⼀个全连接层,所以我们⽤⼀个线性模块就
可以了。因为前⾯我们数据返回的每个batch样本 x 的形状为(batch_size, 1, 28, 28), 所以我们要先
⽤ view() 将 x 的形状转换成(batch_size, 784)才送⼊全连接层'''
num_inputs=784
num_outputs=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)
init.normal_(net.linear.weight,mean=0,std=0.01)
init.constant_(net.linear.bias,val=0)
'''
如果做了上⼀节的练习,那么你可能意识到了分开定义softmax运算和交叉熵损失函数可能会造成数值
不稳定。因此,PyTorch提供了⼀个包括softmax运算和交叉熵损失计算的函数。它的数值稳定性更
好'''
loss=nn.CrossEntropyLoss()
optimizer=torch.optim.SGD(net.parameters(),lr=0.1)
num_epochs=5
d2l.train_ch3(net,train_iter,test_iter,loss,num_epochs,batch_size,None,None,optimizer)
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
def train_ch3(net, train_iter, test_iter,
loss, num_epochs,batch_size,
params=None, lr=None, optimizer=None):
for epoch in range(num_epochs):
train_l_sum, train_acc_sum, n = 0.0, 0.0, 0
for X, y in train_iter:
y_hat = net(X)
l = loss(y_hat, y).sum()
if optimizer is not None:
optimizer.zero_grad()
elif params is not None and params[0].grad is not None:
for param in params:
param.grad.data.zero_()
l.backward()
if optimizer is None:
d2l.sgd(params, lr, batch_size)
else:
optimizer.step()
train_l_sum += l.item()
train_acc_sum += (y_hat.argmax(dim=1) ==y).sum().item()
n += y.shape[0]
test_acc = evaluate_accuracy(test_iter, net)
print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f'% (epoch +1,train_l_sum / n, train_acc_sum / n,test_acc))