热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

pytorch–基于索引的操作

pytorch–基于索引的操作原文:https://www.

py torch–基于索引的操作

原文:https://www . geesforgeks . org/py torch-基于索引的操作/

PyTorch 是脸书开发的 python 库,用于运行和训练深度学习和机器学习算法。张量是机器或深度学习算法的基本数据结构,为了处理它们,我们执行几个操作,PyTorch 库为此提供了许多功能。

为复制、添加、填充值/张量而在某一特定行或列上处理索引的张量操作被称为基于索引开发的操作。PyTorch 中有两种基于索引的操作,一种是原地操作,另一种是异地操作。两者之间的基本区别在于,原位操作直接改变张量的值,而不复制张量的值,而非原位操作则不会。以下是操作:-


  1. 索引 _ 添加 _

  2. 索引 _ 添加

  3. 索引 _ 复制 _

  4. 索引 _ 复制

  5. 索引 _ 填充 _

  6. 索引 _ 填充

  7. 索引放入

  8. 索引 _ put

  9. 索引选择

现在我们用适当的例子讨论每一个函数。

1。index_add_: 沿着矩阵中给定的顺序,将给定的张量元素加到自张量上。

语法:

index_add_(dim,index,ensor)---> Tensor

参数:


  • Dim: along which indicator the dimension is added. "0" represents a column and "1" represents a row.

  • Index: Alternative tensor index. It can be longteng sensor or inttensor .

  • Tensor: Tensor containing the values to be added.

例 1: 我们取一个零向量‘x’, te 大小为(3,5)的张量和指数张量。沿着行累加结果向量,我们得到输出。

Python 3


#importing libraries
import torch
x=torch.zeros(5,5)
te=torch.tensor([[1,3,5,7,9],[1,3,5,7,9],[1,3,5,7,9]],dtype=torch.float32)
index0=torch.tensor([0,2,4])
#adding tensor te to x along row of the given order
x.index_add_(0,index0,te)

输出:

tensor([[1., 3., 5., 7., 9.],
[0., 0., 0., 0., 0.],
[1., 3., 5., 7., 9.],
[0., 0., 0., 0., 0.],
[1., 3., 5., 7., 9.]])

例 2:

Python 3


#importing libraries
import torch
y=torch.ones(5,5)#unitvector
index2=torch.tensor([0,1,1,1,2])
ten=torch.randn(1,5)
#adding values to y along the column with given order
y.index_add_(1,index2,ten)

输出:

tensor([[0.9460, 0.4762, 1.2219, 1.0000, 1.0000],
[0.9460, 0.4762, 1.2219, 1.0000, 1.0000],
[0.9460, 0.4762, 1.2219, 1.0000, 1.0000],
[0.9460, 0.4762, 1.2219, 1.0000, 1.0000],
[0.9460, 0.4762, 1.2219, 1.0000, 1.0000]])

2.index_add: 是上述功能的不合适版本。这会给自身张量临时添加一个给定的张量。参数和语法与上面相同。

例 3:

Python 3


import torch
y=torch.ones(5,5)
index2=torch.tensor([0,1,1,1,2])
ten=torch.randn(1,5)
print("Indexed Matrix:\n",y.index_add(1,index2,ten))
print ("Printing Indexed Matrix again:\n",y)

输出:

Indexed Matrix:
tensor([[-0.2811, -1.0776, 2.2697, 1.0000, 1.0000],
[-0.2811, -1.0776, 2.2697, 1.0000, 1.0000],
[-0.2811, -1.0776, 2.2697, 1.0000, 1.0000],
[-0.2811, -1.0776, 2.2697, 1.0000, 1.0000],
[-0.2811, -1.0776, 2.2697, 1.0000, 1.0000]])
Printing Indexed Matrix again:
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])

3.index_copy_: 通过按照“索引”中给出的顺序选择索引,将给定张量的元素复制到输入张量。

语法:

index_copy_(dim,index,tensor)---> Tensor

参数:


  • Dim: along which dimension the index is copied. It is in' int' format.

  • Index: An optional tensor index. It can be int tensor or [T2】 longtersenter.

  • Tensor: Tensor containing the value to be copied.

例 4: 这里“a”的元素被“T1”张量替换,替换顺序在“index3”中给出。

Python 3


#importing libraries
import torch
a=torch.ones(4,4)#unit vector
t1=torch.randn(2,4)
index3=torch.tensor([1,3])
#copying elements of t1 ensor to 'a' in given order of index
a.index_copy_(0,index3,t1)

输出:

tensor([[ 1.0000, 1.0000, 1.0000, 1.0000],
[-0.1918, -1.2089, 0.3229, -0.1831],
[ 1.0000, 1.0000, 1.0000, 1.0000],
[ 0.7521, 0.8424, -0.8698, -0.3908]])

示例 5: 在下面的示例中,我们将收到一个错误。这是因为张量的第三个维度不等于指数的长度。所以我们必须记住张量的第维度必须与索引的长度具有相同的大小。

Python 3


#importing libraries
import torch
y=torch.ones(5,5)
index1=torch.tensor([0,1,2,3,4])
te=torch.tensor([[1,3,5,7,9],[1,3,5,7,9],[1,3,5,7,9]],dtype=torch.float32)
y.index_copy_(1,index1,te)

输出:

RuntimeError Traceback (most recent call last)
<ipython-input-8-25e4150d5bd7> in <module>
1 y=torch.ones(5,5)
2 index1=torch.tensor([0,1,2,3,4])
----> 3 y.index_copy_(1,index1,te)
RuntimeError: index_copy_(): Source/destination tensor must have same slice shapes.
Destination slice shape: 5 at dimension 1 and source slice shape: 3 at dimension 0.

示例 6: 在本例中,我们将把给定的张量复制到自张量,记住张量的 dim th 维度必须与索引的长度具有相同的大小。

Python 3


import torch
b=torch.ones(4,4)
t2=torch.randn(4,2)
index4=torch.tensor([0,1])
b.index_copy_(1,index4,t2)

输出:

tensor([[-0.3964, -0.3859, 1.0000, 1.0000],
[ 2.6910, -0.9394, 1.0000, 1.0000],
[ 0.3591, -0.2262, 1.0000, 1.0000],
[ 1.2102, -0.8340, 1.0000, 1.0000]])

4.index_copy: 这是用给定张量替换输入张量元素的基于错位索引的操作。语法、参数同上。

5 . index _ fill _:“Val”值用“x”的元素以及向量“index”中给出的索引顺序填充。

语法:

index_fill_(dim, index, val) Tensor

参数:


  • Dim: The dimension along which it is filled. It is in' int' format.

  • Index: Fill in according to the index order given by this index vector. It can be IntTensor or LongTensor.

  • Val(float): the value to be filled in.

示例 7: 在这个示例中,我们将声明一个包含随机元素的张量,然后用‘4’以及给定的索引填充它。

Python 3


#importing libraries
import torch
c=torch.randn(4,4)
index5=torch.tensor([0,2])
#filling 4 within the elements of the tensor 'c' along the indices 0,2
c.index_fill_(0,index5,4)
print(c)

输出:

tensor([[ 4.0000, 4.0000, 4.0000, 4.0000],
[ 0.4762, 0.0056, 0.3258, 1.1345],
[ 4.0000, 4.0000, 4.0000, 4.0000],
[-0.1490, -0.6543, 0.9755, 1.8087]])

示例 8: 类似地,我们对列执行填充操作。

Python 3


d=torch.randn(5,5)
d.index_fill(1,index5,2)
print(d)

输出:

tensor([[ 0.5978, -1.2461, -0.8794, -1.0175, 0.8938],
[-0.6374, 1.0848, 0.1291, 0.6658, 0.3081],
[-0.9686, -0.8212, -0.5223, -0.3208, -1.7718],
[-0.1153, -1.2552, -0.4119, -1.1293, 0.2266],
[ 1.2610, 0.2618, -1.5528, 0.7805, 1.3730]])

6。index_fill: 这是基于索引的不合适的操作,用“val”填充张量元素。语法、参数同上。

7.index_put_: 该操作使用给定“索引”的索引将“val”的值放入自张量。

语法:

index_put_(indices, values, accumulate=False) Tensor

参数:


  • Index: It is the tuple of Longtengsen, which is used to index yourself.

  • Value: Tensor with the value to be put into the target.

  • Accumulate: whether to accumulate.

例 9: 这里我们取目标向量,替换

中提到的值张量的值


#importing libraries
import torch
target=torch.zeros([4,4])
indices = torch.LongTensor([[0,1],[1,2],[3,1],[1,0]])#indices to which values to be put
value = torch.ones(indices.shape[0])
#tuple of the index tensor is passed along with the value
target.index_put_(tuple(indices.t()), value)

输出:

tensor([[0., 1., 0., 0.],
[1., 0., 1., 0.],
[0., 0., 0., 0.],
[0., 1., 0., 0.]])

注意:我们必须进行指数张量的转置,否则会出现错误。

示例 10: 在本例中,我们将累加函数保持为 true,这意味着值中的元素被添加到目标中。

Python 3


e=torch.ones([4,4])
indices2=torch.LongTensor([[0,1],[0,1],[2,1]])
value2=torch.zeros(indices2.shape[0])
e.index_put_(tuple(indices2.t()),value2,accumulate=True)


Output:
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])

8。index_fill: 这是 index_fill_ 的不合适版本。语法、参数同上。

9。 index_select: 通过从目标张量中进行选择,可以返回一个带有上述索引的张量。

语法:

torch.index_select(input, dim, index, out=None)

参数:


  • Input (Tensor): Tensor from which the index will be selected.

  • Dimension (int): The dimension selected along it.

  • Index: An index containing an index.

示例 11: 我们沿着 dim=0(即行)选取张量‘m’的 0,1 个索引,并打印输出。

Python 3


#importing libraries
import torch
m=torch.randn(3,4)
print('Original matrix:\n',m)
indices=torch.tensor([0,1])
print("Indexed Matrix:\n",torch.index_select(m, 0, indices))

输出:

Original matrix:
tensor([[ 0.2008, -0.2637, 2.1216, -0.2892],
[-0.4059, -1.6054, -2.5022, -0.2912],
[-0.3246, 0.4751, -0.1018, -0.6707]])
Indexed Matrix:
tensor([[ 0.2008, -0.2637, 2.1216, -0.2892],
[-0.4059, -1.6054, -2.5022, -0.2912]])

参考:T2】https://pytorch.org/docs/stable/tensors.html


推荐阅读
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • Day2列表、字典、集合操作详解
    本文详细介绍了列表、字典、集合的操作方法,包括定义列表、访问列表元素、字符串操作、字典操作、集合操作、文件操作、字符编码与转码等内容。内容详实,适合初学者参考。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
  • python限制递归次数(python最大公约数递归)
    本文目录一览:1、python为什么要进行递归限制 ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • Java如何导入和导出Excel文件的方法和步骤详解
    本文详细介绍了在SpringBoot中使用Java导入和导出Excel文件的方法和步骤,包括添加操作Excel的依赖、自定义注解等。文章还提供了示例代码,并将代码上传至GitHub供访问。 ... [详细]
author-avatar
广大华软11级软测1班支部
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有