Python Pytorch中的线性重磨问题。未实现错误
我在pytorch训练逻辑回归时遇到问题。 我想使用CIFAR10数据集,但我无法进行训练循环,因为当我可以执行Linnear函数时,我收到了一个NotImplementedError 我可能有不止一个我没有看到的错误,因为正如我所说的,我正在学习 我把密码留在这里了Python Pytorch中的线性重磨问题。未实现错误,python,machine-learning,pytorch,Python,Machine Learning,Pytorch,我在pytorch训练逻辑回归时遇到问题。 我想使用CIFAR10数据集,但我无法进行训练循环,因为当我可以执行Linnear函数时,我收到了一个NotImplementedError 我可能有不止一个我没有看到的错误,因为正如我所说的,我正在学习 我把密码留在这里了 import numpy as np import matplotlib.pyplot as plt import torch from torchvision import datasets, transforms import
import numpy as np
import matplotlib.pyplot as plt
import torch
from torchvision import datasets, transforms
import torch.nn.functional as F
from tqdm import tqdm
import torch.nn as nn
#IMPORTING DATA
datatest = mnist_train = datasets.CIFAR10(root="./datasets",
train=True,
transform=transforms.ToTensor(),
download=True)
datatrain = datasets.CIFAR10(root="./datasets",
train=False,
transform=transforms.ToTensor(),
download=True)
print (f'Number of CIFAR test examples {len(datatest)}')
print (f'Number of CIFAR train examples {len(datatest)}')
train_loader = torch.utils.data.DataLoader(datatrain, batch_size=100, shuffle=True)
test_loader = torch.utils.data.DataLoader(datatest, batch_size=100, shuffle=False)
data_train_iter = iter(train_loader)
images, labels = data_train_iter.next()
print("Shape of the minibatch of images: {}".format(images.shape))
print("Shape of the minibatch of labels: {}".format(labels.shape))
#n_samples, n_features = images.shape, labels.shape
#print(n_samples, n_features)
#MODEL
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = nn.Linear(3072, 10)
def foward(self, x):
return self.linear(x)
#Inicializate model
model = Model()
#Criterion
criterion= nn.CrossEntropyLoss()
#Optimizer
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(),
lr=learning_rate)
# Iterate through train set minibatchs
for images, labels in tqdm(train_loader):
# Zero out the gradients
optimizer.zero_grad()
# Forward pass
x = images.view(-1, 32*32*3)
y = model(x)
loss = criterion(y, labels)
loss.backward()
optimizer.step()
## Testing
correct = 0
total = len(datatest)
with torch.no_grad():
# Iterate through test set minibatchs
for images, labels in tqdm(test_loader):
# Forward pass
x = images.view(-1, 32*32*3)
y = model(x)
predictions = torch.argmax(y, dim=1)
correct += torch.sum((predictions == labels).float())
print('Test accuracy: {}'.format(correct/total))
谢谢 这是由于
模型
类中的向前
拼写错误造成的。您已将其写成foward
。请用英语纠正拼写错误
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.linear = nn.Linear(3072, 10)
def forward(self, x): # You have written it as `foward`
return self.linear(x)
请包含错误的全文!