笔记为自我总结整理的学习笔记,若有错误欢迎指出哟~
pytorch——Tensor
简介
Tensor,又名张量。它可以是一个数(标量)、一维数组(向量)、二维数组(矩阵)和更高维的数组(高阶数据)。Tensor和Numpy的ndarrays类似,但PyTorch的tensor支持GPU加速。
官方文档:
https://pytorch.org/docs/stable/tensors.html
import torch as t
t.__version__ # '2.1.0+cpu'
创建Tensor
创建方法 | 示例 | 输出 |
---|---|---|
通过给定数据创建张量 | torch.Tensor([1, 2, 3]) |
tensor([1., 2., 3.]) |
通过指定tensor的形状 | torch.Tensor(2, 3) |
tensor([[1.1395e+23, 1.6844e-42, 0.0000e+00],[0.0000e+00, 0.0000e+00, 0.0000e+00]]) |
使用torch.arange() 创建连续的张量 |
torch.arange(0, 10, 2) |
tensor([0, 2, 4, 6, 8]) |
使用torch.zeros() 创建全零张量 |
torch.zeros((3, 4)) |
tensor([[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]) |
使用torch.ones() 创建全一张量 |
torch.ones((2, 2)) |
tensor([[1., 1.], [1., 1.]]) |
使用torch.randn() 创建随机张量 |
torch.randn((3, 3)) |
tensor([[ 1.0553, -0.4815, 0.6344], [-0.7507, 1.3891, 1.0460], [-0.5625, 1.9531, -0.5468]]) |
使用torch.rand() 创建在0到1之间均匀分布的随机张量 |
torch.rand((3, 3)) |
tensor([[1, 6, 5], [2, 0, 4], [8, 5, 7]]) |
使用torch.randint() 创建在给定范围内的整数随机张量 |
torch.randint(low=0, high=10, size=(3, 3)) |
tensor([[0, 8, 9], [1, 8, 7], [4, 4, 4]]) |
使用torch.eye() 创建单位矩阵 |
torch.eye(5) |
tensor([[1., 0., 0., 0., 0.], [0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.], [0., 0., 0., 0., 1.]]) |
从Python列表或Numpy数组创建张量 | torch.tensor([1, 2, 3]) 或 torch.tensor(np.array([1, 2, 3])) |
tensor([1, 2, 3])或tensor([1, 2, 3], dtype=torch.int32) |
将整个张量填充为常数值 | torch.full((3, 3), 3.14) |
tensor([[3.1400, 3.1400, 3.1400], [3.1400, 3.1400, 3.1400], [3.1400, 3.1400, 3.1400]]) |
创建指定大小的空张量 | torch.empty((3, 3)) |
tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) |
创建长度为5的随机排列张量 | torch.randperm(5) |
tensor([1, 2, 0, 3, 4]) |
torch.Tensor( )和torch.tensor( )的区别
torch.Tensor( )
torch.Tensor([1, 2, 3])
的创建方式会根据输入的数据类型来确定张量的数据类型。
例如,如果输入的是整数列表,那么创建的张量将使用默认的数据类型 torch.float32
。这意味着即使输入的数据是整数,张的数据类型也会被转换为浮点数类型。
a = t.Tensor([1, 2, 3])
a
# tensor([1., 2., 3.])
torch.Tensor(1,2)
通过指定tensor的形状创建张量
a= t.Tensor(1,2) # 注意和t.tensor([1, 2])的区别
a.shape
# torch.Size([1, 2])
torch.tensor( )
torch.tensor([1, 2, 3])
的创建方式会根据输入的数据类型灵活地选择张量的数据类型。它可以接受各种数据类型的输入,包括整数、浮点数、布尔值等,并根据输入的数据类型自动确定创建张量使用的数据类型。
a = t.tensor([1, 2, 3])
a
# tensor([1, 2, 3])
tensor可以是一个数(标量)、一维数组(向量)、二维数组(矩阵)和更高维的数组(高阶数据)。
标量(scalar )
scalar = t.tensor(3.14)
print('scalar: %s, shape of sclar: %s' %(scalar, scalar.shape))
输出为:
scalar: tensor(3.1400), shape of sclar: torch.Size([])
向量(vector)
vector = t.tensor([1, 2, 3])
print('vector: %s, shape of vector: %s' %(vector, vector.shape))
输出为:
vector: tensor([1, 2, 3]), shape of vector: torch.Size([3])
矩阵(matrix)
matrix = t.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
matrix,matrix.shape
输出为:
(tensor([[0.1000, 1.2000],
[2.2000, 3.1000],
[4.9000, 5.2000]]), torch.Size([3, 2]))
常用Tensor操作
方法 | 描述 |
---|---|
tensor.view(*args) |
改变张量形状 |
tensor.reshape(*args) |
改变张量形状 |
tensor.size() |
返回张量形状 |
tensor.dim() |
返回张量维度 |
tensor.unsqueeze(dim) |
在指定维度上添加一个新的维度 |
tensor.squeeze(dim) |
压缩指定维度的大小为1的维度 |
tensor.transpose(dim0, dim1) |
交换两个维度 |
tensor.permute(*dims) |
重新排列张量的维度 |
tensor.flatten() |
展平所有维度 |
tensor.mean(dim) |
沿指定维度计算张量的平均值 |
tensor.sum(dim) |
沿指定维度计算张量的和 |
tensor.max(dim) |
沿指定维度返回张量的最大值 |
tensor.min(dim) |
沿指定维度返回张量的最小值 |
tensor.argmax(dim) |
沿指定维度返回张量最大元素的索引 |
tensor.argmin(dim) |
沿指定维度返回张量最小元素的索引 |
tensor.add(value) |
将标量加到张量中的每个元素 |
tensor.add(tensor) |
将另一个张量加到该张量 |
tensor.sub(value) |
将标量从张量中的每个元素减去 |
tensor.sub(tensor) |
从该张量中减去另一个张量 |
tensor.mul(value) |
将张量中的每个元素乘以标量 |
tensor.mul(tensor) |
将该张量与另一个张量相乘 |
tensor.div(value) |
将张量中的每个元素除以标量 |
tensor.div(tensor) |
将该张量除以另一个张量 |
调整tensor的形状
tensor.view
通过tensor.view方法可以调整tensor的形状,但必须保证调整前后元素总数一致。view不会修改自身的数据,返回的新tensor与原tensor共享内存,也即更改其中的一个,另外一个也会跟着改变。
a = t.arange(0, 6)
a.view(2, 3)
输出结果为:
tensor([[0, 1, 2],
[3, 4, 5]])
- 案例1
b = a.view(-1, 2) # 当某一维为-1的时候,会自动计算它的大小
b.shape # torch.Size([3, 2])
- 案例2
b = a.view(-1, 3) # 当某一维为-1的时候,会自动计算它的大小
b.shape # torch.Size([2,3])
tensor.squeeze与tensor.unsqueeze
tensor.squeeze(dim)
tensor.squeeze(dim)
方法用于压缩张量中指定维度大小为1的维度,即将大小为1的维度去除。如果未指定 dim
参数,则会去除所有大小为1的维度。
# 创建一个形状为 (1, 3, 1, 4) 的张量
x = torch.arange(12).reshape(1, 3, 1, 4)
print(x.shape) # 输出: torch.Size([1, 3, 1, 4])
# 使用 squeeze 去除大小为1的维度
y = x.squeeze()
print(y.shape) # 输出: torch.Size([3, 4])
# 指定 dim 参数去除指定维度大小为1的维度
z = x.squeeze(0)
print(z.shape) # 输出: torch.Size([3, 1, 4])
tensor.unsqueeze(dim)
tensor.unsqueeze(dim)
方法用于在指定维度上添加一个新的维度,新的维度大小为1。