【问题标题】:Keras: Get the first n layersKeras:获取前 n 层
【发布时间】:2017-04-05 17:00:07
【问题描述】:

我正在通过执行以下操作从保存的文件中加载自动编码器,结构如下所示:

autoencoder = load_model("autoencoder_mse1.h5")
autoencoder.summary()
>>> ____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_8 (InputLayer)             (None, 19)            0                                            
____________________________________________________________________________________________________
dense_43 (Dense)                 (None, 16)            320         input_8[0][0]                    
____________________________________________________________________________________________________
dense_44 (Dense)                 (None, 16)            272         dense_43[0][0]                   
____________________________________________________________________________________________________
dense_45 (Dense)                 (None, 2)             34          dense_44[0][0]                   
____________________________________________________________________________________________________
dense_46 (Dense)                 (None, 16)            48          dense_45[0][0]                   
____________________________________________________________________________________________________
dense_47 (Dense)                 (None, 16)            272         dense_46[0][0]                   
____________________________________________________________________________________________________
dense_48 (Dense)                 (None, 19)            323         dense_47[0][0]                   
====================================================================================================
Total params: 1269
__________________

前四层,包括InputLayer,构成编码器部分。我想知道是否有一种快速的方法可以抓住这四层。到目前为止,我遇到的唯一可能的解决方案是:

encoder = Sequential()
encoder.add(Dense(16, 19, weights=autoencoder.layers[1].get_weights()))

^ 并手动为另外两层执行此操作。我希望有一种方法可以更有效地提取前四层。尤其是.summary() 方法会吐出层摘要。

编辑 1(可能的解决方案): 我已经找到了以下解决方案,但我希望有更高效的解决方案(更少的代码)。

encoder = Sequential()
for i,l in enumerate(autoencoder.layers[1:]):
    if i==0:
        encoder.add(Dense(input_dim=data.shape[1],output_dim=l.output_dim,
                          activation="relu",weights=l.get_weights()))
    else:
        encoder.add(Dense(output_dim=l.output_dim,activation="relu",weights=l.get_weights()))
    if l.output_dim == 2:
        break

【问题讨论】:

    标签: python keras keras-layer


    【解决方案1】:

    试试这个,让我知道它是否有效:

    # TO get first four layers
    model.layers[0:3]
    #To get the input shape
    model.layers[layer_of_interest_index].input_shape
    #To get the input shape
    model.layers[layer_of_interest_index].output_shape
    # TO get weights matrices
    model.layers[layer_of_interest_index].get_weights()
    

    希望这会有所帮助。

    【讨论】:

    • 我想我明白你想要得到什么,但这个答案应该是一个评论。它没有直接回答我的问题。我可以通过这种方法获得权重,但我仍然需要知道权重的尺寸和要先验堆叠的层的类型。
    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2019-01-27
    • 2018-12-16
    相关资源
    最近更新 更多