【问题标题】:How to reshape (None, 10)-dimensional tensor to (None, None, 10) in Keras?如何在 Keras 中将 (None, 10) 维张量重塑为 (None, None, 10)?
【发布时间】:2019-04-27 19:05:50
【问题描述】:

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

我尝试过使用带有 target_shape (-1, 10) 的 Keras Reshape-Layer,这应该相当于将 (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) 重塑为 (None, None, 10) 张量?

【问题讨论】:

    标签: python machine-learning keras neural-network recurrent-neural-network


    【解决方案1】:

    这样做 Keras 不会比在 NumPy 中进行重塑有任何好处。你可以:

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

    并在传递到 Keras 之前执行整形,您的输入中有实际的 batch_sizesequence_length

    【讨论】:

      猜你喜欢
      • 2019-04-19
      • 2018-10-26
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多