简体   繁体   English

在某个索引处连接火炬张量

[英]Concatenate torch tensor at a certain index

I am looking to Concatenate 2 torch tensors at a certain index.我正在寻找以某个索引连接 2 个火炬张量。 As an example, I want to add b after a[1].例如,我想在 a[1] 之后添加 b。

a = torch.Tensor([1, 2, 3, 4, 5])
b = torch.Tensor([6, 7, 8, 9, 10])

The desired output is所需的 output 是

torch.Tensor([1, 2, 6, 7, 8, 9, 10, 3, 4, 5])

I tried torch.cat , but I can only have我试过torch.cat ,但我只能有

tensor([ 6.,  7.,  8.,  9., 10.,  1.,  2.,  3.,  4.,  5.])
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])

you will need to split the first tensor and concatenate the second in between您将需要拆分第一个张量并在两者之间连接第二个

torch.cat([a[:2], b, a[2:]])

output will be like output 会像

tensor([ 1.,  2.,  6.,  7.,  8.,  9., 10.,  3.,  4.,  5.])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM