import torch import torch.nn as nn import torch.optim as optim import torchvision # datasets and pretrained neural nets import torch.utils.data import torch.nn.functional as F import torchvision.transforms as transforms transform = transforms.Compose([transforms.ToTensor(), ]) trainset = torchvision.datasets.MNIST(root='.\mydata', train=True, download=True, transform=transform) #文件夹地址可自定义 testset = torchvision.datasets.MNIST(root='.\mydata', train=False, download=True, transform=transform) #文件夹地址可自定义 trainloader = torch.utils.data.DataLoader(trainset, batch_size =32, shuffle=True) testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)写一个构建网络定义损失函数,优化器训练 迭代次数为10次 模型评测尝试将数据集更改为FashionMNIST或者CIFIR10再训练模型,或对比卷积神经网络和全连接神经网络模型的预测准确度,或尝试数据增强方法的代码
时间: 2024-02-10 22:13:01 浏览: 175
好的,这里是一个简单的代码示例,包括构建网络、定义损失函数、优化器、训练和评估模型的步骤。同时,也包括了针对FashionMNIST数据集和CIFAR10数据集的代码,以及卷积神经网络和全连接神经网络模型的比较,以及数据增强方法的代码。
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torch.utils.data
import torch.nn.functional as F
import torchvision.transforms as transforms
# 数据集选项
dataset = 'MNIST' # 可以选择'MNIST', 'FashionMNIST'或者'CIFAR10'
data_root = './data' # 数据集存储的文件夹地址
# 构建网络
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28 * 28)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# 定义损失函数和优化器
net = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# 加载数据集
if dataset == 'MNIST':
trainset = torchvision.datasets.MNIST(root=data_root, train=True, download=True, transform=transforms.ToTensor())
testset = torchvision.datasets.MNIST(root=data_root, train=False, download=True, transform=transforms.ToTensor())
elif dataset == 'FashionMNIST':
trainset = torchvision.datasets.FashionMNIST(root=data_root, train=True, download=True, transform=transforms.ToTensor())
testset = torchvision.datasets.FashionMNIST(root=data_root, train=False, download=True, transform=transforms.ToTensor())
elif dataset == 'CIFAR10':
trainset = torchvision.datasets.CIFAR10(root=data_root, train=True, download=True, transform=transforms.ToTensor())
testset = torchvision.datasets.CIFAR10(root=data_root, train=False, download=True, transform=transforms.ToTensor())
else:
raise ValueError('Invalid dataset name')
trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)
testloader = torch.utils.data.DataLoader(testset, batch_size=32, shuffle=False)
# 训练模型
for epoch in range(10):
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 100 == 99:
print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 100))
running_loss = 0.0
print('Finished Training')
# 评估模型
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))
```
针对FashionMNIST数据集和CIFAR10数据集的代码仅需要改变数据集的名称和地址即可。卷积神经网络和全连接神经网络模型的比较可以使用以下代码:
```python
# 定义卷积神经网络
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, padding=2)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5, padding=2)
self.fc1 = nn.Linear(32 * 7 * 7, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 32 * 7 * 7)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
# 定义全连接神经网络
class FCNet(nn.Module):
def __init__(self):
super(FCNet, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28 * 28)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
if dataset == 'MNIST' or dataset == 'FashionMNIST':
net = FCNet()
elif dataset == 'CIFAR10':
net = ConvNet()
else:
raise ValueError('Invalid dataset name')
# 训练和评估模型的代码和之前相同
```
最后,以下是数据增强的代码示例,可以在训练数据集上应用随机的图像变换,从而增加数据集的多样性和泛化性能。
```python
# 数据增强
train_transform = transforms.Compose([
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
trainset = torchvision.datasets.MNIST(root=data_root, train=True, download=True, transform=train_transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)
# 训练模型的代码和之前相同
```
需要注意的是,在评估模型时,不应该使用数据增强,因为这会导致测试结果不准确。
阅读全文