首页 文章

当我们有model.to('CUDA')时为什么我们需要image.to('CUDA')

提问于
浏览
0

我正在 PyTorch 学习一门课程 . 而且我想知道为什么我们需要分别告诉 torch.utils.data.DataLoader 输出它正在运行的设备 . 如果模型已经在 CUDA 上,为什么它不会相应地自动更改输入?这个模式对我来说很有趣:

model.to(device)

for ii, (inputs, labels) in enumerate(trainloader):

    # Move input and label tensors to the GPU
    inputs, labels = inputs.to(device), labels.to(device)

是否有一个用例,我想在GPU上运行模型,但我的输入是在CPU模式下,反之亦然?

1 回答

  • 2

    当您调用 model.to(device) (假设 device 是GPU)时,您的模型参数将移动到您的GPU . 关于你的评论:然后他们从CPU内存转移到GPU内存 .

    默认情况下,如果没有另外指定,则在CPU上创建新创建的张量 . 所以这也适用于 inputslabels .

    这里的问题是 same device operands 的操作需要在 same device 上!如果省略 to 并使用CPU张量作为输入,则会收到错误消息 .

    这是一个简短的例子:

    import torch
    
    # device will be 'cuda' if a GPU is available
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    # creating a CPU tensor
    cpu_tensor = torch.rand(10)
    # moving same tensor to GPU
    gpu_tensor = cpu_tensor.to(device)
    
    print(cpu_tensor, cpu_tensor.dtype, type(cpu_tensor), cpu_tensor.type())
    print(gpu_tensor, gpu_tensor.dtype, type(gpu_tensor), gpu_tensor.type())
    
    print(cpu_tensor*gpu_tensor)
    

    输出:

    tensor([0.8571, 0.9171, 0.6626, 0.8086, 0.6440, 0.3682, 0.9920, 0.4298, 0.0172,
            0.1619]) torch.float32 <class 'torch.Tensor'> torch.FloatTensor
    tensor([0.8571, 0.9171, 0.6626, 0.8086, 0.6440, 0.3682, 0.9920, 0.4298, 0.0172,
            0.1619], device='cuda:0') torch.float32 <class 'torch.Tensor'> torch.cuda.FloatTensor
    ---------------------------------------------------------------------------
    RuntimeError                              Traceback (most recent call last)
    <ipython-input-15-ac794171c178> in <module>()
         12 print(gpu_tensor, gpu_tensor.dtype, type(gpu_tensor), gpu_tensor.type())
         13 
    ---> 14 print(cpu_tensor*gpu_tensor)
    
    RuntimeError: Expected object of type torch.FloatTensor but found type torch.cuda.FloatTensor for argument #2 'other'
    

相关问题