首页 > 解决方案 > RuntimeError: 参数 #3 'mat2' 的张量在 CPU 上,但预计它在 GPU 上(同时检查 addmm 的参数)

问题描述

我有一个使用 PyTorch 的自定义编写模型。但是,当我尝试使用.to(device)GPU 进行训练时,我遇到了这个错误。该模型确实在 CPU 上运行,但速度很慢。谁能给我一些关于如何解决这个问题的见解?

我也将输入添加到 GPU 中,但仍然收到此错误。添加的第二张图片显示了错误的初始部分和我在错误的最后一个谎言中提到的标题。 模型火车循环

错误初始部分

def build_network(dim_val, dim_attn, input_size, dec_seq_len, output_sequence_length, n_decoder_layers, n_encoder_layers, n_heads):
# Input Size = 1
    t = Transformer(dim_val, dim_attn, input_size ,dec_seq_len,  output_sequence_length, n_decoder_layers, n_encoder_layers, n_heads)
    return t


def build_optimizer(model ,learning_rate):
    optimizer = torch.optim.Adam(model.parameters(), learning_rate)
    return optimizer

初始化网络

t = build_network(2, 2, 1, 2, 2, 2, 2, 2).to(device)

初始化优化器

optimizer = build_optimizer(t, 0.001)

MAPE公式

def mape(actual, pred): 
actual, pred = np.array(actual), np.array(pred)
return round(np.mean(np.abs((actual - pred) / actual)) * 100,2)

for e in range(2):
   start_time = time.time()

   # Batch Train Loss
   batch_losses = []
   batch_mape = []

   # Batch Test Loss
   val_batch_loss = []
   val_batch_mape = []

################################################ Train Batch Loop ################################################

   for test_x, test_y in train_loader:  
      X = test_x.to(device)
      Y = test_y.to(device)

      # Zero Grad at start of loop
      optimizer.zero_grad()
      # Forward pass and calculate loss
      net_out = t(X)
      ## Loss MSE
      loss = torch.mean((net_out - Y) ** 2)
      # Train MAPE
      train_mape = mape(np.abs((Y.detach().numpy())), 
      net_out.detach().numpy())
      # Backwards pass
      loss.backward()
      optimizer.step()

      # Append the Batch Loss
      batch_losses.append(loss.item())
      # Append the Batch MAPE
      batch_mape.append(train_mape)

################################################ Validation Batch Loop ################################################

   for test_x, test_y in val_loader:  
      X_Test = test_x.to(device)
      Y_Test = test_y.to(device)

      #test_values = torch.from_numpy(Transformer_Xtest)
      val = t(X_Test)

      # Val Loss
      validation_loss = torch.mean((val - Y_Test) ** 2)
      # Append the Batch Loss
      val_batch_loss.append(validation_loss.item())

      # Val MAPE
      validation_mape = mape(np.abs((Y_Test.detach().numpy())), 
      val.detach().numpy())
      # Append the Batch MAPE
      val_batch_mape.append(validation_mape)

   ######## Train Epoch Summary
   epoch_train_loss = round(np.mean(batch_losses), 5)
   epoch_train_mape = round(np.mean(batch_mape), 5)

   ######## Val Epoch Summary
   epoch_val_loss = round(np.mean(val_batch_loss), 5)
   epoch_val_mape = round(np.mean(val_batch_mape), 5)

   show_print = print("Epoch: ", e+1, "epoch_train_loss:", epoch_train_loss, "epoch_train_mape: ", epoch_train_mape, "epoch_val_loss:", epoch_val_loss, "epoch_val_mape:", epoch_val_mape,"--- Elapsed Time Seconds ---", round((time.time() - start_time),  3) )  

标签: pythondeep-learningpytorchtime-series

解决方案


推荐阅读