花书学习Day1:实现softmax回归 大部分借鉴了花书的代码使用的数据集是FashionMNIST其图像为28*28像素为方便学习与理解将其展平为28*28784由于数据集有十类因此设置为10个输出。import torch import torch.optim as optim from torch import nn if __name__ __main__: batch_size 256 device torch.device(cuda if torch.cuda.is_available() else cpu) train_iter, test_iter load_data_fashion_mnist(batch_size) net nn.Sequential(nn.Flatten(), nn.Linear(784, 10)) def init_weights(m): if isinstance(m, nn.Linear): nn.init.normal_(m.weight, mean0, std0.01) net.apply(init_weights) loss nn.CrossEntropyLoss() optimizer optim.SGD(net.parameters(), lr0.1) num_epochs 10 net.to(device) loss.to(device) for epoch in range(num_epochs): for X, y in train_iter: X X.to(device) y y.to(device) l loss(net(X).to(device), y) optimizer.zero_grad() l.backward() optimizer.step() X, y next(iter(test_iter)) X X.to(device) y y.to(device) with torch.no_grad(): net.eval() probs torch.softmax(net(X).to(device), dim1) print(accuracy(probs, y) / len(y))在写代码时发现似乎代码中没有显式地使用softmax层进行分类因此询问DeepSeek了解到nn.CrossEntropyLoss()实际内部会自动地隐式执行softmax运算等价于先对net(X)应用LogSoftmax再计算负对数似然损失NLLLoss因此上述net和loss可换为netnn.Sequential(nn.Flatten(), nn.Linear(784, 10), nn.Softmax(dim1)) l nn.NLLLoss()上述代码中读取数据集的代码和计算准确度的代码如下所示def accuracy(y_hat, y): if len(y_hat.shape) 1 and y_hat.shape[1] 1: y_hat y_hat.argmax(axis1) cmp y_hat.type(y.dtype) y return float(cmp.type(y.dtype).sum()) def load_data_fashion_mnist(batch_size, resizeFalse): #save trans[transforms.ToTensor()] if resize: trans.insert(0, transforms.Resize(resize)) trans transforms.Compose(trans) mnist_train torchvision.datasets.FashionMNIST(root../data, trainTrue, transformtrans, downloadTrue) mnist_test torchvision.datasets.FashionMNIST(root../data, trainFalse, transformtrans, downloadTrue) return (data.DataLoader(mnist_train, batch_size, shuffleTrue, num_workersget_dataloader_workers()), data.DataLoader(mnist_test, batch_size, shuffleFalse, num_workersget_dataloader_workers()))