在PyTorch中,张量Tensor是最基础的运算单位,与NumPy中的NDArray类似, 张量表示的是一个多维矩阵。不同的是,PyTorch中的Tensor可以运行在GPU. 上,而NumPy的NDArray只能运行在CPU上。由于Tensor能在GPU_ 上运行,因此大大加快了运算速度。
PyTorch中Tensor的维度不同对应python中不同的数据类型:维度为0的表示标量,对应python中的一个普通的数;维度为1的Tensor对应python中的一维数组;以此类推
PyTorch不支持string类型,它不是一个完备的语言库,它是一个面向计算的GPU加速库,没有内建的对string的支持。
怎么表达string?
One-hot编码用于类别Embedding数据类型 同一个数据可能放在CPU上或者GPU上,在Pytorch中对应的数据类型不一样
运行结果:
tensor([[-0.6646, 0.3935, 1.2683], [-1.8576, 0.2761, 1.4787]]) <class 'torch.Tensor'> torch.FloatTensor True运行结果:
torch.FloatTensor False torch.cuda.FloatTensor True标量一般用于在Loss
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 0dim=1的Tensor一般用在Bais这种地方,或者神经网络线性层的输入Linear Input,例如MINST数据集的一张图片用shape=[784]的Tensor来表示。
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的张量一般用在带有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 5dim=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的张量适合用于CNN表示图像,例如100张MNIST手写数据集的灰度图(通道数为1,如果是RGB图像通道数就是3),每张图高=28像素,宽=28像素,所以这个Tensor的shape=[10,1,28,28]。
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])**numel()**方法,返回Tensor元素数量
import torch a = torch.rand(10, 1, 28, 28) print(a.numel()) # number of element 10*1*28*28=7840