一、数据类型对比
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 in coding
- 例如:dog : [1 0],cat :[0 1]
- 对于复杂的编码来说,用One-hot进行编码会变得十分稀疏
- Embedding(待补)
- Word2vec
- glove
三、PyTorch数据类型
Data type | dtype | CPU tensor | GPU tensor |
---|
32位float | torch.float32 or torch.float | torch.FloatTensor | torch.cuda.FloatTensor |
64位float | torch.float64 or torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor |
32位int(signed) | torch.int32 or torch.int | torch.IntTensor | torch.cuda.IntTensor |
64位int(signed) | torch.float64 or torch.long | torch.LongTensor | torch.cuda.LongTensor |
8为int(unsigned) | torch.uint8 | torch.ByteTensor | torch.cuda.ByteTensor |
四、Tensor常用函数
a.type()
:返回数据类型
a = torch.rand(2,3)
print(a.type())
isinstance(obj,type)
:检查数据类型
a = rand(1)
print(isinstance(a,torch.FloatTensor))
a = a.cuda()
print(isinstance(a,torch.cuda.FloatTensor))
- 标量dim=0
torch.tensor()
:生成标量obj.shape
:返回obj的维度大小len(obj.shape)
:返回dimobj.size()
:返回元素个数
b = torch.tensor(1.3)
print(b.shape)
print(len(b.shape))
print(b.size())
- 向量dim=1
torch.tensor([...])
:生成标量np.ones(shape)
:生成全为1的向量
c = torch.tensor([1.1])
print(c)
d = torch.tensor([1.1,2.2])
print(d)
e = torch.FloatTensor(1)
print(e)
f = torch.FloatTensor(5)
print(f)
g = np.ones(2)
print(g)
print(torch.from_numpy(g))
h = torch.ones(2)
print(h.shape)
- dim=2
torch.rand(h,w)
:生成行h,列wsize()
:0行,1列shape[]
:返回维度的个数
i = torch.rand(2,3)
print(i)
print(i.shape)
print(i.size(0))
print(i.size(1))
print(i.shape[0])
print(i.shape[1])
- dim=3
torch.rand(a,b,c)
obj.numel()
:返回内存大小obj.dim()
:返回dim
j = torch.rand(1,2,3)
print(j)
print(j.shape)
print(j[0])
print(list(j.shape))
print(j.numel())
print(j.dim())