- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 pytorch 中训练 LSTM 层。我使用 4 个 GPU。初始化时,我添加了 .cuda() 函数将隐藏层移动到 GPU。但是当我使用多个 GPU 运行代码时,我收到此运行时错误:
RuntimeError: Input and hidden tensors are not at the same device
我尝试通过在转发函数中使用 .cuda() 函数来解决问题,如下所示:
self.hidden = (self.hidden[0].type(torch.FloatTensor).cuda(), self.hidden[1].type(torch.FloatTensor).cuda())
这行代码似乎解决了问题,但它引起了我的担忧,即是否在不同的 GPU 中看到更新的隐藏层。我是否应该在批量的前向函数结束时将向量移回CPU,或者是否有其他方法来解决该问题。
最佳答案
当您在张量上调用 .cuda()
时,Pytorch 将其移动到 current GPU device默认情况下(GPU-0)。因此,由于数据并行性,您的数据位于不同的 GPU 中,而您的模型则位于另一个 GPU 中,这会导致您面临运行时错误。
循环神经网络实现数据并行的正确方法如下:
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
class MyModule(nn.Module):
# ... __init__, other methods, etc.
# padded_input is of shape [B x T x *] (batch_first mode) and contains
# the sequences sorted by lengths
# B is the batch size
# T is max sequence length
def forward(self, padded_input, input_lengths):
total_length = padded_input.size(1) # get the max sequence length
packed_input = pack_padded_sequence(padded_input, input_lengths,
batch_first=True)
packed_output, _ = self.my_lstm(packed_input)
output, _ = pad_packed_sequence(packed_output, batch_first=True,
total_length=total_length)
return output
m = MyModule().cuda()
dp_m = nn.DataParallel(m)
您还需要为多 GPU 设置相应地设置 CUDA_VISIBLE_DEVICES
环境变量。
引用文献:
关于python - 使用多个 GPU 运行 LSTM 得到 "Input and hidden tensors are not at the same device",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54511769/
我试图将迁移学习应用于 InceptionV3。这是我的代码: inception_model = InceptionV3(weights='imagenet',include_top=False)
我正在尝试展示 GAN 网络在某些指定时期的结果。打印当前结果的功能以前与 TF 一起使用。我需要换成 pytorch。 def show_result(G_net, z_, num_epoch, s
我对孪生神经网络还很陌生,最近发现了 this example和 Colab notebook . 运行代码时出现以下错误: IndexError: invalid index of a 0-dim
我正在尝试使用在此 PR 中添加的“高级”、numpy 样式的切片,但是我遇到了 same issue as the user here : ValueError: Shape must be ran
我想在 TensorFlow 中做类似这段 Numpy 代码的事情: a = np.zeros([5, 2]) idx = np.random.randint(0, 2, (5,)) row_idx
我有以下特征张量: Eigen::Tensor m(3,10,10); 我想访问第一个矩阵。在 numpy 中我会这样做 m(0,:,:) 我如何在 Eigen 中做到这一点 最佳答案 您可以使用 .
1、问题 模型训练完后进行测试,报错 RuntimeError: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我正在对 TFRecords 进行配对,它为我提供了一个标签作为数值。但是我需要在读取原始记录时将此值转换为分类向量。我怎样才能做到这一点。这是读取原型(prototype)记录的代码片段: def
我应该如何从 Eigen::Tensor 创建一个 tensorflow::Tensor?我可以一个接一个地复制元素,但我希望有更好的方法。 最佳答案 没有公共(public) api 可以在不复制数
我正在尝试使用 Tensorflow(版本 0.9.0)以与 beginner's tutorial 非常相似的方式训练一个简单的二元逻辑回归分类器。并且在拟合模型时遇到以下错误: ValueErro
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor 有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么? 最佳答案 在 PyTorch 中,torc
PyTorch0.4中,.data 仍保留,但建议使用 .detach(), 区别在于 .data 返回和 x 的相同数据 tensor, 但不会加入到x的计算历史里,且require s_grad
我有一个参差不齐的张量,在尝试创建模型并使用 model.fit() 时,出现错误:TypeError: Failed to convert object of type to Tensor. Co
我必须用生成器和判别器训练一个 GAN 网络。我的发电机网络如下。 def Generator(image_shape=(512,512,3): inputs = Input(image_shap
我正在使用 Flask 运行 Web 服务器,当我尝试使用 vgg16 时出现错误,vgg16 是 keras 的预训练 VGG16 模型的全局变量。我不知道为什么会出现这个错误,也不知道它是否与 T
我正在使用 keras 的预训练模型,并且在调用 ResNet50(weights='imagenet') 时出现错误。 我在 flask 服务器中有以下代码: def getVGG16Predict
执行以下代码时出现以下错误。 rnn.rnn() 返回张量列表。错误在 convert_to_tensor 行。 TypeError: List of Tensors when single Tens
我有一个fruit_train_net.py 文件,其中包含以下代码 import tensorflow as tf import numpy as np import time import os
我们可以使用 torch.Tensor([1., 2.], device='cuda') 在 GPU 上分配张量.使用这种方式而不是torch.cuda.Tensor([1., 2.])有什么不同吗?
我是一名优秀的程序员,十分优秀!