【知识】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])

相关推荐
测试员周周2 分钟前
【AI测试智能体-面试】AI测试面试60题(附回答思路)
人工智能·python·功能测试·测试工具·单元测试·自动化·测试用例
用户83562907805121 分钟前
使用 Python 操作 Word 评论和回复
后端·python
ShyanZh29 分钟前
【skill】Humanizer-zh:24条规则消灭AI写作痕迹
人工智能·ai写作·skill
电商软件开发 小银31 分钟前
思域不再安全?AI+独立APP破局指南
人工智能·软件开发·数字化转型·商业模式·超级app·商业思维·ai 矩阵运营
asyxchenchong88837 分钟前
最新Hermes Agent 技能封装与科研自动化:以 Meta-Analysis 为例-实现从文献检索到绘图的一站式工作流
运维·人工智能·自动化
武子康38 分钟前
调查研究-168 MiroFish 本地化部署分析:主仓库、Zep Cloud、离线 Fork 与真正可控的多智能体沙盘
人工智能·aigc·openai
Zella折耳根39 分钟前
复习篇-继承和接口
java·开发语言·python
诗词在线43 分钟前
求推荐飞花令
大数据·人工智能·python
云烟成雨TD1 小时前
Spring AI 1.x 系列【47】 MCP Annotations 模块
java·人工智能·spring
心枢AI研习社1 小时前
我问了claude目前最强大的模型fable 5这个问题?
人工智能·agent·claude