【问题标题】:Extract encoder and decoder from saved autoencoder从保存的自动编码器中提取编码器和解码器
【发布时间】:2021-04-28 09:19:59
【问题描述】:

我为我的项目使用的大量自动编码器保存了模型。它们是使用autoencoder.save(outdir + "autoencoder_"+params) 函数保存的。

我有什么方法可以提取每个已保存模型的编码器和解码器组件,还是我需要重新运行脚本并添加 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
_________________________________________________________________

【问题讨论】:

    标签: python keras autoencoder


    【解决方案1】:

    您可以将权重转移到两个不同的神经网络模型。您只需要确定瓶颈层的索引,您可以通过运行model.summary() 轻松知道

    这里有一个sn-p,可以帮你复制模型

    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:]

    当然,你甚至可以通过检查当前层的单元来识别瓶颈索引,如果它小于连续层。

    【讨论】:

    • 它给我一个错误 copied_layer.set_weights(layer.get_weights()) 行:ValueError:您在“dense_2”层上调用了“set_weights(weights)”,权重列表长度为 2,但该层期望权重为 0 .
    • 这很奇怪,可能是层没有初始化权重导致失败,我已经更新了代码在传输权重之前构建层,你可以试试吗?检查线路:copied_layer.build(layer.input_shape)
    • 这确实解决了这个问题,但现在它正在努力处理重塑层。我在问题中添加了摘要,似乎它可能会有所帮助。以下是错误消息: AttributeError: The layer "reshape has multiple inbound nodes, with different input shapes. 因此,"input shape" 的概念对层的定义不明确。请改用get_input_shape_at(node_index)
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2019-07-22
    • 2019-05-19
    • 2019-07-07
    • 2017-11-21
    • 2018-04-04
    相关资源
    最近更新 更多