python - 从 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_space
decode_32
解决方案
这个答案符合你的问题吗?
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)
推荐阅读
- java - 从 DB 中检索 Uri 时 - “无法执行 android:onClick 的非公共方法”,e); 爪哇
- common-lisp - “progv”中“符号值”的行为
- error-handling - 平滑误差的残差
- python - 拆分文件文档并对其进行排序
- r - 如何填充 R 中 plot_ly 中两个子图的中间以绘制为“体积”图?
- c# - 如何将 IdentityServer 与 docker-compose 一起使用?
- javascript - 在 jquery 或 puppeteer 上动态调用没有 src="" 的 iframe 上的 WebScraping
- python - 如何将意图与 cogs 一起使用?
- mysql - 在 SQL 中轻松查询 2 个表
- javascript - 如何使用 JavaScript 根据 id 更改 svg 路径的颜色?