当前位置: 首页 > article >正文

PyTorch 中 12 种张量操作详解

创作不易,还请各位同学三连点赞!!收藏!!转发!!!

对于刚入门学习Python还找不到方向的小伙伴可以试试我的这份学习方法和籽料,免费自取!!

PyTorch 是一个强大的深度学习框架,它允许开发者轻松地定义和训练神经网络。张量是 PyTorch 的核心数据结构,类似于 NumPy 数组,但支持自动微分以及在 GPU 上加速计算。本文将详细介绍 PyTorch 中常用的 12 种张量操作,帮助你更好地理解和使用这个工具。

1. 创建张量

首先,我们需要安装 PyTorch 并导入必要的库。

# 安装 PyTorch  
!pip install torch  
  
# 导入 PyTorch 库  
import torch  

创建张量是最基本的操作之一。你可以从 Python 列表或 NumPy 数组中创建张量。

# 从列表创建张量  
tensor_from_list = torch.tensor([1, 2, 3])  
print(tensor_from_list)  # 输出: tensor([1, 2, 3])  
  
# 从 NumPy 数组创建张量  
import numpy as np  
numpy_array = np.array([1, 2, 3])  
tensor_from_numpy = torch.from_numpy(numpy_array)  
print(tensor_from_numpy)  # 输出: tensor([1, 2, 3])  

2. 查看张量形状

了解张量的形状对于处理数据非常重要。

# 创建一个 2x3 的矩阵  
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])  
print(matrix.shape)  # 输出: torch.Size([2, 3])  

3. 转置张量

转置可以改变张量的维度顺序。

# 创建一个 2x3 的矩阵  
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])  
transposed_matrix = matrix.t()  
print(transposed_matrix)  # 输出:  
# tensor([[1, 4],  
#         [2, 5],  
#         [3, 6]])  

4. 拆分张量

拆分张量可以帮助你在不同维度上分割数据。

# 创建一个 3x4 的矩阵  
matrix = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])  
split_tensors = torch.split(matrix, split_size=2, dim=1)  
for t in split_tensors:  
    print(t)  
# 输出:  
# tensor([[ 1,  2],  
#         [ 5,  6],  
#         [ 9, 10]])  
# tensor([[ 3,  4],  
#         [ 7,  8],  
#         [11, 12]])  

5. 拼接张量

拼接操作可以将多个张量合并成一个更大的张量。

# 创建两个 2x2 的矩阵  
matrix1 = torch.tensor([[1, 2], [3, 4]])  
matrix2 = torch.tensor([[5, 6], [7, 8]])  
concatenated_tensor = torch.cat((matrix1, matrix2), dim=0)  
print(concatenated_tensor)  # 输出:  
# tensor([[1, 2],  
#         [3, 4],  
#         [5, 6],  
#         [7, 8]])  

6. 张量索引

索引操作允许你选择张量中的特定元素或子集。

# 创建一个 2x3 的矩阵  
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])  
element = matrix[0, 1]  
print(element)  # 输出: tensor(2)  
  
sub_matrix = matrix[1, :]  
print(sub_matrix)  # 输出: tensor([4, 5, 6])  

7. 张量切片

切片可以让你选择张量的一部分。

# 创建一个 2x3 的矩阵  
matrix = torch.tensor([[1, 2, 3], [4, 5, 6]])  
slice_tensor = matrix[:, 1:]  
print(slice_tensor)  # 输出:  
# tensor([[2, 3],  
#         [5, 6]])  

8. 张量广播

广播是一种机制,允许你执行不同形状的张量之间的操作。

# 创建一个 1x3 的向量和一个标量  
vector = torch.tensor([1, 2, 3])  
scalar = torch.tensor(2)  
  
# 将向量乘以标量  
broadcasted_tensor = vector * scalar  
print(broadcasted_tensor)  # 输出: tensor([2, 4, 6])  

9. 张量相加

相加操作用于将两个张量对应位置的元素相加。

# 创建两个 2x2 的矩阵  
matrix1 = torch.tensor([[1, 2], [3, 4]])  
matrix2 = torch.tensor([[5, 6], [7, 8]])  
  
# 相加  
sum_tensor = matrix1 + matrix2  
print(sum_tensor)  # 输出:  
# tensor([[ 6,  8],  
#         [10, 12]])  

10. 张量乘法

乘法操作可以用于点积或矩阵乘法。

# 创建两个 2x2 的矩阵  
matrix1 = torch.tensor([[1, 2], [3, 4]])  
matrix2 = torch.tensor([[5, 6], [7, 8]])  
  
# 点积  
dot_product = torch.dot(matrix1.view(-1), matrix2.view(-1))  
print(dot_product)  # 输出: tensor(70)  
  
# 矩阵乘法  
matrix_product = torch.matmul(matrix1, matrix2)  
print(matrix_product)  # 输出:  
# tensor([[19, 22],  
#         [43, 50]])  

11. 张量归一化

归一化可以将张量的值调整到特定范围内。

# 创建一个 1x3 的向量  
vector = torch.tensor([1, 2, 3])  
  
# 归一化  
normalized_vector = torch.nn.functional.normalize(vector, p=2, dim=0)  
print(normalized_vector)  # 输出: tensor([0.2673, 0.5345, 0.8018])  

12. 张量随机初始化

随机初始化在神经网络训练中非常重要。

# 随机初始化一个 2x3 的矩阵  
random_matrix = torch.randn(2, 3)  
print(random_matrix)  # 输出类似:  
# tensor([[ 1.0431, -0.1827, -0.2591],  
#         [-0.2442, -0.3353,  0.4927]])  

总结

本文详细介绍了 PyTorch 中常用的 12 种张量操作,包括创建张量、查看张量形状、转置张量、拆分张量、拼接张量、张量索引、张量切片、张量广播、张量相加、张量乘法、张量归一化和张量随机初始化。这些操作是使用 PyTorch 进行深度学习的基础,掌握它们将有助于你更高效地开发和训练神经网络模型。


http://www.kler.cn/news/360004.html

相关文章:

  • 7、Vue2(三) element-ui+less
  • 15分钟学 Go 第 13 天:数组
  • springboot 读取配置的方式
  • 基于Lambda架构的大数据处理详解
  • 洛谷刷题 P1008 [NOIP1998 普及组] 三连击
  • 深入剖析CAS:无锁并发编程的核心机制与实际应用
  • MySQL 的系统函数
  • 快速了解Python流程控制语句基本使用
  • 【C++】在Windows中使用Boost库——实现TCP、UDP通信
  • Moectf-week1-wp
  • ENSP环回路由的配置
  • 深度学习基础—神经风格迁移
  • PCL 基于中值距离的点云对应关系(永久免费版)
  • 我常用的两个单例模式写法 (继承Mono和不继承Mono的)
  • 通过SSH远端免密登录执行脚本,修改最新5分钟生成文件权限
  • 离散数学 第二讲 特殊集合和集合间关系 笔记 [电子科大]王丽杰
  • uniapp 常用的地区行业各种多选多选,支持回显,复制粘贴可使用
  • 使用HIP和OpenMP卸载的Jacobi求解器
  • Netty笔记
  • 【论文学习与撰写】,论文word文档中出现乱码的情况,文档中显示的乱码,都是英文字母之类的,但打印预览是正常的