我从一个教程中构建了一个简单的网络,但出现此错误:
RuntimeError:类型为torch.cuda.FloatTensor的预期对象,但为参数#4'mat1'找到类型为torch.FloatTensor的对象
有什么帮助吗?谢谢!
import torch
import torchvision
device = torch.device("cuda:0")
root = '.data/'
dataset = torchvision.datasets.MNIST(root, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=4)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.out = torch.nn.Linear(28*28, 10)
def forward(self, x):
x = x.view(x.size(0), -1)
x = self.out(x)
return x
net = Net()
net.to(device)
for i, (inputs, labels) in enumerate(dataloader):
inputs.to(device)
out = net(inputs)
Run Code Online (Sandbox Code Playgroud) python machine-learning image-processing deep-learning pytorch