Я пытался реализовать простое NN с использованием наборов данных MNIST, и я продолжаю получать эту ошибку import matplotlib.pyplot как plt
import torch
from torchvision import models
from torchvision import datasets, transforms
from torch import nn, optim
import torch.nn.functional as F
import helper
transform = transforms.ToTensor()
train_data = datasets.MNIST(root='data', train=True,
download=True, transform=transform)
test_data = datasets.MNIST(root='data', train=False,
download=True, transform=transform)
train_loader = torch.utils.data.DataLoader(train_data, batch_size = 20, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size = 20, shuffle=True)
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.fc1 = nn.Linear(784,10)
def forward(self,x):
x = x.view(-1,784)
x = F.relu(self.fc1(x))
x = F.log_softmax(x, dim = 1)
return x
model = Net()
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr = 0.003)
epochs = 20
model.train()
for epoch in range(epochs):
train_loss = 0
for image, lables in train_data:
optimizer.zero_grad()
output = model(image)
loss = criterion(output, lables)
loss.backwards()
optimizer.step()
train_loss += loss.item()*image.size(0)
train_loss = train_loss/len(train_data.dataset)
print('Epoch: {} \tTraining Loss: {:.6f}'.format(epoch+1, train_loss))
Вот ошибка RuntimeError: измерение указано как 0, но тензор не имеет измерений