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 进行深度学习的基础,掌握它们将有助于你更高效地开发和训练神经网络模型。

相关推荐
Navicat中国1 小时前
Navicat 18 内置 AI 助手让数据库开发从繁琐操作迈向智能协作
大数据·人工智能·数据库开发
4SAPI1 小时前
多模型请求路由怎么设计?成本、速度和能力如何平衡
人工智能·python·gateway·php
魔乐社区2 小时前
翼芯・智算测试与适配优化专区,让国产算力“选得准、跑得通、用得好“
人工智能·开源·大模型
XCDoris3 小时前
郴州logo设计机构
大数据·人工智能·python
Goboy4 小时前
一个人带三个Agent,我用全新TRAE从0到1搭了个资讯周刊站
人工智能·trae
俊哥V4 小时前
每日 AI 研究简报 · 2026-10-19
人工智能·ai
AI_AGENT_DEV_AI4 小时前
AI英语教培智能体的开发
人工智能
物联网软硬件开发-轨物科技4 小时前
【技术白皮书】光伏清检一体化机器人系统解决方案技术白皮书
大数据·运维·网络·人工智能·机器人
gCode Teacher 格码致知4 小时前
Pandas教学-17:Series与Dataframe的关系
开发语言·python·pandas
海宇数据4 小时前
零信任架构实战:基于海宇网上踪迹欺诈行为识别构建自动化信贷资信评估网关
运维·人工智能·架构·自动化