【问题标题】:How to convert ndarray to autograd variable in GPU format?如何将 ndarray 转换为 GPU 格式的 autograd 变量?
【发布时间】:2019-08-19 05:32:14
【问题描述】:

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

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 这样的一些方法

【问题讨论】:

    标签: python numpy gpu pytorch autograd


    【解决方案1】:

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

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

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

    更多具体信息check documentation for version 0.3.0

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2021-02-24
      • 1970-01-01
      • 2013-05-04
      • 1970-01-01
      • 2013-08-14
      相关资源
      最近更新 更多