PyTorch 中 12 种张量操作详解

ops/2024/10/22 7:44:20/

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

对于刚入门学习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.ppmy.cn/ops/127507.html

相关文章

Ajax:跨域、防抖和节流、HTTP协议

在善意的“双向奔赴”中,每个普通人都如星辰,微小但释放着自己的光芒,交织成灿烂的星河 文章目录 跨域防抖和节流HTTP协议HTP状态码以及代表意义错误代码的影响移动的小天使 跨域 同源策略 概念:协议,域名&#xff0c…

Go的客户端与服务器通信案例

客户端代码 package main import ("fmt""net""os""bufio" )func main(){fmt.Println("客户端启动……")conn,err:net.Dial("tcp","127.0.0.1:8888")if err!nil{fmt.Println("客户端连接服务器失败…

WEB前端使用标签制作网页

需要使用HTML的一些基本标签制作网页 基本代码如下: <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title></head><body><form action"#" method"post" enctype"text/…

大数据-178 Elasticsearch Query - Java API 索引操作 文档操作

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

git 免密的方法

方法一&#xff1a; 通过生成credential配置 git config --global credential.helper store 查看.gitconfig文件&#xff0c;发现多了一行 [credential] helper store 方法二&#xff1a; 修改仓库中.git/config文件 url http://账号:密码git.test.com.cn/test/xx.git或者带…

动态规划-子数组系列——413.等差数列划分

1.题目解析 测试用例&#xff1a;413.等差数列划分——力扣 测试用例 2.算法原理 1.状态表示 dp[i]:以第i个位置为结尾的子数组中等差数列的个数 2.状态转移方程 当遇到新的数可以与原来的等差数列构成等差数列说明此时多了一种等差数列的情况则dp[i]dp[i-1]1;反之不能构成等…

抓取指定网站上的所有图片的Python脚本

引言 在当今信息爆炸的时代&#xff0c;互联网上的数据量呈现出指数级的增长。对于开发者、数据分析师以及研究人员而言&#xff0c;从网页中提取有价值的信息是一项至关重要的技能。其中&#xff0c;抓取网站上的图片资源不仅能够丰富我们的数据集&#xff0c;还能为各种应用…

c++面试八股

* 基类、派生类、成员对象构造函数调用顺序 构造时&#xff1a;基类构造函数[a1,a2]→对象成员构造函数[声明顺序 a,b]→派生类本身的构造函数c 析构时&#xff1a;派生类本身的析构函数c→对象成员析构函数[声明反序b,a]→基类析构函数[a2,a1] #include <iostrea…