如何在keras中拉平张量?

How to flat the tensor in keras?

我有一个名为 words_conv_bigram_pool 的张量,其形状为 (?, 1, 1, 64)? 是批量大小。 我尝试通过 Flatten()(words_conv_bigram_pool) 将张量展平为 (?, 64), 但它 returns 的形状 (?,?).

>>> Flatten()(words_conv_bigram_pool)
WARNING:tensorflow:From /home/xuemeng.cyn/anaconda2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py:1264: calling reduce_prod (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
<tf.Tensor 'flatten_1/Reshape:0' shape=(?, ?) dtype=float32>

发生了什么以及如何在keras中使用flatten函数?

不关心"tensorflow shapes",关心"keras shapes"。当您使用 keras 时,经常会看到这样的 ? 张量流维度。

如果您将这个展平层添加到您的模型中,然后执行 model.summary(),您将看到所需的形状。

如果您不使用 "keras model",而只想删除额外的维度,您可以尝试 tf.squeeze