gpt4 book ai didi

python - 如何使用一个模型中间层的输出作为另一个模型的输入?

转载 作者:行者123 更新时间:2023-12-02 15:53:36 27 4
gpt4 key购买 nike

我训练模型 A 并尝试使用中间层的输出和 name="layer_x" 作为模型 B.

我尝试像 Keras 文档一样使用中间层的输出 https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer .

模型A:

inputs = Input(shape=(100,))
dnn = Dense(1024, activation='relu')(inputs)
dnn = Dense(128, activation='relu', name="layer_x")(dnn)
dnn = Dense(1024, activation='relu')(dnn)
output = Dense(10, activation='softmax')(dnn)

模型B:

input_1 = Input(shape=(200,))
input_2 = Input(shape=(100,)) # input for model A

# loading model A
model_a = keras.models.load_model(path_to_saved_model_a)

intermediate_layer_model = Model(inputs=model_a.input,
outputs=model_a.get_layer("layer_x").output)

intermediate_output = intermediate_layer_model.predict(data)

merge_layer = concatenate([input_1, intermediate_output])
dnn_layer = Dense(512, activation="relu")(merge_layer)
output = Dense(5, activation="sigmoid")(dnn_layer)
model = keras.models.Model(inputs=[input_1, input_2], outputs=output)

当我调试时,我在这一行收到错误:

intermediate_layer_model = Model(inputs=model_a.input, 
outputs=model_a.get_layer("layer_x").output)

File "..", line 89, in set_model
outputs=self.neural_net_asc.model.get_layer("layer_x").output)
File "C:\WinPython\python-3.5.3.amd64\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "C:\WinPython\python-3.5.3.amd64\lib\site-packages\keras\engine\topology.py", line 1592, in __init__
mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'

我可以使用 get_layer("layer_x").output 访问张量,并且 output_maskNone。我是否必须手动设置输出掩码?如果需要,如何设置此输出掩码?

最佳答案

您似乎做错了两件事:

intermediate_output = intermediate_layer_model.predict(data)

当你执行.predict()时,你实际上是在通过图表传递数据并询问结果是什么。当您这样做时,intermediate_output 将是一个 numpy 数组,而不是您希望的层。

其次,您不需要重新创建新的中间模型。您可以直接使用 model_a 中您感兴趣的部分。

这是为我“编译”的代码:

from keras.layers import Input, Dense, concatenate
from keras.models import Model

inputs = Input(shape=(100,))
dnn = Dense(1024, activation='relu')(inputs)
dnn = Dense(128, activation='relu', name="layer_x")(dnn)
dnn = Dense(1024, activation='relu')(dnn)
output = Dense(10, activation='softmax')(dnn)

model_a = Model(inputs=inputs, outputs=output)

# You don't need to recreate an input for the model_a,
# it already has one and you can reuse it
input_b = Input(shape=(200,))

# Here you get the layer that interests you from model_a,
# it is still linked to its input layer, you just need to remember it for later
intermediate_from_a = model_a.get_layer("layer_x").output

# Since intermediate_from_a is a layer, you can concatenate it with the other input
merge_layer = concatenate([input_b, intermediate_from_a])
dnn_layer = Dense(512, activation="relu")(merge_layer)
output_b = Dense(5, activation="sigmoid")(dnn_layer)
# Here you remember that one input is input_b and the other one is from model_a
model_b = Model(inputs=[input_b, model_a.input], outputs=output_b)

我希望这就是您想要做的。

如果有不清楚的地方请告诉我:-)

关于python - 如何使用一个模型中间层的输出作为另一个模型的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com