基本数据类型
简述
对于Python的数据类型int
、float
等,PyTorch中相对应的就是torch.IntTensor
、torch.FloatTensor
等,如果就是普通的数就用demension是0,如果是数组那就用更高的维度来表示就可以了。
上面都是在CPU上的数据类型,如果是在GPU上面的数据类型,那就是torch.cuda.IntTensor
、torch.cuda.FloatTensor
之类的。
PyTorch没有对String
的支持,如果是用于类别可以直接用one-hot编码,也可以用Word2Vec之类的。
类型检查
import torch
a = torch.randn(2, 3) # 2行3列,正态分布~N(0,1)
print(a)
print(type(a))
print(a.type())
print(isinstance(a, torch.FloatTensor))
运行结果:
tensor([[-0.6646, 0.3935, 1.2683],
[-1.8576, 0.2761, 1.4787]])
<class 'torch.Tensor'>
torch.FloatTensor
True
CPU转GPU类型
import torch
a = torch.randn(2, 3)
print(a.type())
print(isinstance(a, torch.cuda.FloatTensor))
a = a.cuda() # 转换为GPU上的类型
print(a.type())
print(isinstance(a, torch.cuda.FloatTensor))
运行结果:
torch.FloatTensor
False
torch.cuda.FloatTensor
True
各个维度的Tensor
注意,size()得到的也是shape,shape有多少个分量,那么维度dim就等于几。
标量(dim=0)
标量一般用在Loss这种地方。
注意shape和size()是同一个东西,都表示shape,但是和dim()不是同一个东西,dim()只求维度。
import torch
a = torch.tensor(1.6) # dimension为0就是标量了
print(a, a.type())
# 一些检验维度的方法
print(a.dim())
print(a.shape, a.size())
print(len(a.shape), len(a.size()))
运行结果:
tensor(1.6000) torch.FloatTensor
0
torch.Size([]) torch.Size([])
0 0
dim=1的张量
dim=1的Tensor一般用在Bais这种地方,或者神经网络线性层的输入Linear Input,例如MINST数据集的一张图片用shape=[784]的Tensor来表示。
dim=1相当于只有一个维度,但是这个维度上可以有多个分量(就像一维数组一样)。
import torch
import numpy as np
def printMsg(k):
"""输出Tensor的信息,维度,shape"""
print(k, k.dim(), k.size(), k.shape)
# 从给定的一维list构造dim=1的Tensor
a = torch.tensor([1.1])
printMsg(a)
b = torch.tensor([1.1, 2.2])
printMsg(b)
print("-" * 20)
# 随机构造dim=1的Tensor,这里传入的是
c = torch.FloatTensor(1)
printMsg(c)
d = torch.FloatTensor(2)
printMsg(d)
print("-" * 20)
# 从numpy构造dim=1的Tensor
e = np.ones(2)
print(e)
e = torch.from_numpy(e)
printMsg(e)
运行结果:
tensor([1.1000]) 1 torch.Size([1]) torch.Size([1])
tensor([1.1000, 2.2000]) 1 torch.Size([2]) torch.Size([2])
--------------------
tensor([-1.0842e-19]) 1 torch.Size([1]) torch.Size([1])
tensor([3.6894e+19, 7.8125e-03]) 1 torch.Size([2]) torch.Size([2])
--------------------
[1. 1.]
tensor([1., 1.], dtype=torch.float64) 1 torch.Size([2]) torch.Size([2])
dim=2的张量
dim=2的张量一般用在带有batch的Linear Input,例如MNIST数据集的k张图片如果放再一个Tensor里,那么shape=[k,784]。
import torch
from utils.show import printMsg
# dim=2,shape=[4,5],随机生成Tensor
a = torch.FloatTensor(4, 5)
printMsg(a)
print(a.shape[0], a.shape[1])
print(a.size(0), a.size(1))
运行结果:
tensor([[1.0930e-38, 0.0000e+00, 2.8170e-36, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 2.1019e-44, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 1.4013e-45, 0.0000e+00, 0.0000e+00, 0.0000e+00]]) 2 torch.Size([4, 5]) torch.Size([4, 5])
4 5
4 5
dim=3的张量
dim=3的张量很适合用于RNN和NLP,如20句话,每句话10个单词,每个单词用100个分量的向量表示,得到的Tensor就是shape=[20,10,100]。
import torch
from utils.show import printMsg
# dim=3,shape=[1,2,3],随机取0~1之间的数
a = torch.rand(1, 2, 3)
printMsg(a)
b = a[0] # 在第一个维度上取,得到的就是shape=[2,3]的dim=2的Tensor
printMsg(b)
运行结果:
tensor([[[0.4408, 0.3562, 0.3832],
[0.7311, 0.6969, 0.0686]]]) 3 torch.Size([1, 2, 3]) torch.Size([1, 2, 3])
tensor([[0.4408, 0.3562, 0.3832],
[0.7311, 0.6969, 0.0686]]) 2 torch.Size([2, 3]) torch.Size([2, 3])
dim=4的张量
dim=4的张量适合用于CNN表示图像,例如100张MNIST手写数据集的灰度图(通道数为1,如果是RGB图像通道数就是3),每张图高=28像素,宽=28像素,所以这个Tensor的shape=[10,1,28,28]。
C N N : [ b a t c h , c h a n n e l , h e i g h t , w i d t h ] CNN:[batch,channel,height,width] CNN:[batch,channel,height,width]
import torch
from utils.show import printMsg
a = torch.rand(2, 2, 2, 2)
printMsg(a)
运行结果:
tensor([[[[0.1466, 0.7224],
[0.1785, 0.6431]],
[[0.3087, 0.1673],
[0.5298, 0.7762]]],
[[[0.3388, 0.6073],
[0.7946, 0.4776]],
[[0.9370, 0.3064],
[0.0945, 0.6645]]]]) 4 torch.Size([2, 2, 2, 2]) torch.Size([2, 2, 2, 2])
计算Tensor中元素的数目
这受Tensor的shape直接影响:
import torch
a = torch.rand(10, 1, 28, 28)
print(a.numel()) # number of element 10*1*28*28=7840