这里写自定义目录标题
一.基本数据类型
1.不同地方的数据类型对应表
在深度学习里面注意关注CPU和GPU的数据类型。
2.测试数据类型
一共有三种方法 :
1. a.type()
2. type(a)
3. isinstance(a, torch.FloatTensor)
Example
a = torch.randn(2, 3) #二维的tensor
print(a.type()) # torch.FloatTensor 32位浮点数 CPUtensor张量
print(type(a)) #<class 'torch.Tensor'>基本的数据类型,并没有提供额外的信息
# 合法化检验
print(isinstance(a, torch.FloatTensor)) # isinstance(a,b) a是实例对象,b是类型,判断数据a是不是b类型
# true
print(isinstance(a, torch.cuda.DoubleTensor)) # False
a = a.cuda() #搬运到GPU上面,x.cuda会返回一个GPU的引用
print(isinstance(a, torch.cuda.DoubleTensor)) # True
3.维度 Dimension / rank
就是向量。
0维,就是一个具体的数字,比如torch.tensor(1.)
括号里面是一个数字。
1维,就是数学里面的一维向量。比如torch.tensor([1.1])
,小括号里面是一个一维矩阵
2维,就是数学里面的二维向量。比如tensor([[ 0.2318, 0.1173,-0.0886],[0.7468, -1.6688, -1.5902]])
3维,tensor([[[0.4397, 0.7143, 0.6235], [0.5181, 0.0557, 0.8930]]])
一般有几个维度就有几个中括号
1. torch.tensor([1.1]) #.tensor接受的是数据的内容,该Tensor为一维,内容是1.1
2. torch.FloatTensor(1) #.FloatTensor接受的是数据的shape,也就是大小,代表这个Tensor是一维的,里面有一个数字
3. a.shape / a.size() # torch.Size([2, 3]),输出整体框架,如果二维就是行数和列数 shape: number of rows and columns
4. a.size(x) /a.shape[x] # 返回shape的第x个元素
5. len(a.shape()) # 返回维度的具体值,0,1,2等等
具体例子
'''
Dimension 0 / rank 0
'''
a = torch.tensor(1.)
print(a) # tensor(1.)
b = torch.tensor(1.3)
print(b) # tensor(1.3000)
print(b.shape) # torch.Size([]) ,shape代表尺寸大小
print(len(b.shape)) # 0,返回维度的值
print(b.size()) # torch.Size([])
'''
Dim 1 / rank 1
数学里面的向量,torch里面的张量
一维用在bias,神经网络的偏置
linear input
'''
a = torch.tensor([1.1]) #.tensor接受的是数据的内容
print(a) # tensor([1.1000])
b = torch.tensor([1.1, 2.2])
print(b) # tensor([1.1000, 2.2000])
c = torch.FloatTensor(1) #.FloatTensor接受的是数据的shape
print(c) # tensor([1.4013e-45]) 这个每次运行都不一样
d = torch.FloatTensor(2)
print(d) # tensor([-1.1833e+08, 4.5907e-41])
data = np.ones(2)
print(data) # [1. 1.]
# torch.from_numpy()方法把数组转换成张量,且二者共享内存,
# 对张量进行修改比如重新赋值,那么原始数组也会相应发生改变。
print(torch.from_numpy(data)) # tensor([1., 1.], dtype=torch.float64)
'''
Dim 1
'''
a = torch.ones(2)
print(a.shape) # torch.Size([2])
'''
Dim 2
LInear input batch
'''
a = torch.randn(2, 3)
print(a) # tensor([[ 0.2318, 0.1173, -0.0886],
# [ 0.7468, -1.6688, -1.5902]])
print(a.shape) # torch.Size([2, 3])
print(a.size(0)) # 2,
print(a.size(1)) # 3,返回shape的第二个元素3
print(a.shape[1]) # 3
print(a.shape[0]) # 2
# print(a.shape[3]) # IndexError: tuple index out of range
'''
Dim 3
RNN input batch
'''
a = torch.rand(1, 2, 3) #随机均匀分布
print(a) #tensor([[[0.5242, 0.6464, 0.5177],
#[0.8840, 0.0703, 0.4289]]])
print(a.shape) # torch.Size([1, 2, 3])
print(a[0]) # tensor([[0.8280, 0.3117, 0.5487],
# [0.4074, 0.2204, 0.2677]])
print(list(a.shape)) # [1, 2, 3]
'''
Dim 4
CNN:[b,c,h,w]
b张照片,c个通道,h*w的大小
'''
a = torch.rand(2,3,28,28)
print(a)
print(a.shape) #torch.Size([2, 3, 28, 28])
'''
mixed
'''
print(a.shape) #torch.Size([2, 3, 28, 28])
#numel是指Tensor占用内存的数量Number of element
print(a.numel()) #4704=6*28*28
print(a.dim()) #4
a = torch.tensor(1)
print(a.dim()) #0在这里插入代码片
二.创建Tensor
介绍创建Tensor的两种方式,和常用函数。
1.import from numpy
从numpy引入,从numpy导入的是double类型。
Example
2.import from list
a = np.array([2,3,3])
print(a) # [2 3 3]
print(torch.from_numpy(a)) # tensor([2, 3, 3], dtype=torch.int32)
a = np.ones([2,3])
print(torch.from_numpy(a)) # tensor([[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64)
用在数据量不是很大。
.tensor接受的是现有的数据
.Tensor接受的是维度的维度或者线程的数据
torch.FloatTensor(d1,d2,d3)给定的是shape
Example
a = torch.tensor([2.,3.2])
print(a) #tensor([2.0000, 3.2000])
# 少使用这种
# b = torch.FloatTensor([2.,3.2])
# print(b) #tensor([2.0000, 3.2000])
c = torch.tensor([[2.,3.2],[1.,22.3]])
print(c) #tensor([[ 2.0000, 3.2000],[ 1.0000, 22.3000]])
3.常用函数
(1)不知道是什么
example
'''
uninitalized
未初始化:生成的随机数据大小不一
未初始化的数据一定要跟写入数据的后续工作
'''
a = torch.empty(1) #生成shape
print(a) # tensor([0.])
b = torch.Tensor(2,3)
print(b) #tensor([[0., 0., 0.],[0., 0., 0.]])
c = torch.IntTensor(2,3)
print(c)
d = torch.FloatTensor(2,3)
print(d)
'''
set default type
增强学习一般使用double,其他一般使用float
'''
a = torch.tensor([1.2,3]).type()
print(a) # torch.FloatTensor,转换到默认的数据类型FloatTensor
torch.set_default_tensor_type(torch.DoubleTensor)
d = torch.tensor([1.2,3]).type()
print(d) # torch.DoubleTensor
'''
(2)rand/rand_like,randint
随机初始化
rand会随机产生0-1之间的数值,不包括1
a = torch.rand(3,3)
print(a)
b=torch.rand_like(a) #a的shape读出来喂给Tensor函数,生成和a同shape的函数
print(b)
c = torch.randint(1,10,[2,2]) #用于生成一个指定范围内的整数。[下限,上限,shape)
print(c)
(3)randn
正态分布,均值为0,方差为1
'''
N(u,std):自定义方差和均值
3*3先打平成9,然后9.mean求均值,9.std求方差
mean: full([10],0)生成长度为10的数字,均值为0
std: arange不包括右边界[1,0.9,。。。,0.1] 方差逐渐减小
'''
a = torch.randn(3,3)
print(a)
b = torch.normal(mean=torch.full([10],0.),std = torch.arange(1.,0,-0.1))
print(b)
(4)full
把Tensor全部赋值为一个元素
a = torch.full([2,3],7)
print(a) # tensor([[7, 7, 7],[7, 7, 7]])
b = torch.full([],7)
print(b) # tensor(7)
c = torch.full([1],7)
print(c) # tensor([7])
(5) arange/range
生成等差数列
a = torch.arange(0,10) #[0,10)的等差数列
print(a) # tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a = torch.arange(0,10,2) #2代表步长
print(a)
# b = torch.range(0,10) #不建议使用,用arange代替
print(b)
(6)linspace/logspace
等分的切
logspace返回的数据是切割后*10,它的base参数可以设置2,10,e等底数
a = torch.linspace(0,10,steps = 4) #4代表数量[1,10]
print(a) # tensor([ 0.0000, 3.3333, 6.6667, 10.0000])
b = torch.linspace(0,10,steps = 10)
print(b) # tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,8.8889, 10.0000])
c = torch.linspace(0,10,steps=11)
print(c) # tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
d = torch.logspace(0,-1,steps=10)
print(d) #tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,0.1000])
e = torch.logspace(0,1,steps=10)
print(e) # tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948,7.7426, 10.0000])
(7)Ones/zeros/eye
全1,全0,对角阵
a = torch.ones(3,3)
print(a) # tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]])
b = torch.zeros(3,3) # tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])
c = torch.eye(3,4)
# print(c)
# tensor([[1., 0., 0., 0.],
# [0., 1., 0., 0.],
# [0., 0., 1., 0.]])
e = torch.eye(3)
print(e)
# tensor([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
f = torch.zeros(3,3)
f =torch.ones_like(f)
print(f) # tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]])
(8)randperm
随机打散,但是每一组数据的互相匹配的。
比如(【小明,2】,【小红,3】)打散后可能得到小红,但是数值仍然是小红
a = torch.randperm(10)
print(a) # tensor([0, 1, 5, 3, 6, 8, 9, 4, 2, 7])
a = torch.rand(2,3)
b = torch.rand(2,2)
idx=torch.randperm(2)
print(idx) # tensor([1, 0])
print(idx) # tensor([1, 0])
print(a[idx]) # tensor([[0.9129, 0.5082, 0.0319],[0.3081, 0.0239, 0.6304]])
print(b[idx]) # tensor([[0.0885, 0.8689],[0.7707, 0.2766]])
print(a,b) # tensor([[0.4820, 0.0119, 0.9619],[0.3823, 0.5798, 0.6279]]) tensor([[0.5192, 0.0659],[0.9258, 0.6447]])
三.索引与切片
索引:找出Tensor里面的某个或多个元素
01.批量操作索引元素
a = torch.rand(4,3,28,28)
print(a[0].shape) # torch.Size([3, 28, 28])
#取得了第一张图片,包含的3个通道,大小为28*28
print(a[0,0].shape) # torch.Size([28, 28])
print(a[0,0,2,4]) # tensor(0.7172)
02.索引连续数据
'''
连续的数据,取前N个或者后N个
select first/last N
a[:2] [0,2)
a[1:] 1,2两个通道
'''
print(a.shape) # torch.Size([4, 3, 28, 28])
print(a[:2].shape) # torch.Size([2, 3, 28, 28])
print(a[:2,:1,:,:].shape) # torch.Size([2, 1, 28, 28])
print(a[:2,1:,:,:].shape) # torch.Size([2, 2, 28, 28])
print(a[:2,-1:,:,:].shape)# torch.Size([2, 1, 28, 28])
'''
select by steps
0:28:2 步长是2 不包括28
'''
print(a[:,:,0:28:2,0:28:2].shape) # torch.Size([4, 3, 14, 14])
print(a[:,:,::2,::2].shape) # torch.Size([4, 3, 14, 14])
03.给出具体索引号
'''
给了具体的索引号
select by specific index
'''
print(a.shape) # torch.Size([4, 3, 28, 28])
#选择第一个维度里面的0和2
print(a.index_select(0,torch.tensor([0,2])).shape) #torch.Size([2, 3, 28, 28])
print(a.index_select(1,torch.tensor([1,2])).shape) #torch.Size([4, 2, 28, 28])
print(a.index_select(2,torch.arange(28)).shape) #torch.Size([4, 3, 28, 28])
print(a.index_select(2,torch.arange(8)).shape) #torch.Size([4, 3, 8, 28])
#...代表任意多的维度
print(a[...].shape) # torch.Size([4, 3, 28, 28])
print(a[0,...].shape) # torch.Size([3, 28, 28])
print(a[:,1,...].shape) # torch.Size([4, 28, 28])
print(a[...,:2].shape) # torch.Size([4, 3, 28, 2])
04.把数据打平
'''
使用掩码来索引,默认把数据打平
select by mask
masked_select()
'''
x = torch.randn(3,4)
print(x) #输出三行四列的Tensor
mask = x.ge(0.5) #ge()表示大于等于0.5的元素置为1
# tensor([[0,0,0,0],[0,1,0,0],[0,0,1,1]],dtype = torch.uint8)
y = torch.masked_select(x,mask)
print(y) # tensor([0.7927, 0.5571, 1.5553, 1.1502, 0.6673, 1.8108])
print(torch.masked_select(x,mask).shape) # torch.Size([6])
'''
select by flatten index
把Tensor先打平,
'''
src = torch.tensor([[4,3,5],
[6,7,8]])
print(torch.take(src,torch.tensor([0,2,5]))) # tensor([4, 5, 8]),里面的0,2,5代表索引