pytorch tensor 创建方法
![](https://img.haomeiwen.com/i8207483/e446bc1c3aa3b463.png)
import numpy as np
import torch
![](https://img.haomeiwen.com/i8207483/cb265e06784070de.jpeg)
甜点
我们生活的世界是 3 维的空间,这样就造成我们对高维空间理解的困难。我们通常都是通过感知来认识事物,所以如何在脑海里构建高维空间成为我们理解熟练掌握 tensor 的必修课。可能大家平时更多时间和精力都花在理解和创建复杂神经网络设计上,而忽视了 tensor 在如何在网络中变化(形状改变)。
通过 numpy 来创建 Tensor
在 python 项目中,随处可以看到 numpy 的身影、无论是机器学习还是数据分析挖掘项目中都可以会用到 numpy。所以在 tensorflow 和 pytorch 这些当下流行的深度学习框架都提供了对 numpy 的支持。
a = np.array([1,1.2])
使用 torch.from_numpy 将 numpy 的 ndarray 类型转换为 pytorch 的 tensor 类型,并且保持类型不变 float64
a_tensor = torch.from_numpy(a)
a_tensor
tensor([1.0000, 1.2000], dtype=torch.float64)
通过 List 来创建 Tensor
除了上边可以通过 numpy 的 ndarray 创建 Tensor 以外,pytorch 还支持通过 list 来创建 Tensor。
a_list = [1,1.2]
a_tensor = torch.tensor(a_list)
a_tensor
tensor([1.0000, 1.2000])
b_tensor = torch.FloatTensor(a_list)
b_tensor
tensor([1.0000, 1.2000])
注意这里使用 torch.tensor 创建 Tensor,在 torch 中还有一个大写开头 torch.Tensor,他们具有不同用途,也就是接收不同参数来创建 Tensor,小写开头 tensor 接收 list 或者 numpy 对象作为参数来创建 Tensor,而大写开头除此以外还接收表示size来创建一个随机初始化数据的 Tensor。
a_empty = torch.empty(2,3)
a_empty
tensor([[ 0.0000e+00, -2.5244e-29, -1.7533e+06],
[-3.6902e+19, 9.8091e-45, 0.0000e+00]])
a_tensor = torch.FloatTensor(2,3,28,28)
# a_tensor
使用 empty 或 FloatTensor 进行未初始化创建 Tensor,其实 Tensor 中也是有随机给定的值,不过这些值过大或者过小。
设置默认类型
如果我们在创建 tesnor 没有指定数据类型时,系统就会使用 torch.FloatTensor 默认数据类型。
a = torch.tensor([1.2,2.1]).type()
a
'torch.FloatTensor'
设置默认类型,可以通过 set_default_tensor_type 来设置默认 tensor 数据类型。
torch.set_default_tensor_type(torch.DoubleTensor)
a = torch.tensor([1.2,2.1]).type()
a
'torch.DoubleTensor'
使用 rand 方法随机创建 tensor
可以调用 rand 方法来创建 tensor ,rand 创建的 tensor 是由一些随机数初始化而得,rand 方法接收一个指定 tensor 的形状的参数,下面创建一个 tensor。
torch.rand(2,3)
tensor([[7.1757e-01, 5.3697e-01, 1.2008e-05],
[7.0986e-01, 1.3878e-01, 2.7967e-01]])
使用 rand_like 方法创建 tensor
rand_like 和 rand 相比不同处,rand_like 接收一个 tensor 作为参数,rand_like 创建出的 tensor 会与输入 tensor 的形状一致。
torch.rand_like(a)
tensor([[0.6781, 0.5458, 0.8327, 0.8412, 0.3030],
[0.3547, 0.0281, 0.5317, 0.8594, 0.2613]])
# [min,max)
torch.randint(1,10,(2,3))
tensor([[7, 4, 1],
[6, 5, 9]])
# 正态分布,randn 表示均值为 0 方差为 1 的正态分布
torch.randn(3,3)
tensor([[ 1.0778, 0.3406, 0.8222],
[ 0.8307, 0.1610, -0.0403],
[-0.3309, -1.2102, -0.9363]])
# normal 函数提供自定义均值和方差来创建 tensor,例如我们想要创建 2 * 5 的正态分布,先创建 10 数,然后创建 10 均值,再创建 10 个方差
a = torch.normal(mean=torch.full([10],0),std=torch.arange(1,0,-0.1))
# full 函数创建 10 为 0 的 Tensor
# 使用 reshape 对形状进行变换
a = a.reshape(2,5)
a
tensor([[ 0.1704, -0.7479, -0.5759, -0.5080, -0.1951],
[ 0.1986, -0.1511, -0.1400, 0.0939, -0.1796]])
full 方法创建 tensor
full 方法接收两个参数,一个参数用于指定创建的 tensor 的形状,第二参数是填充 tensor 的数值。
torch.full([2,3],7)
tensor([[7., 7., 7.],
[7., 7., 7.]])
# 创建一个标量
torch.full([],7)
tensor(7.)
# 创建一个向量
torch.full([1],7)
tensor([7.])
arange 方法创建 tensor
arange 创建 tensor 接收一个区间,区间原则符合左闭右开原则,[)
torch.arange(0,10)
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 第三个参数表示步进
torch.arange(0,10,2)
tensor([0, 2, 4, 6, 8])
等分方法创建 tensor
linspace 是在一个指定区间进行等分,例如 steps 对区间进行 steps 等分。
torch.linspace(0,10,steps=4)
tensor([ 0.0000, 3.3333, 6.6667, 10.0000])
torch.linspace(0,10,steps=10)
tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,
8.8889, 10.0000])
torch.linspace(0,10,steps=11)
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
torch.logspace(0,-1,steps=10)
tensor([1.0000, 0.7743, 0.5995, 0.4642, 0.3594, 0.2783, 0.2154, 0.1668, 0.1292,
0.1000])
torch.logspace(0,1,steps=10)
tensor([ 1.0000, 1.2915, 1.6681, 2.1544, 2.7826, 3.5938, 4.6416, 5.9948,
7.7426, 10.0000])
其他方法创建 tensor
- ones 方法创建一个全部都是 1 的 tensor,ones 接收参数用于指定 tensor 形状
- zeros 方法创建一个全部都是 0 的 tensor,ones 接收参数用于指定 tensor 形状
- eye 方法创建一个对角线全部都是 1 的,其他位置都是 0 的 tensor,ones 接收参数用于指定 tensor 形状
torch.ones(3,3)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
torch.zeros(3,3)
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
torch.eye(3,2)
tensor([[1., 0.],
[0., 1.],
[0., 0.]])
torch.eye(3)
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
a = torch.zeros(3,3)
torch.ones_like(a)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
# 随机打散 [0,10)
torch.randperm(10)
tensor([1, 3, 0, 2, 9, 5, 6, 7, 4, 8])
a = torch.rand(2,3)
b = torch.rand(2,2)
idx = torch.randperm(2)
idx
tensor([0, 1])
a[idx]
tensor([[0.9753, 0.6958, 0.7198],
[0.9417, 0.3410, 0.9740]])
b[idx]
tensor([[0.0652, 0.7491],
[0.9848, 0.7652]])