gpt4 book ai didi

python - 如何在 Keras 中将 (None, 10) 维张量 reshape 为 (None, None, 10)?

转载 作者:行者123 更新时间:2023-11-30 09:43:29 29 4
gpt4 key购买 nike

我正在尝试将可变大小的序列输入 LSTM。因此我使用生成器且批量大小为 1。
我有一个嵌入的 (sequence_length,)-input-tensor,并输出 (batch_size,equence_length, embeding_dimension)-tensor。
同时,我拥有的其他输入数据的大小为 (sequence_length, features)(None, 10),我想将其 reshape 为 (batch_size,序列长度,特征)(无,无,10)

我尝试使用 Keras Reshape-Layer 和 target_shape (-1, 10) ,这应该相当于将 (None, 10) 展开为 ( None, None, 10),但我收到的是 (None, 1, 10) 张量,这使得无法连接它和嵌入的数据以将其提供给LSTM。
我的代码:

cluster = Input(shape=(None,))
embeded = Embedding(115, 25, input_length = None)(cluster)

features = Input(shape=(10,)) #variable
reshape = Reshape(target_shape=(-1, 10))(features)

merged = Concatenate(axis=-1)([embeded, reshape])

[...]

model.fit_generator(generator(), steps_per_epoch=1, epochs=5)

输出:

[...]
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, None, 25), (None, 1, 10)]

如何在 Keras 中将 (None, 10) reshape 为 (None, None, 10) 张量?

最佳答案

与在 NumPy 中进行 reshape 相比,Keras 执行此操作不会有任何好处。您可以:

# perform reshaping prior to passing to Keras
features = Input(shape=(None, 10))

并在传递到 Keras 之前执行 reshape ,在 Keras 中您可以在输入中获得实际的 batch_sizesequence_length

关于python - 如何在 Keras 中将 (None, 10) 维张量 reshape 为 (None, None, 10)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55883940/

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