pytorchtensordtype

Convert FloatTensor to HalfTensor in Pytorch?


I am using a pretrained model from transformers which expects input type to be torch.HalfTensor. However, I have input of type torch.FloatTensor. How to convert?


Solution

  • torch.Tensor.half() should do:

    torch.rand(3,3).half()
    # tensor([[0.1456, 0.0580, 0.9277],
    #        [0.3318, 0.1979, 0.6670],
    #        [0.5391, 0.2012, 0.2445]], # dtype=torch.float16)