Question: The error came out when I try to train the CNN model using pytorch This is the model I create The model 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  Training loop 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()  Error message (test on Google Colab) output error When I try to train the model using the following program, It raised the runtime error I kinda confuse because I have put everything on the GPU but still there's a conflict between type. Moreover Is there a mistake on model definition or on the data preparation?

Answer: Default float in Numpy is float64, you must convert the Numpy tensor to np.float32 before converting it to Pytorch. train_dat = torch.utils.data.TensorDataset(torch.tensor(test_data_x).to(device), torch.tensor(test_data_y).to(device))