pytorch tensor data type conversion

 Tensor data type contains a total of data

class DoubleTensor(Tensor): ...
class FloatTensor(Tensor): ...
class LongTensor(Tensor): ...
class IntTensor(Tensor): ...
class ShortTensor(Tensor): ...
class CharTensor(Tensor): ...
class ByteTensor(Tensor): ...
class BoolTensor(Tensor): ...

 example:

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
b = torch.rand(3,3)#得到的是floattensor值,
print(type(b),b,b.type())
a = b.long()#得到的是longtensor值
print(type(a),a,a.type())
d=b.int()
print(type(d),d,d.type())

operation result:

PS F:\Graduation project\deeplearning> python .\c.py
<class 'torch.Tensor'> tensor([[0.4874, 0.3303, 0.6307],
        [0.4765, 0.2642, 0.4723],
        [0.2643, 0.5587, 0.0493]]) torch.FloatTensor
<class 'torch.Tensor'> tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]) torch.LongTensor
<class 'torch.Tensor'> tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]], dtype=torch.int32) torch.IntTensor

 

to sum up:

See data type a.type (), the data conversion .long () similar to the rest

Reference: https://blog.csdn.net/hustchenze/article/details/79154139

Published 234 original articles · won praise 61 · views 120 000 +

Guess you like

Origin blog.csdn.net/weixin_42528089/article/details/103841360