pytorch日积月累1-张量数据类型

张量是一个多维数组,它是标量、向量、矩阵的高维拓展。

Variabletorch.autograd中的数据类型,主要用于封装Tensor,进行自动求导

  • data : 被包装的Tensor

  • grad: data的梯度

  • grad_fn : 创建Tensor的Function,是自动求导的关键

  • requires_ grad : 指示是否需要梯度

  • is_ leaf: 指示是否是叶子结点(张量)

PyTorch 0.4.0版开始,Variable并入Tensor

  • dtype : 张量的数据类型,如 torch .FloatTensor, torch .cuda.FloatTensor

  • shape : 张量的形状,如( 64 , 3 , 224 , 224 )

  • device : 张量所在设备,GPU/CPU,是加速的关键

image-20220629122315290

image-20200721175749987

1.直接创建

1
2
3
4
5
6
7
8
9
10
#直接创建
import torch
arr = np.ones((3, 3))
torch.tensor(
data, #数据, 可以是list, numpy
dtype=None, #数据类型,默认与data的一致
device=None, #所在设备, cuda/cpu
requires_grad=False, #是否需要梯度
pin_memory=False#是否存于锁页内存
)

image-20220629125322277

numpy引入法

torch.from_numpy (ndarray)

功能:从numpy创建tensor

注意事项:从torch.from_numpy创建的tensor于原ndarray共享内存,当修改其中一个的数据,另外一个也将会被改动

1
2
3
4
5
6
7
import numpy as np
import torch
a=np.array([2,3.3])
#从numpy导入的float其实是double类型
torch.from_numpy(a)
a=np.ones([2,3])
torch.from_numpy(a)

2.依据数值创建

1
2
3
4
torch.zeros(*size, out=None, dtype=None, 
layout=torch.strided, device=None, requires_grad=False)
torch.zeros_like(input, dtype=None, layout=None, device=None,
requires_grad=False)

image-20220629125346184

1
2
3
4
torch.ones(*size, out=None, dtype=None, layout=torch.strided, 
device=None, requires_grad=False
torch.ones_like(input, dtype=None, layout=None, device=None,
requires_grad=False)
1
2
torch.full( size, fill_value, out=None, dtype=None,
layout=torch.strided, device=None, requires_grad=False)

image-20220629125406945

1
2
3
4
5
6
#功能:创建等差的1维张量,注意事项:数值区间为[start, end)           
torch.arange(start=0, #起始值
end, #结束值
step=1, #步长,数列公差,默认为1
out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False)

image-20220629125425456

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#功能:创建均分的1维张量           
torch.linspace(start,
end, #注意事项:数值区间为[start, end]
steps=100,#steps: 数列长度
out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False)
torch.logspace(start, #功能:创建对数均分的1维张量
end, #注意事项:长度为steps, 底为base
steps=100,
base=10.0,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)

image-20220629125441943

1
2
3
4
5
6
7
8
torch.eye(#功能:创建单位对角矩阵( 2维张量)注意事项:默认为方阵
n,#矩阵行数
m=None, #矩阵列数
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)

image-20220629125507722

3.依据概率分布创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#功能:生成正态分布(高斯分布) 
torch.normal(mean,std, out=None)

#功能:生成标准正态分布
torch.randn(*size, out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False)

#功能:在区间[0, 1)上,生成均匀分布
torch.rand(*size, out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False)

#功能:区间[low, high)生成整数均匀分布
torch.randint(low=0,
high, size, out=None, dtype=None, layout=torch.strided,
device=None, requires_grad=False)

#功能:生成生成从0到n-1的随机排列,n : 张量的长度
torch.randperm(n,out=None,dtype=torch.int64,layout=torch.strided,
device=None,requires_grad=False)

#功能:以input为概率,生成伯努力分布(0-1分布,两点分布)
torch.bernoulli(input, *, generator=None, out=None)

image-20220629125622372

List引入法

1
2
3
4
5
import numpy as np
import torch
torch.tensor([2.,3.2])
torch.FloatTensor([2.,3.2])
torch.tensor([[2.,3.2],[1.,22.3]])

未初始化数据

torch.empty()

torch.FloatTensor(d1,d2,d3)注意:这里不是torch.FloatTensor([1,2])=torch.tensor([1,2])

Torch.IntTensor(d1,d2,d3)

1
2
3
4
5
6
import numpy as np
import torch
torch.empty(1)
torch.Tensor(2,3)
torch.IntTensor(2,3)
torch.FloatTensor(2,3)

一个小技巧:设置对应的Tensor格式:

1
2
3
4
#增强学习中一般使用tensor,很少使用double
torch.tensor([1.2,3]).type()
torch.set_default_tensor_type(torch.DoubleTensor)
torch.tensor([1.2,3]).type()