【知识】Pytorch中基于索引的操作

转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn]

如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~

目录

[1. index_add_](#1. index_add_)

2.index_add

3.index_copy_

4.index_copy

5.index_fill_

[6. index_fill](#6. index_fill)

7.index_put_

[8. index_fill](#8. index_fill)

[9. index_select](#9. index_select)


Pytorch - Index-based Operation - GeeksforGeeks

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

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

  1. index_add_
  2. index_add
  3. index_copy_
  4. index_copy
  5. index_fill_
  6. index_fill
  7. index_put_
  8. index_put
  9. index_select

1. index_add_

将给定的张量元素沿着矩阵中给定的顺序添加到自张量中。

python 复制代码
 index_add_(dim,index,ensor)---> Tensor

Parameters: 参数:

  • dim:要添加索引的维度。"0"代表列,"1"代表行。
  • index:要选择的张量索引。它可以是 LongTensorIntTensor
  • tensor:包含要添加的值的张量。
python 复制代码
#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.]])

python 复制代码
#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

它是上述函数的out-of-place 版本。这会暂时将给定的张量添加到张量中。参数和语法与上述相同。

python 复制代码
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' 中给定的顺序选择索引,将给定张量的元素复制到输入张量。

python 复制代码
index_copy_(dim,index,tensor)---> Tensor
  • dim:要复制的索引的维度。它是"int"格式。
  • index:要选择的张量索引。它可以是 IntTensorLongTensor。
  • Tensor:包含要复制的值的张量。
python 复制代码
#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]])

python 复制代码
#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.

python 复制代码
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

这是out-of-place 的基于索引的操作,用于用给定张量替换输入张量的元素。语法、参数同上。

5.index_fill_

'Val' 值填充了 'x' 的元素以及向量 'index' 中给出的索引顺序。

python 复制代码
index_fill_(dim, index, val) → Tensor
  • dim:要填充的维度。它是"int"格式。
  • index:按此索引向量中给出的索引的顺序填充。它可以是 IntTensor 或 LongTensor。
  • val(float):用于填充的值。
python 复制代码
#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]])

python 复制代码
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

这是out-of-place 的基于索引的操作,用于用"val"填充张量元素。语法、参数同上。

7.index_put_

此操作使用给定 'index' 的索引将 'val' 的值放入自张量中。

python 复制代码
index_put_(indices, values, accumulate=False) → Tensor
  • indices:它是 LongTensor 的元组,用于索引到 self。
  • values:具有需要放入目标中的值的张量。
  • accumulate:是否积累。
python 复制代码
#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.]])

**注意:**我们必须对索引张量进行转置,否则会发生错误。

python 复制代码
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_out-of-place 版本。语法、参数同上。

9. index_select

通过从目标张量中进行选择,返回一个张量和上述索引。

python 复制代码
torch.index_select(input, dim, index, out=None) 
  • input(Tensor):将从中选择索引的张量。
  • dim(int):要选择的维度。
  • index:它包含索引的索引。
python 复制代码
#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]])

相关推荐
天下无敌笨笨熊41 分钟前
PyQT开发总结
python·pyqt
趣味科技v41 分钟前
2024外滩大会:机器人汽车飞机都来了
人工智能·机器人·汽车
基算仿真44 分钟前
基于sklearn的机器学习 — KNN
人工智能·机器学习·sklearn
机器学习Zero1 小时前
让效率飞升的秘密武器:解锁编程高效时代的钥匙
git·python·github·aigc
wjcroom1 小时前
celery-APP在windows平台的发布方法(绿色免安装exe可搭配eventlet)
windows·python·celery
AI让世界更懂你2 小时前
漫谈设计模式 [5]:建造者模式
python·设计模式·建造者模式
芯语新知2 小时前
半导体芯闻--20240913
人工智能·科技·智能手机·电脑·显示器·平板
FutureUniant2 小时前
GitHub每日最火火火项目(9.13)
人工智能·python·计算机视觉·github·音视频
liugddx2 小时前
使用 BentoML快速实现Llama-3推理服务
人工智能·ai
ai_xiaogui2 小时前
【AIStarter:AI绘画、设计、对话】零基础入门:Llama 3.1 + 千问2快速部署
人工智能·深度学习