簡體   English   中英

從保存的自動編碼器中提取編碼器和解碼器

[英]Extract encoder and decoder from saved autoencoder

我為我的項目使用的大量自動編碼器保存了模型。 它們是使用autoencoder.save(outdir + "autoencoder_"+params) function 保存的。

我有什么方法可以提取每個已保存模型的編碼器和解碼器組件,還是我需要重新運行腳本並添加encoder = Model(input, bottleneck)decoder = Model(bottleneck, output)行並保存這些模型?

這是我試圖檢索的自動編碼器結構:

autoencoder.summary()

Model: "model_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 3593, 4)]         0         
_________________________________________________________________
flatten (Flatten)            (None, 14372)             0         
_________________________________________________________________
dense (Dense)                (None, 1797)              25828281  
_________________________________________________________________
dense_1 (Dense)              (None, 719)               1292762   
_________________________________________________________________
dense_2 (Dense)              (None, 180)               129600    
_________________________________________________________________
dense_3 (Dense)              (None, 719)               130139    
_________________________________________________________________
dense_4 (Dense)              (None, 1797)              1293840   
_________________________________________________________________
dense_5 (Dense)              (None, 14372)             25840856  
_________________________________________________________________
reshape (Reshape)            multiple                  0         
=================================================================
Total params: 54,515,478
Trainable params: 54,515,478
Non-trainable params: 0
_________________________________________________________________

您可以將權重轉移到兩個不同的神經網絡模型。 您只需要確定瓶頸層的索引,您可以通過運行model.summary()輕松知道

這是一個片段,可以幫助您復制 model

bottleneck_index = # this you need to identify
encoder_model = tf.keras.Sequential()
for layer in ae_model.layers[:bottleneck_index]:
    layer_config = layer.get_config()  # to get all layer's parameters (units, activation, etc...)
    copied_layer = type(layer).from_config(layer_config) # to initialize the same layer class with same parameters
    copied_layer.build(layer.input_shape)  # build the layer to initialize the weights.
    copied_layer.set_weights(layer.get_weights())  # transfer the trainable parameters
    encoder_model.add(copied_layer)  # add it to the encoder's model

對解碼器執行相同操作,其中ae_model.layers[bottleneck_index:]

當然,您甚至可以通過檢查當前層的單元來識別瓶頸索引,如果它小於連續層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM