tensorflow 从Keras序列模型中提取子网络

ghhaqwfi  于 2023-05-01  发布在  其他
关注(0)|答案(2)|浏览(135)

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

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_space层作为输入,decode_32层作为输出。这可能吗?

sdnqo3pr

sdnqo3pr1#

最简单的方法是获取所需的输入和输出层,并使用它们创建一个新模型。在本例中,请确保指定输入形状:

model = keras.Sequential([
    layers.Dense(128, activation="relu", input_shape=(15,)),
    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(...)

in_layer = model.get_layer("latent_space")
out_layer = model.get_layer("decode_32")

sub_model = keras.Model(inputs=in_layer.input, outputs=out_layer.output)

sub_model将是一个只包含您所选图层的模型,就像您想要的那样。

nafvub8i

nafvub8i2#

this answer符合您的问题吗?

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)

相关问题