【问题标题】:Dimensionality of Keras Dense layerKeras Dense 层的维度
【发布时间】:2020-03-12 22:21:44
【问题描述】:

我有一个 Keras 模型,尺寸如下:

________________________________________________________________________________
Layer (type)              Output Shape              Param #
================================================================================
stft (InputLayer)         (None, 1, 16384)          0
________________________________________________________________________________
static_stft (Spectrogram) (None, 1, 65, 256)        16640
________________________________________________________________________________
conv2d_1 (Conv2D)         (None, 38, 5, 9)          12882
________________________________________________________________________________
dense_1 (Dense)           (None, 38, 5, 512)        5120
________________________________________________________________________________
predictions (Dense)       (None, 38, 5, 368)        188784
================================================================================

我对最后密集层的维度感到困惑。我希望分别拥有 (None,512) 和 (None,368)。这是由以下答案建议的:Keras lstm and dense layer

它们最终的密集层创建如下:

x = keras.layers.Dense(512)(x)
outputs = keras.layers.Dense(
        368, activation='sigmoid', name='predictions')(x)

那么为什么他们有超过 512 个输出呢?我该如何改变呢?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    根据您的应用程序,您可以在 Conv2D 层之后展平:

    input_layer = Input((1, 1710))
    x = Reshape((38, 5, 9))(input_layer)
    x = Flatten()(x)
    x = Dense(512)(x)
    x = Dense(368)(x)
    
    Layer (type)                 Output Shape              Param #   
    _________________________________________________________________
    input_1 (InputLayer)         [(None, 1, 1710)]         0         
    _________________________________________________________________
    reshape (Reshape)            (None, 38, 5, 9)          0         
    _________________________________________________________________
    flatten (Flatten)            (None, 1710)              0         
    _________________________________________________________________
    dense (Dense)                (None, 512)               876032    
    _________________________________________________________________
    dense_1 (Dense)              (None, 368)               188784    
    

    【讨论】:

      【解决方案2】:

      这是Conv2D 层。卷积层产生长度为 9 的 38x5 输出,然后您的Dense 层将每个 38x5 长度 9 序列作为输入,并将其转换为长度为 512 的序列作为输出。

      为了摆脱空间依赖性,您需要使用池化层之类的东西,可能是GlobalMaxPool2D。这会将数据合并到仅通道维度中,并生成(None, 9) 形状的输出,这将导致来自Dense 层的预期形状。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-16
        • 2019-02-04
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多