【问题标题】:is it possible to remove the dtype from tensor in pytorch?是否可以从 pytorch 的张量中删除 dtype?
【发布时间】:2020-10-21 12:01:03
【问题描述】:
targets-> [{'boxes': tensor([[ 23.7296,  28.9209, 122.0997, 213.2374]], device='cuda:0',
       dtype=torch.float64), 'labels': tensor([1], device='cuda:0'), 'area': tensor([18131.2344], device='cuda:0'), 'iscrowd': tensor([0], device='cuda:0')}]

现在盒子有 dtype=torch.float64 是不是可能看起来像

targets-> [{'boxes': tensor([[ 23.7296,  28.9209, 122.0997, 213.2374]], device='cuda:0',), 'labels': tensor([1], device='cuda:0'), 'area': tensor([18131.2344], device='cuda:0'), 'iscrowd': tensor([0], device='cuda:0')}]

【问题讨论】:

    标签: computer-vision pytorch torch torchvision


    【解决方案1】:

    所有张量都有一个dtype 属性,没有例外。但是,PyTorch 有一个default float dtype,通常是torch.float32(单精度 32 位浮点)。使用此默认 dtype 显示张量时,将其省略。但是,您的 boxes 张量具有 -默认 dtype,torch.float64,因此它正在显示。
    您可以使用 .to() 命令将此张量转换为默认的 torch.float32 dtype,从而使 PyTorch 不显式显示 dtype:

    targets[0]['boxes'] = targets[0]['boxes'].to(dtype=torch.float32)  #.to() in _not_ an in-place operation
    

    这将导致

    In [*]: targets
    Out[*]:
    [{'boxes': tensor([[ 23.7296,  28.9209, 122.0997, 213.2374]], device='cuda:0'),
      'labels': tensor([1], device='cuda:0'),
      'area': tensor([18131.2344], device='cuda:0'),
      'iscrowd': tensor([0], device='cuda:0')}]
    

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 2019-01-10
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多