### CNN Pytorch 错误:输入类型(torch.cuda.ByteTensor)和权重类型(torch.cuda.FloatTensor)应相同

我收到了以下错误,

输入类型(torch.cuda.ByteTensor)和权重类型(torch.cuda.FloatTensor)应相同

以下是我的代码,

device    = torch.device('cuda:0')trainData = torchvision.datasets.FashionMNIST('/content/', train=True, transform=None, target_transform=None, download=True)testData  = torchvision.datasets.FashionMNIST('/content/', train=False, transform=None, target_transform=None, download=True)class Net(nn.Module):  def __init__(self):    super().__init__()    '''    网络结构:    输入 >     (1)Conv2D > (2)MaxPool2D >     (3)Conv2D > (4)MaxPool2D >     (5)Conv2D > (6)MaxPool2D >     (7)Linear > (8)LinearOut    '''    # 创建卷积层    self.conv1 = nn.Conv2d(in_channels=CHANNELS, out_channels=32, kernel_size=3)    self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3)    self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3)    self.flatten = None    # 创建一个随机的虚拟样本来获取扁平化维度    x = torch.randn(CHANNELS, DIM, DIM).view(-1, CHANNELS, DIM, DIM)    x = self.convs(x)    # 创建线性层    self.fc1   = nn.Linear(self.flatten, 512)    self.fc2   = nn.Linear(512, CLASSES)  def convs(self, x):    # 创建最大池化层    x = F.max_pool2d(F.relu(self.conv1(x)), kernel_size=(2, 2))    x = F.max_pool2d(F.relu(self.conv2(x)), kernel_size=(2, 2))    x = F.max_pool2d(F.relu(self.conv3(x)), kernel_size=(2, 2))    if not self.flatten:      self.flatten = x[0].shape[0] * x[0].shape[1] * x[0].shape[2]    return x  # 前向传递  def forward(self, x):    x = self.convs(x)    x = x.view(-1, self.flatten)    sm = F.relu(self.fc1(x))    x = F.softmax(self.fc2(sm), dim=1)    return x, sm  x_train, y_train = training_set  x_train, y_train = x_train.to(device), y_train.to(device)  optimizer = optim.Adam(net.parameters(), lr=LEARNING_RATE)  loss_func = nn.MSELoss()  loss_log  = []  for epoch in range(EPOCHS):    for i in tqdm(range(0, len(x_train), BATCH_SIZE)):        x_batch = x_train[i:i+BATCH_SIZE].view(-1, CHANNELS, DIM, DIM).to(device)        y_batch = y_train[i:i+BATCH_SIZE].to(device)        net.zero_grad()        output, sm = net(x_batch)        loss = loss_func(output, y_batch.float())        loss.backward()        optimizer.step()    loss_log.append(loss)    # print(f"Epoch : {epoch} || Loss : {loss}")  return loss_logtrain_set = (trainData.train_data, trainData.train_labels)test_set  = (testData.test_data, testData.test_labels)EPOCHS        = 5LEARNING_RATE = 0.001BATCH_SIZE    = 32net = Net().to(device)loss_log = train(net, train_set, EPOCHS, LEARNING_RATE, BATCH_SIZE)

这是我收到的错误,

RuntimeError                              Traceback (most recent call last)<ipython-input-8-0db1a1b4e37d> in <module>()      5 net = Net().to(device)      6 ----> 7 loss_log = train(net, train_set, EPOCHS, LEARNING_RATE, BATCH_SIZE)6 frames<ipython-input-6-7de4a78e3736> in train(net, training_set, EPOCHS, LEARNING_RATE, BATCH_SIZE)     13      14         net.zero_grad()---> 15         output, sm = net(x_batch)     16         loss = loss_func(output, y_batch.float())     17         loss.backward()/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)    539             result = self._slow_forward(*input, **kwargs)    540         else:--> 541             result = self.forward(*input, **kwargs)    542         for hook in self._forward_hooks.values():    543             hook_result = hook(self, input, result)<ipython-input-5-4fddc427892a> in forward(self, x)     41   # 前向传递     42   def forward(self, x):---> 43     x = self.convs(x)     44     x = x.view(-1, self.flatten)     45     sm = F.relu(self.fc1(x))<ipython-input-5-4fddc427892a> in convs(self, x)     31      32     # 创建最大池化层---> 33     x = F.max_pool2d(F.relu(self.conv1(x)), kernel_size=(2, 2))     34     x = F.max_pool2d(F.relu(self.conv2(x)), kernel_size=(2, 2))     35     x = F.max_pool2d(F.relu(self.conv3(x)), kernel_size=(2, 2))/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)    539             result = self._slow_forward(*input, **kwargs)    540         else:--> 541             result = self.forward(*input, **kwargs)    542         for hook in self._forward_hooks.values():    543             hook_result = hook(self, input, result)/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)    343     344     def forward(self, input):--> 345         return self.conv2d_forward(input, self.weight)    346     347 class Conv3d(_ConvNd):/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in conv2d_forward(self, input, weight)    340                             _pair(0), self.dilation, self.groups)    341         return F.conv2d(input, weight, self.bias, self.stride,--> 342                         self.padding, self.dilation, self.groups)    343     344     def forward(self, input):RuntimeError: 输入类型(torch.cuda.ByteTensor)和权重类型(torch.cuda.FloatTensor)应相同

我已经双重检查了我的神经网络和输入都已经在GPU上。我仍然收到这个错误,我不明白为什么!

请有人帮我解决这个错误。


回答:

将你的输入 x_batch 转换为浮点数。在通过模型之前使用 x_batch = x_batch.float()

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注