Flatten() 运算符从最后一个维度开始展开值(至少对于 Theano 而言,它是“通道优先”,而不是像 TF 那样的“通道最后”。)。这等价于 numpy.reshape 与 'C' 排序:
‘C’表示使用类似C的索引顺序读取/写入元素,最后一个axis索引变化最快,回到第一个axis索引变化最慢。
这是一个独立示例,说明了使用 Keras Functional API 的 Flatten 运算符:
import numpy as np
from keras.layers import Input, Flatten
from keras.models import Model
inputs = Input(shape=(3,2,4))
# Define a model consisting only of the Flatten operation
prediction = Flatten()(inputs)
model = Model(inputs=inputs, outputs=prediction)
X = np.arange(0,24).reshape(1,3,2,4)
print(X)
#[[[[ 0 1 2 3]
# [ 4 5 6 7]]
#
# [[ 8 9 10 11]
# [12 13 14 15]]
#
# [[16 17 18 19]
# [20 21 22 23]]]]
model.predict(X)
#array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
# 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.,
# 22., 23.]], dtype=float32)