← 返回首页
张量
发表时间:2025-03-15 13:36:48
张量

PyTorch 中的基本运算的数据类型,实际上就是一个多维数组,并且可以看作是 Numpy 的翻版实现。

1.Tensor张量

PyTorch最基本的操作对象是Tensor(张量)。张量是一个数字容器,同时也是定义张量转换来生成新张量的一组规则。在数学中标量是只有大小没有方向的量;向量时有大小和方向的量;矩阵是由多个向量组成的。在PyTorch中标量就可以视为零阶的张量,向量可以视为一阶张量,矩阵就是二阶张量。

2.常规创建

import torch

# 直接创建 Tensor 类
mat = torch.Tensor([[1, 2], [3, 3]])
print(mat)
# 通过函数创建
mat = torch.tensor([[1, 2], [3, 3]])
print(mat)
# 单位阵
matE = torch.eye(5, 5)
print(matE)
# 创建全1.全0
matOne = torch.ones(2, 3)
print(matOne)
matZore = torch.zeros(2, 3)
print(matZore)
# 随机创建
mat = torch.rand(6, 6)
print(mat)
mat = torch.randn(5, 4)
print(mat)
# 不同位置的元素,指定不同的正态分布
mat = torch.normal(mean=torch.ones((3, 4)), std=10 * torch.rand((3, 4)))
print(mat)
# 根据现有的矩阵的形状,进行创建
matOne = torch.ones_like(mat)
matZore = torch.zeros_like(mat)
mat = torch.randn_like(mat)
mat = torch.rand_like(mat)
# 生成序列
vec = torch.arange(0, 15, 1)
print(vec)
vec = torch.linspace(0, 15, 31)
print(vec)
# 生成随机索引序列
vec = torch.randperm(16)
print(vec)

3.稀疏矩阵

稀疏矩阵: 为了节省存储空间,稀疏矩阵通常只存储非零元素及其位置。 稠密矩阵: 稠密矩阵需要存储所有元素,因此内存使用量较大。

import torch

# 创建一个稀疏矩阵
# 维度索引
index = torch.tensor([[1,3,6],[2,4,6]])
# 值
value = torch.tensor([1,2,3])
# 稀疏矩阵
matSpares = torch.sparse_coo_tensor(index, value,(8,7))
print(matSpares)
# 将稀疏矩阵转为稠密形式
mat = matSpares.to_dense()
print(mat)

4.属性

import torch
mat = torch.tensor([[1,2,3],[4,5,6]],dtype=torch.float,device=torch.device('cuda'))
# 形状
print(mat.shape)
# 数据类型
print(mat.dtype)
# 存储位置
print(mat.device)
# 矩阵形式:稀疏还是稠密
print(mat.layout)
# 真正的数据
print(mat.data)

5.四则运算

import torch
a = torch.tensor([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
b = torch.tensor([[3, 4, 5], [1, 2, 3], [6, 7, 8]])
c = a + b
print(c)
c = a / b
print(c)
c = a - b
print(c)
c = a * b
print(c)

6.张量乘法

import torch
# 二维矩阵
a = torch.tensor([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
b = torch.tensor([[3, 4, 5], [1, 2, 3], [6, 7, 8]])
# 矩阵运算
c = a @ b
print(c)
c = torch.mm(a,b)
print(c)
c = torch.matmul(a, b)
print(c)

7.范数

在 PyTorch 中,范数(Norm)是一个非常重要的概念,用于衡量张量(Tensor)的大小或长度。范数在机器学习和深度学习中有多种应用,例如正则化、优化和评估模型性能等。

常见的范数类型 - L0 范数:计算张量中非零元素的个数。 - L1 范数(曼哈顿范数):计算张量中所有元素的绝对值之和。 - L2 范数(欧几里得范数):计算张量中所有元素的平方和的平方根。 - 无穷范数(最大范数):计算张量中绝对值最大的元素的值。

计算指定维度的 L1 范数:

import torch
#创建一个二维张量
tensor = torch.tensor([[1.0, 2.0], [3.0, 4.0]])

# 计算每一行的 L1 范数
l1_norm_rows = torch.norm(tensor, p=1, dim=1)
print("每一行的 L1 范数:", l1_norm_rows)

# 计算每一列的 L1 范数
l1_norm_cols = torch.norm(tensor, p=1, dim=0)
print("每一列的 L1 范数:", l1_norm_cols)

计算整个张量的 L2 范数:

import torch

# 创建一个张量
tensor = torch.tensor([1.0, 2.0, 3.0, 4.0])

# 计算 L2 范数
l2_norm = torch.norm(tensor, p=2)
print("L2 范数:", l2_norm)

计算无穷范数:

import torch
# 创建一个张量
tensor = torch.tensor([1.0, -2.0, 3.0, -4.0])

# 计算无穷范数
inf_norm = torch.norm(tensor, p=float('inf'))
print("无穷范数:", inf_norm)

8.clamp

将 tensor 值限制在某个区间内:

torch.clamp(tensor,min,max)
import torch

a = torch.tensor([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
a = a.clip(min=0, max=4)
print(a)

9.dim

张量维度的编号,从 0 开始。在函数中,设定维度编号,就表示对哪个维度进行操作。

import torch

A = torch.tensor([[1,2,3],[4,5,6]])
print(A.shape)
# 对矩阵进行列相加。也就是对第 1 维度进行加和操作
print(torch.sum(A, dim=1))
# 对矩阵进行行相加。也就是对第 0 维度进行加和操作
print(torch.sum(A, dim=0))

10.in-place

直接在 tensor 进行修改,不产生中间结果。所有运算函数名末尾带_的都是。

import torch

a = torch.tensor([[1, 2, 3], [3, 4, 5], [6, 7, 8]])
a.clip(min=0, max=4)
print(a)

a.clip_(min=0, max=4)
print(a)

11.广播机制

PyTorch 和 NumPy 的广播机制在规则上几乎完全一致。

作用: 当两个张量不能满足上述运算的维度关系时,对于特定条件的维度,可以根据「广播机制」进行维度扩充,进而实现对应计算。

广播条件:

import torch
a = torch.tensor([2,3,1])
b = torch.tensor(4)
c = a + b

print(c)

注意和以下代码的区别:

import torch
a = torch.tensor([2,3,1])
b = torch.tensor([4])
c = a + b

print(c)