文章整理或摘抄自肖智清的《神经网络与PyTorch实战》 ,本文仅作笔记。
目录
3.1张量
3.1.1 张量的数学定义
- 零维张量(单纯一个数),大小为0个条目元组()
- 一维张量(一个向量),大小为1个条目元组(3,)
- 二维张量(一个矩阵),大小为2个条目元组(3,3)
- 三维张量,大小为3个条目元组(3,3,3)
⚠大小为(3,1)和大小为(3,)的张量虽然有着相同的元素个数,但表示的不是相同的张量(维度、大小不一样)
3.1.2PyTorch里的张量
import torch
t2=torch.tensor([[0,1,2],[3,4,5]])
print(t2)
print('数据是{}'.format(t2))
print('大小:{}'.format(t2.size()))
print('维度:{}'.format(t2.dim()))
print('元素个数:{}'.format(t2.numel()))
print('元素类型:{}'.format(t2.dtype))
print(t2.reshape(3,2))#重新组织元素
print(t2+1)
以上为张量的简单用法
3.2构造torch.Tensor类实例
3.2.1构造含有特定数据的张量
t1=torch.tensor([1.,2.,3.])#t1表示一维张量,其余依此类推
t2=torch.tensor([[1.,2.,3.],[4.,5.,6.]])
#使用列表作为参数,由于参数中的数据都是浮点数,所以张量的数据类型是torch.float32
t_int8=torch.tensor([1,2],dtype=torch.int8)
t_int16=torch.tensor([1,2],dtype=torch.int16)
t_float16=torch.tensor([1,2],dtype=torch.float16)
#还有torch.int32 , torch.int64 , torch.float32 , torch.float64类型
⚠在构造torch.Tensor类实例时,如果用的是int值列表,那么默认数据类型是torch.int64,如果用float值列表,那么默认数据类型是torch.float32。
3.2.2构造特定大小的张量
#t1即是一维张量
t1=torch.empty(2) #未初始化
t2=torch.zeros(2,2) #各元素值为0
t3=torch.ones(2,2,2) #各元素值为1
t4=torch.full((2,2,2,2),3) #各元素值为3
#以上各函数都有各自对应的torch.~_like()形式,用以构造和指定张量一样大小的张量。
torch.ones_like(t2) #构造了和t2大小一样的张量
3.2.3构造等比数列和等差数列张量
——————
#等差数列的构造
#都可以构造一个tensor([0, 1, 2, 3])
print('torch.arange(0,4,step=1)={}'.format(torch.arange(0,4,step=1)))
print('torch.range(0,3,step=1)={}'.format(torch.range(0,3,step=1)))
torch.linspace(0,3,steps=4)
- 但是我在实际使用时遇到了这个问题:
UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).
print('torch.range(0,3,step=1)={}'.format(torch.range(0,3,step=1)))
简单的说就是,现在torch.range已经不用了,因为逻辑和Python里面有点不符,推荐替换成torch.arange(),范围左闭右开。
-
在linspace中,范围是[start,end],要注意的是,steps表达的意思是数列中一共有几个数。
-
还有一个注意的点,我得到的结果是这样的:
这说明了linspace 生成的张量数据类型是浮点型,arange是整型。
——————
#等比数列的构造
#构造tensor([ 1., 10., 100., 1000.])
print('torch.logspace(0,3,steps=4)'.format(torch.logspace(0,3,steps=4)))
- 与上面linspace类似的是,这里logspace得到的也是浮点型:torch.logspace(0,3,steps=4)=tensor([ 1., 10., 100., 1000.])。
- 再有就是书中未给出其他可以构造等比数列的函数。(可我感觉像arange那样的好用啊……😭)
3.2.4构造随机张量
——————
#随机张量
probs=torch.full((3,4),0.6)
print('torch.bernoulli(probs)=\n{}'.format(torch.bernoulli(probs)))
这个我还理解了好一会:
bernoulli函数生成的张量是一个每个位置数据为或0或1的张量,而张量的大小取决于概率张量的大小。如上面例子,torch.bernoulli(probs)生成的是一个3×4的张量,这是因为概率张量probs是一个3×4的张量。那么如何确定每个位置是0还是1呢?
如上图,probs里第1个数据为0.6,那么意味着在bernoulli生成的张量里,第1个数据出现0的概率为0.6,其余依此类推。
所以概率张量里的每一个数据必须在[0,1]范围内。
——————
weights=torch.tensor([[1,100],[100,1],[1,1]],dtype=torch.float32)#权重
print('torch.multinomial(weights,1)=\n{}'.format(torch.multinomial(weights,1)))
说实话,这个我不是很懂,不知道有没有会的大佬指点一下。关multinomial的说明,原书是这样表达的:
函数torch.multinomial()可以生成元素值为{0,1,……,n-1}的张量。这个函数可以接受两个参数,分别是表示结果张量中元素出现可能性的张量和表示对每个概率分布抽样次数的数值(一般设置为1)。值得一提的是,第0个参数中各元素出现的可能性的和不需要为1。该函数会自行完成概率的归一化。
——————
print('torch.randperm(5,dtype=torch.float32)=\n{}'.format(torch.randperm(5,dtype=torch.float32)))
randperm的使用如下:
torch.randperm(n,dtype)会生成一个大小为(n,)、数据类型为dtype的一维向量,是一个{0,……,n-1}的随机排列。
——————
print('tor...size=(3,4))\n{}'.format(torch.randint(low=10,high=25,size=(3,4))))
print('torch.randint(high=5,size=(3,4))\n{}'.format(torch.randint(high=5,size=(3,4))))
原文指出了函数的参数有low和high,但在实际使用时发现提示里只有high,但是输入low也是可以的。这里指出的是,如果省略了low,那么默认low=0(整型)。范围:[low,high)。
同样的,torch.randint_like(t,low,high)构造一个大小与第0个参数的张量t大小一样的随机张量,上下限取决于high,low。范围:[low,high)。
——————
print('torch.rand(3,4)=\n{}'.format(torch.rand(3,4)))
print('torch.rand_like=\n{}'.format(torch.rand_like(torch.tensor([[0.5,0.7,0.1],[1.,2.5,0.3]]))))
⚠需要注意的是,原文并没有提及到,rand_like中传入的张量数据类型必须是浮点型,否则会报出这样的错误:"check_uniform_bounds" not implemented for 'Long'
rand , rand_like产生的随机张量在[0,1)上标准均匀分布的概率是一样的。
——————
print('torch.randn(3,4)=\n{}'.format(torch.randn(3,4)))
t=torch.tensor([[1.,0.2,3,3],[4,4.,2,3]])
print('torch.randn_like=\n{}'.format(torch.randn_like(t)))
randn , randn_like与ones , ones_like用法相似,而且randn_like传入的数据类型也是浮点型。
randn(_like)产生的张量是标准正态分布的随机张量,均值为0,方差为1。
也可以产生自定义方差,均值的正态分布的随机张量:
mean=torch.tensor([0.,1.]) #均值
std=torch.tensor([3.,2.]) #方差
print('\n',torch.normal(mean,std))
但是这个还是不太能明白具体含义,书中也没有过多讲述。