作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将可变大小的序列输入 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_size
和 sequence_length
。
关于python - 如何在 Keras 中将 (None, 10) 维张量 reshape 为 (None, None, 10)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55883940/
我是一名优秀的程序员,十分优秀!