Pytorch基本数据类型
All is about Tensor
Python | PyTorch |
---|
Int | IntTensor of size() | float | FloatTensor of size() | Int array | IntTensor of size[d1, d2,…] | Float array | FloatTensor of size[d1,d2,…] | Str | – |
How to denote string
Data type
import torch
a = torch.randn(2,3)
a.type()
'torch.FloatTensor'
type(a)
torch.Tensor
isinstance(a, torch.FloatTensor)
True
data = a
isinstance(data, torch.cuda.FloatTensor)
False
data = data.cuda()
isinstance(data, torch.cuda.FloatTensor)
True
torch.tensor(1.)
tensor(1.)
torch.tensor(1.3)
tensor(1.3000)
a = torch.tensor(2.2)
a.shape
torch.Size([])
len(a.shape)
0
a.size()
torch.Size([])
torch.tensor([1.1])
tensor([1.1000])
torch.tensor([1.1,2.2])
tensor([1.1000, 2.2000])
torch.FloatTensor(1)
tensor([0.])
torch.FloatTensor(2)
tensor([0., 0.])
import numpy as np
data = np.ones(2)
data
array([1., 1.])
torch.from_numpy(data)
tensor([1., 1.], dtype=torch.float64)
a = torch.ones(2)
a.shape
torch.Size([2])
a = torch.randn(2,3)
a
tensor([[-2.3587, 0.5037, 0.8426],
[-0.1395, 0.5736, 0.0038]])
a.shape
torch.Size([2, 3])
a.size(0)
2
a.size(0)
2
a.size(1)
3
a.shape[1]
3
a = torch.rand(1,2,3)
a
tensor([[[0.5847, 0.4324, 0.2659],
[0.2969, 0.0352, 0.1932]]])
a.shape
torch.Size([1, 2, 3])
a[0]
tensor([[0.8617, 0.3860, 0.4491],
[0.9014, 0.7236, 0.5982]])
list(a.shape)
[1, 2, 3]
a = torch.rand(2, 3, 28, 28)
a
tensor([[[[0.8828, 0.6617, 0.0595, ..., 0.2063, 0.6698, 0.9692],
[0.2762, 0.8724, 0.5322, ..., 0.9264, 0.4652, 0.1117],
[0.0797, 0.7335, 0.5584, ..., 0.7487, 0.3993, 0.8792],
...,
[0.4200, 0.7895, 0.0608, ..., 0.2230, 0.4791, 0.9895],
[0.6966, 0.6509, 0.7475, ..., 0.9357, 0.2222, 0.5091],
[0.9888, 0.3824, 0.3494, ..., 0.0992, 0.1751, 0.9104]],
[[0.9390, 0.1380, 0.6556, ..., 0.8514, 0.8710, 0.1470],
[0.0582, 0.1105, 0.2613, ..., 0.5967, 0.6255, 0.5425],
[0.3768, 0.3766, 0.6858, ..., 0.8140, 0.6406, 0.3226],
...,
[0.4511, 0.9047, 0.8087, ..., 0.5146, 0.3643, 0.3774],
[0.6760, 0.4325, 0.8090, ..., 0.6828, 0.6155, 0.3927],
[0.5951, 0.2969, 0.2370, ..., 0.4704, 0.6218, 0.6115]],
[[0.9448, 0.0337, 0.4944, ..., 0.5077, 0.3605, 0.1576],
[0.8310, 0.1867, 0.1950, ..., 0.7729, 0.1453, 0.5513],
[0.0218, 0.1491, 0.4334, ..., 0.6247, 0.9791, 0.0589],
...,
[0.0835, 0.9804, 0.0470, ..., 0.5622, 0.3367, 0.5041],
[0.8451, 0.3326, 0.0539, ..., 0.6224, 0.7943, 0.8747],
[0.5174, 0.0323, 0.3455, ..., 0.7088, 0.1844, 0.6642]]],
[[[0.1665, 0.6587, 0.3739, ..., 0.7705, 0.2233, 0.5640],
[0.6353, 0.2907, 0.2761, ..., 0.0375, 0.9990, 0.1509],
[0.8480, 0.1878, 0.5105, ..., 0.9123, 0.3218, 0.2966],
...,
[0.7924, 0.1838, 0.1870, ..., 0.2371, 0.3911, 0.2764],
[0.0753, 0.4554, 0.5595, ..., 0.3365, 0.0462, 0.5924],
[0.2841, 0.0991, 0.6688, ..., 0.3673, 0.4465, 0.1054]],
[[0.4828, 0.7561, 0.5818, ..., 0.4361, 0.5383, 0.2329],
[0.6777, 0.8608, 0.3140, ..., 0.2832, 0.5151, 0.4656],
[0.1136, 0.0579, 0.4239, ..., 0.9170, 0.6756, 0.3849],
...,
[0.0961, 0.5933, 0.5865, ..., 0.5757, 0.6700, 0.8369],
[0.0503, 0.9264, 0.4842, ..., 0.2266, 0.0400, 0.3625],
[0.7243, 0.7906, 0.8159, ..., 0.0406, 0.3719, 0.6781]],
[[0.6152, 0.3097, 0.1880, ..., 0.8094, 0.8706, 0.4427],
[0.1928, 0.2313, 0.4480, ..., 0.8179, 0.0032, 0.4803],
[0.4220, 0.3285, 0.3260, ..., 0.1025, 0.1659, 0.6078],
...,
[0.1934, 0.3361, 0.6843, ..., 0.8997, 0.9822, 0.9209],
[0.1084, 0.8956, 0.9310, ..., 0.6991, 0.9082, 0.5535],
[0.0576, 0.6069, 0.9415, ..., 0.7191, 0.9698, 0.4304]]]])
a.shape
torch.Size([2, 3, 28, 28])
a.numel()
4704
a.dim()
4
a = torch.tensor(1)
a.dim()
0
|