gpt4 book ai didi

machine-learning - PyTorch 中的双向 LSTM 输出问题

转载 作者:行者123 更新时间:2023-11-30 08:24:42 25 4
gpt4 key购买 nike

您好,我有一个关于如何从 BI-LSTM 模块的输出收集正确结果的问题。

假设我有一个 10 长度的序列输入到具有 100 个隐藏单元的单层 LSTM 模块中:

lstm = nn.LSTM(5, 100, 1, bidirectional=True)

输出的形状为:

[10 (seq_length), 1 (batch),  200 (num_directions * hidden_size)]
# or according to the doc, can be viewed as
[10 (seq_length), 1 (batch), 2 (num_directions), 100 (hidden_size)]

如果我想在两个方向(两个 100 维向量)获得第三个(1-索引)输入的输出,我怎样才能正确地做到这一点?

我知道 output[2, 0] 会给我一个 200 维的向量。 这个 200 个暗淡向量是否代表第三个输入在两个方向上的输出?

令我困扰的是,当进行反向馈送时,第三个(1-索引)输出向量是根据第 8 个(1-索引)输入计算的,对吧?

pytorch 会自动处理这个问题并根据方向分组输出吗?

谢谢!

最佳答案

是的,当使用 BiLSTM 时,方向的隐藏状态只是连接起来(中间之后的第二部分是用于输入相反序列的隐藏状态)。
所以在中间分开就可以了。

由于 reshape 是从右向左维度进行的,因此在分离两个方向时不会出现任何问题。

<小时/>

这是一个小例子:

# so these are your original hidden states for each direction
# in this case hidden size is 5, but this works for any size
direction_one_out = torch.tensor(range(5))
direction_two_out = torch.tensor(list(reversed(range(5))))
print('Direction one:')
print(direction_one_out)
print('Direction two:')
print(direction_two_out)

# before outputting they will be concatinated
# I'm adding here batch dimension and sequence length, in this case seq length is 1
hidden = torch.cat((direction_one_out, direction_two_out), dim=0).view(1, 1, -1)
print('\nYour hidden output:')
print(hidden, hidden.shape)

# trivial case, reshaping for one hidden state
hidden_reshaped = hidden.view(1, 1, 2, -1)
print('\nReshaped:')
print(hidden_reshaped, hidden_reshaped.shape)

# This works as well for abitrary sequence lengths as you can see here
# I've set sequence length here to 5, but this will work for any other value as well
print('\nThis also works for more multiple hidden states in a tensor:')
multi_hidden = hidden.expand(5, 1, 10)
print(multi_hidden, multi_hidden.shape)
print('Directions can be split up just like this:')
multi_hidden = multi_hidden.view(5, 1, 2, 5)
print(multi_hidden, multi_hidden.shape)

输出:

Direction one:
tensor([0, 1, 2, 3, 4])
Direction two:
tensor([4, 3, 2, 1, 0])

Your hidden output:
tensor([[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]]]) torch.Size([1, 1, 10])

Reshaped:
tensor([[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]]]) torch.Size([1, 1, 2, 5])

This also works for more multiple hidden states in a tensor:
tensor([[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]],

[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]],

[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]],

[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]],

[[0, 1, 2, 3, 4, 4, 3, 2, 1, 0]]]) torch.Size([5, 1, 10])
Directions can be split up just like this:
tensor([[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]],


[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]],


[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]],


[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]],


[[[0, 1, 2, 3, 4],
[4, 3, 2, 1, 0]]]]) torch.Size([5, 1, 2, 5])

希望这有帮助! :)

关于machine-learning - PyTorch 中的双向 LSTM 输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53010465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com