首页 > 解决方案 > 从 Keras Sequential 模型中提取子网

问题描述

我训练了一个非常简单的自动编码器网络,类似于这个例子:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
        layers.Dense(128, activation="relu"),
        layers.Dense(64, activation="relu"),
        layers.Dense(32, activation="relu"),
        layers.Dense(16, activation="relu"),
        layers.Dense(8, activation="relu", name="latent_space"),
        layers.Dense(16, activation="relu"),
        layers.Dense(32, activation="relu", name="decode_32"),
        layers.Dense(64, activation="relu"),
        layers.Dense(128, activation="sigmoid"),
        ])

model.compile(...)
model.fit(...)

# Extract subnetwork here after training

我想知道是否可以将数据提供给latent_space图层,以便之后我可以从图层中提取激活decode_32?理想情况下,我想在以层作为输入、层作为输出层crop进行训练后创建一个子网络。那可能吗?latent_spacedecode_32

标签: pythontensorflow

解决方案


这个答案符合你的问题吗?

def extract_layers(main_model, starting_layer_ix, ending_layer_ix) :
  # create an empty model
  new_model = Sequential()
  for ix in range(starting_layer_ix, ending_layer_ix + 1):
    curr_layer = main_model.get_layer(index=ix)
    # copy this layer over to the new model
    new_model.add(curr_layer)
  return new_model 

如果您更喜欢使用第一层和最后一层的名称来选择您的子网,则该get_layer方法还具有层名称的参数,但更简单的解决方案是检索要选择的层的索引,这要归功于该layer.name参数。

这样,您只需要通过添加来修改以前的功能

layer_names = [layer.name for layer in main_model.layers]
starting_layer_ix = layer_names.index(starting_layer_name)
ending_layer_ix = layer_names.index(ending_layer_name)

推荐阅读