如何用pytorch在fashionmnist数据集中使把数据分为训练集,测试集和验证集,在使用dropout方法
时间: 2024-03-14 12:43:49 浏览: 68
首先,我们需要导入PyTorch和FashionMNIST数据集:
```python
import torch
import torchvision
import torchvision.transforms as transforms
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
trainset = torchvision.datasets.FashionMNIST(root='./data', train=True,
download=True, transform=transform)
testset = torchvision.datasets.FashionMNIST(root='./data', train=False,
download=True, transform=transform)
```
接下来,我们可以使用`random_split`方法将训练集分为训练集和验证集:
```python
trainset, valset = torch.utils.data.random_split(trainset, [50000, 10000])
```
然后,我们可以使用`DataLoader`来加载数据集:
```python
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64,
shuffle=True, num_workers=2)
valloader = torch.utils.data.DataLoader(valset, batch_size=64,
shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(testset, batch_size=64,
shuffle=False, num_workers=2)
```
接下来,我们可以定义我们的神经网络模型,并在其中使用`nn.Dropout`方法来使用dropout:
```python
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
self.dropout = nn.Dropout(p=0.5)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
net = Net()
```
最后,我们可以定义我们的优化器和损失函数,并开始训练我们的模型:
```python
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
for epoch in range(10): # 训练网络10个epoch
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()
# 每个epoch之后,在验证集上测试模型
correct = 0
total = 0
with torch.no_grad():
for data in valloader:
images, labels = data
outputs = net(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Epoch %d: Loss: %.3f Validation Accuracy: %.3f' %
(epoch + 1, running_loss / len(trainloader),
100 * correct / total))
print('Finished Training')
```
阅读全文