python与pytorch对应转换

python PyTorch
Int IntTensor of size()
float FloatTensor of size()
Int array IntTensor of size [d1, d2 ,.….]
Float array FloatTensor of size [d1, d2,…]
string

怎样表达string

  • One-hot
    • [0,1,0,0,…]
    • 缺点:单词之间的相关性并没有体现出来,并且是极度稀疏矩阵。
  • Embedding:数字表达语言
    • word2vec
    • glove

数据类型

Data type dtype CPU tensor GPU tensor
32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point torch.float16 or torch.half torch.HalfTensor torch.cuda.HalfTensor
8-bit integer (unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor
8-bit integer (signed) torch.int8 torch.CharTensor torch.cuda.CharTensor
16-bit integer (signed) torch.int16 or torch.short torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.int32 or torch.int torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.int64 or torch.long torch.LongTensor torch.cuda.LongTensor

Type check

in:a = torch.randn(2,3)
in:a.type()
out:'torch.floatTensor'
in: type(a)
out:torch.Tensor
in:isinstance(a,torch.FloatTensor)
out:True
isinstance(data,torch.cude,DoubleTensor)
#false
data=data.cude()
isinstance(data,torch.FloatTensor)
#True

Dimension 0 /rank 0

torch.tensor(1.)
#tensor(1.)
torch.tensor(1.3)
#tensor(1.300)

Dim 0

>>> a=torch.tensor(2.2)
>>> a.shape
torch.Size([])
>>> len(a.shape)
0
>>> a.size()
torch.Size([])
  • 通常用在loss

dim 1 /rank 1

>>> torch.tensor([1.1])
tensor([1.1000])

>>> torch.tensor([1.1,2.2])
tensor([1.1000, 2.2000])

>>> torch.FloatTensor(1)
tensor([3.0100e+32])

>>> torch.FloatTensor(2)
tensor([0.0000, 1.8750])

>>> data = np.ones(2)
>>> data
array([1., 1.])

>>> torch.from_numpy(data)
tensor([1., 1.], dtype=torch.float64)
  • 通常用在Bias
  • 或者用在Linear Input
>>> a = torch.ones(2)
>>> a.shape
torch.Size([2])

Dim 2

>>> a = torch.randn(2,3)
>>> a
tensor([[ 2.8500, -1.0306,  0.4257],
        [-0.9918,  0.3094,  0.3869]])
>>> a.shape
torch.Size([2, 3])
>>> a.size(0)
2
>>> a.size(1)
3
>>> a.shape[1]
3
  • 通常用在带有batch的linear input
>>> import torch
>>> a=torch.rand(1,2,3)
>>> a
tensor([[[0.2698, 0.2206, 0.2297],
         [0.6471, 0.9053, 0.2742]]])
>>> a.shape
torch.Size([1, 2, 3])
>>> a[0]
tensor([[0.2698, 0.2206, 0.2297],
        [0.6471, 0.9053, 0.2742]])
>>> list(a.shape)
[1, 2, 3]
  • RNN input Batch
    • [10个单词,20句话,100维表示]

Dim4

>>> a=torch.rand(2,3,28,28)#张数,通道数,28*28尺寸
>>> a
tensor([[[[0.6408, 0.0313, 0.0700,  ..., 0.1324, 0.7475, 0.1444],
          [0.8315, 0.7339, 0.0126,  ..., 0.5260, 0.4896, 0.7950],
          [0.5399, 0.6679, 0.6118,  ..., 0.1571, 0.0436, 0.6759],
         [[0.6578, 0.7380, 0.5285,  ..., 0.0970, 0.8109, 0.2364],
          [0.4398, 0.4145, 0.6420,  ..., 0.7736, 0.1780, 0.4816],
          [0.5297, 0.0386, 0.2156,  ..., 0.3286, 0.7100, 0.1554],
          ...,
          [0.4681, 0.1787, 0.7982,  ..., 0.5238, 0.5333, 0.7992],
          [0.5267, 0.1461, 0.7075,  ..., 0.0974, 0.9872, 0.8482],
          [0.9274, 0.4764, 0.9747,  ..., 0.1132, 0.7222, 0.1312]]]])
>>> a.shape
torch.Size([2, 3, 28, 28])

Mixd

>>> a.shape
torch.Size([2, 3, 28, 28])
>>> a.numel()
4704
>>> a.dim()
4
>>> a=torch.tensor(1)
>>> a.dim()
0