Keras 2 API 文档 / 层 API / 重塑层 / 重塑层

重塑层

[源代码]

Reshape

tf_keras.layers.Reshape(target_shape, **kwargs)

将输入重塑为给定形状的层。

输入形状

任意,但输入形状中的所有维度都必须已知/固定。当在模型中将此层用作第一层时,请使用关键字参数 input_shape(整数元组,不包括样本/批次大小轴)。

输出形状

(batch_size,) + target_shape

示例

>>> # as first layer in a Sequential model
>>> model = tf.keras.Sequential()
>>> model.add(tf.keras.layers.Reshape((3, 4), input_shape=(12,)))
>>> # model.output_shape == (None, 3, 4), `None` is the batch size.
>>> model.output_shape
(None, 3, 4)
>>> # as intermediate layer in a Sequential model
>>> model.add(tf.keras.layers.Reshape((6, 2)))
>>> model.output_shape
(None, 6, 2)
>>> # also supports shape inference using `-1` as dimension
>>> model.add(tf.keras.layers.Reshape((-1, 2, 2)))
>>> model.output_shape
(None, 3, 2, 2)