PyTorch 运行时错误:输入类型(双精度)和偏置类型(浮点)应该相同

G.P*_*G.P 2 python conv-neural-network pytorch

当我尝试使用 pytorch 训练 CNN 模型时出现错误这是我创建的模型

该模型

import torch

class NNnet(torch.nn.Module):
    def __init__(self, channels = 19, samples = 1000.0, outputs = 4):
        super(NNnet, self).__init__()
        #Sequential 1
        
        self.seq1 = torch.nn.Sequential(
            torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = (1,20), stride = 1),
            torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = (3,1), stride = 1),
            torch.nn.BatchNorm2d(32, eps = 0.001, momentum = 0.99),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size = [1,5], stride = [1,2])
        )
        #calculate output of sample at each opeartion
        samples = (samples - 20) + 1
        samples = (samples - 1) + 1
        channels = channels - 3 + 1
        samples = floor((samples - 5) / 2 + 1)


        #Sequential 2
        self.seq2 = torch.nn.Sequential(
            torch.nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = (1,20)),
            torch.nn.BatchNorm2d(64, eps = 0.001, momentum = 0.99), #tensorflow duplicate
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size = [1,7], stride = [1,2])
        )
        samples = (samples - 20) + 1
        samples = floor((samples- 7) / 2 + 1)


        #Sequential 3
        self.seq3 = torch.nn.Sequential(
            torch.nn.Conv2d(in_channels = 64, out_channels = 64, kernel_size = (1,10)),
            torch.nn.BatchNorm2d(64, eps = 0.001, momentum = 0.99),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(kernel_size = [1,5], stride = [1,2])
        ) 
        samples = (samples - 10) + 1
        samples = floor((samples - 5) / 2 + 1)


        #fully connect
        self.fc = torch.nn.Sequential(
            torch.nn.Dropout1d(p = 0.5),
            #cal from (initla_ch - 2) * last_layer_fmap * final datapoint from conv 
            torch.nn.Linear(in_features = channels * 64 * samples, out_features = 32),
            torch.nn.BatchNorm1d(32, eps = 0.001, momentum = 0.99),
            torch.nn.ReLU(),
            torch.nn.Dropout1d(p = 0.3),
            torch.nn.Linear(in_features = 32, out_features = outputs),
            torch.nn.Softmax()
        )

    def forward(self, x):
        x = self.seq1(x)
        x = self.seq2(x)
        x = self.seq3(x)
        x = torch.flatten(x, start_dim = 1, end_dim = -1)
        x = self.fc(x)

        return x
Run Code Online (Sandbox Code Playgroud)

训练循环

import torch
import numpy

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

#dummy data of 540 instances, 19 channel and 1000 sample
test_data_x = np.ones(shape = (540,19,1000))
#dummy label
test_data_y = np.ones(shape = (540,4))

train_dat    = torch.utils.data.TensorDataset(torch.tensor(test_data_x).to(device), torch.tensor(test_data_y).to(device))
train_loader = torch.utils.data.DataLoader(train_dat, batch_size = 16, shuffle = True)

test_model = NNnet(channels = 19, samples = 1000, outputs = 4)
# optimizer and the loss function definition 
optimizer = torch.optim.Adam(test_model.parameters(), lr = 0.001, weight_decay = 0.0001)
criterion = torch.nn.CrossEntropyLoss()

#pin to gpu
test_model.to(device)
criterion.to(device)

#train loop-----------------------------------------------
for epoch in range(10):
    running_loss = 0.0
    for i, data in enumerate(train_loader,0):
        inputs, labels = data
        inputs, labels = inputs.to(device), labels.to(device)
        #zero grad
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = model(inputs) #<<< ERROR HERE
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
Run Code Online (Sandbox Code Playgroud)

错误信息(在Google Colab上测试) 输出错误

当我尝试使用以下程序训练模型时,它引发了运行时错误,我有点困惑,因为我已将所有内容都放在 GPU 上,但类型之间仍然存在冲突。而且

模型定义或数据准备是否有错误?

小智 5

floatNumpy 中的默认值为float64,您必须在将 Numpy 张量转换np.float32为 Pytorch 之前将其转换为 。

train_dat    = torch.utils.data.TensorDataset(torch.tensor(test_data_x).to(device), torch.tensor(test_data_y).to(device))
Run Code Online (Sandbox Code Playgroud)