构建一个全连接网络,网络中只允许存在Linear层(层数不得超过5),可以调节的超参数为learning_rate和epoch(eopch不得超过50)。使用FashionMNlST数据集训练并测试网络的精度
时间: 2024-11-06 10:13:12 浏览: 41
cnn.zip_CNN_appearances5w_cnn、神经网络、_卷积神经网络_神经网络 java
构建一个简单的全连接神经网络(也称为多层感知机MLP)用于Fashion MNIST数据集,首先需要确保满足以下几个步骤:
1. 导入所需库:`torch`, `torch.nn` (用于创建模型),`torch.optim` (优化器) 和 `torch.utils.data` (数据处理)。
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
```
2. 定义网络结构:创建一个最多包含5层线性层的网络,每一层后面通常会跟一个激活函数如ReLU。这里假设最后一层不带激活函数,因为对于分类任务,通常是Sigmoid或Softmax。
```python
class LinearModel(nn.Module):
def __init__(self, input_size, hidden_layers=(64, 64), output_size=10):
super(LinearModel, self).__init__()
layers = [nn.Linear(input_size, hidden_layers[0])]
for i in range(len(hidden_layers) - 1):
layers.append(nn.Linear(hidden_layers[i], hidden_layers[i+1]))
layers.append(nn.Linear(hidden_layers[-1], output_size))
self.layers = nn.Sequential(*layers)
def forward(self, x):
return self.layers(x)
```
3. 设置超参数:学习率 (`learning_rate`) 和最大迭代次数 (`epochs`)。
```python
input_size = 784 # FashionMNIST每张图片的像素数
hidden_layers = (64, 64) # 假设隐藏层节点数
output_size = 10 # 类别数
learning_rate = 0.001 # 学习率
epochs = 50 # 最大训练轮数
model = LinearModel(input_size, hidden_layers, output_size)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss() # 交叉熵损失函数,适用于多类别分类
```
4. 准备数据:加载FashionMNIST数据,并进行必要的预处理。
```python
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)
```
5. 训练和测试过程:
```python
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 使用GPU如果可用
for epoch in range(1, epochs + 1):
model.train()
running_loss = 0.0
for inputs, labels in train_loader:
inputs, labels = inputs.to(device), labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'Epoch {epoch}/{epochs}, Loss: {running_loss / len(train_loader)}')
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f'Test Accuracy: {accuracy}%')
```
阅读全文