如果形状完全不同,如何连接两个不同模型的输出?

问题描述 投票:0回答:1

我在 pytorch 中有两个预训练模型,它们使用不同类型的输入数据。我使用它们进行特征提取,并希望在最后连接它们的输出并将它们放入 LSTM。

我有以下输出:

型号A

[batch size, num of features, num of frames]

型号B

[batch size, num of features, num of frames, height, width]

如何将它们连接起来,以便将连接的向量放入 LSTM 中并对其进行训练。我不知道如何以某种方式做到这一点,以便我的连接向量仍然“有意义”并且不会丢失任何信息。

machine-learning deep-learning pytorch computer-vision lstm
1个回答
0
投票

您可以重塑第二个模型的输出向量,使新的特征数量为

num of features x height x width

假设两个向量具有相同的批量大小和相同的帧数: 首先,对两个输出向量重新排序,顺序为

[batch size, num of frames, num of features, ...]

然后,连接最后一个维度:

output1 = torch.randn(10, 5, 20)
output2 = torch.randn(10, 7, 20, 100, 200)
output1 = output1.permute(0, 2, 1) 
output2 = output2.permute(0, 2, 1, 3, 4)
output = torch.concatenate([output1, output2], dim=2)

现在,输出张量具有来自两个输出向量的所有特征,批量中每个实例的每帧编号,这通常是有意义的

© www.soinside.com 2019 - 2024. All rights reserved.