如何将 ndarray 转换为 GPU 格式的 autograd 变量?

How to convert ndarray to autograd variable in GPU format?

我正在尝试做这样的事情,

data = torch.autograd.Variable(torch.from_numpy(nd_array))

它属于 Variable[torch.FloatTensor] 类型,但我需要 Variable[torch.cuda.FloatTensor] 我也想在 pytorch version 0.3.0 中执行此操作,它缺少像 to(device)set_default_device

您可以使用张量的 cuda() 方法。

如果您想使用特定设备,您可以使用上下文管理器,例如

with torch.cuda.device(device_index):
    t = torch.FloatTensor(1.).cuda()

更多具体信息check documentation for version 0.3.0