在pytorch 框架下加载MNIST手写数字数据集,并构建只有一个全连接层和softmax层的网络来进行训练,损失函数为MSE,优化器为SGD
时间: 2024-03-04 16:51:06 浏览: 104
首先,我们需要导入必要的库:
```
import torch
from torch import nn
from torch import optim
from torchvision import datasets, transforms
```
然后,我们可以加载MNIST数据集并进行预处理:
```
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
```
接下来,我们可以定义一个只有一个全连接层和softmax层的网络:
```
class Network(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(784, 10)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
x = x.view(x.shape[0], -1)
x = self.fc(x)
x = self.softmax(x)
return x
model = Network()
```
接下来,我们可以定义MSE损失函数和SGD优化器:
```
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
```
最后,我们可以开始训练模型:
```
epochs = 10
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
optimizer.zero_grad()
output = model(images)
loss = criterion(output, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
print(f"Training loss: {running_loss/len(trainloader)}")
```
在每个epoch中,我们循环遍历训练数据集,并在每个batch上计算MSE损失并进行反向传播和权重更新。最后,我们打印出每个epoch的平均损失。
阅读全文