当前位置:   article > 正文

torch.Tensor.new_tensor(data):用于创建与data具有相同内容且与torch.Tensor有相同的dtype和device的tensor

new_tensor
  • Python中,有时需要进行其他类型type的数据与Tensor进行转换,例如ndarray转换为Tensor,同时希望Tensor具有与其他的Tensor相同的dtype和device时,可采用torch.Tensor.new_tensor
Tensor.new_tensor(data, *, dtype=None, device=None, requires_grad=False)
'''
1)data:输出的tensor具有与data相同的内容
2)dtype:指定输出tensor的dtype,默认为Tensor的dtype
3)device:指定输出tensor的device,默认为Tensor的device
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 例如:
tensor = torch.ones((2,), dtype=torch.int8)
data = [[0, 1], [2, 3]]
tensor.new_tensor(data)
# tensor([[ 0,  1],
#        [ 2,  3]], dtype=torch.int8)
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/272418
推荐阅读
相关标签
  

闽ICP备14008679号