目录
PyTorch 是 Facebook 开发的一个 Python 库,用于运行和训练深度学习和机器学习算法。张量是机器或深度学习算法的基本数据结构,为了处理它们,我们执行了几个操作,PyTorch 库为此提供了许多功能。
处理对某些特定行或列的索引以复制、添加、填充值/张量的张量操作被称为基于索引的开发操作。PyTorch 中有两种类型的基于索引的操作,一种是就地操作,另一种是就地操作。两者之间的基本区别在于:就地操作直接更改张量的值,而不复制张量的值,而异地操作则不会。以下是操作:
- index_add_
- index_add
- index_copy_
- index_copy
- index_fill_
- index_fill
- index_put_
- index_put
- index_select
1. index_add_
将给定的张量元素沿着矩阵中给定的顺序添加到自张量中。
index_add_(dim,index,ensor)---> Tensor
Parameters: 参数:
- dim:要添加索引的维度。“0”代表列,“1”代表行。
- index:要选择的张量索引。它可以是 LongTensor 或 IntTensor。
- tensor:包含要添加的值的张量。
#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)
#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)
2.index_add
它是上述函数的out-of-place版本。这会暂时将给定的张量添加到自张量中。参数和语法与上述相同。
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)
3.index_copy_
通过按照 'index' 中给定的顺序选择索引,将给定张量的元素复制到输入张量。
index_copy_(dim,index,tensor)---> Tensor
- dim:要复制的索引的维度。它是“int”格式。
- index:要选择的张量索引。它可以是 IntTensor 或 LongTensor。
- Tensor:包含要复制的值的张量。
#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)
#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)
import torch
b=torch.ones(4,4)
t2=torch.randn(4,2)
index4=torch.tensor([0,1])
b.index_copy_(1,index4,t2)
4.index_copy
这是out-of-place的基于索引的操作,用于用给定张量替换输入张量的元素。语法、参数同上。
5.index_fill_
'Val' 值填充了 'x' 的元素以及向量 'index' 中给出的索引顺序。
index_fill_(dim, index, val) → Tensor
- dim:要填充的维度。它是“int”格式。
- index:按此索引向量中给出的索引的顺序填充。它可以是 IntTensor 或 LongTensor。
- val(float):用于填充的值。
#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)
d=torch.randn(5,5)
d.index_fill(1,index5,2)
print(d)
6. index_fill
这是out-of-place的基于索引的操作,用于用“val”填充张量元素。语法、参数同上。
7.index_put_
此操作使用给定 'index' 的索引将 'val' 的值放入自张量中。
index_put_(indices, values, accumulate=False) → Tensor
- indices:它是 LongTensor 的元组,用于索引到 self。
- values:具有需要放入目标中的值的张量。
- accumulate:是否积累。
#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)
注意: 我们必须对索引张量进行转置,否则会发生错误。
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)
8. index_fill
这是 index_fill_ 的out-of-place版本。语法、参数同上。
9. index_select
通过从目标张量中进行选择,返回一个张量和上述索引。
torch.index_select(input, dim, index, out=None)
- input(Tensor):将从中选择索引的张量。
- dim(int):要选择的维度。
- index:它包含索引的索引。
#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))