python - 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) )
解决方案
推荐阅读
- c - 为什么 `volatile` 变量的值不会改变?
- python - 在Python2.7的函数中独立执行代码块
- javascript - 页面加载时调用 Javascript onclick 函数
- flutter - Flutter 检查一个类是否包含一个键
- ios - 如何在应用程序中检索小部件大小以根据设备进行预览?
- javascript - 记忆回调函数
- swiftui - 如何处理小部件上的点击手势?
- javascript - nodejs 使用 dbus-daemon 做什么?
- python - 如何在 Python 中对包含 TRUE/FALSE 值的数据集执行聚类?
- python - 为什么 Tkinter 没有垂直填充我的窗口