gpt4 book ai didi

tensorflow - 在 Keras 中 Flatten 层是如何工作的?

转载 作者:行者123 更新时间:2023-12-03 13:45:15 24 4
gpt4 key购买 nike

我正在使用 TensorFlow 后端。

我正在依次应用卷积、最大池化、展平和密集层。卷积需要 3D 输入(高度、宽度、color_channels_depth)。

卷积之后,就变成了(height, width, Number_of_filters)。

应用最大池化后高度和宽度发生变化。但是,在应用平坦层之后,究竟会发生什么?例如,如果flatten之前的输入是(24, 24, 32),那么它是如何展平的呢?

对于每个过滤器编号的高度、重量是按顺序(24 * 24)还是按其他方式按顺序排列?一个带有实际值的示例将不胜感激。

最佳答案

Flatten()运算符从最后一个维度开始展开值(至少对于 Theano,它是“ channel 优先”,而不是像 TF 那样的“ channel 最后”。我无法在我的环境中运行 TensorFlow)。这相当于 numpy.reshape 'C' 排序:

‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest.



这是一个独立的示例,说明 Flatten带有 Keras Functional API 的运算符。您应该能够轻松适应您的环境。
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)

关于tensorflow - 在 Keras 中 Flatten 层是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44176982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com