卡在 Collab RuntimeError: Expected all tensors to be on the same device, but found at least two device
Posted
技术标签:
【中文标题】卡在 Collab RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:【英文标题】:Stuck at Collab RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda: 【发布时间】:2021-12-24 09:31:23 【问题描述】:我检查并看到我已经在 GPU (context_vector) 中定义了我的张量,但是当我在 Colab 上运行时,它总是显示错误“预期所有张量都在同一设备上,但发现至少两个设备、cpu 和 cuda" 错误轨迹是这样的:
RuntimeError Traceback (most recent call last)
<ipython-input-16-bf7edd8e474b> in <module>()
85 for context, target in data:
86 context_vector = make_context_vector(context)
---> 87 log_probs = model(context_vector)
88 total_loss += loss_function(log_probs, torch.tensor([target]).to(device))
89 #optimize at the end of each epoch
这是我的 make_context_vector 函数:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def make_context_vector(context):
return torch.tensor(context, dtype=torch.long).to(device)
这是我的转发功能:
def forward(self, inputs):
embeds = sum(self.embeddings(inputs)).view(1,-1)
# print('embeds size: '.format(embeds.shape))
out = self.linear1(embeds)
out = self.activation_function1(out)
# print('out1 size: '.format(out.shape))
out = self.linear2(out)
out = self.activation_function2(out)
return out
#TRAINING
for epoch in range(50):
total_loss = 0
for context, target in data:
context_vector = make_context_vector(context)
log_probs = model(context_vector)
total_loss += loss_function(log_probs, torch.tensor([target], device = device))
#optimize at the end of each epoch
optimizer.zero_grad()
total_loss.backward()
optimizer.step()
请大家帮忙! 谢谢
【问题讨论】:
【参考方案1】:哦,我的朋友支持我我发现不仅要为输入设置GPU,还需要为模型设置GPU。 因此,在训练代码中,我将其更改为:
model = CBOW(vocab_size, embedding_dim)**.to(device)**
然后,它运行良好
【讨论】:
以上是关于卡在 Collab RuntimeError: Expected all tensors to be on the same device, but found at least two device的主要内容,如果未能解决你的问题,请参考以下文章
隧道未找到错误 Pyngrok streamlit collab
未找到隧道错误 Pyngrok streamlit collab
从 PC 的控制台使用 ssh 连接到 google collab