Keras 向中间层提供输入并获得最终输出 [英] Keras give input to intermediate layer and get final output

查看:35
本文介绍了Keras 向中间层提供输入并获得最终输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型是一个简单的全连接网络,如下所示:

My model is a simple fully connected network like this:

inp=Input(shape=(10,))
d=Dense(64, activation='relu')(inp)
d=Dense(128,activation='relu')(d)
d=Dense(256,activation='relu')(d)     #want to give input here, layer3
d=Dense(512,activation='relu')(d)
d=Dense(1024,activation='relu')(d)
d=Dense(128,activation='linear')(d)

所以,在保存模型后,我想为第 3 层提供输入.我现在正在做的是:

So, after saving the model I want to give input to layer 3. What I am doing right now is this:

model=load_model('blah.h5')    #above described network
print(temp_input.shape)        #(16,256), which is equal to what I want to give

index=3
intermediate_layer_model = Model(inputs=temp_input,
                                 outputs=model.output)

End_output = intermediate_layer_model.predict(temp_input)

但它不起作用,即我收到了不兼容的输入等错误,输入应该是元组等.错误消息是:

But it isn't working, i.e. I am getting errors like incompatible input, inputs should be tuple etc. The error message is:

raise TypeError('`inputs` should be a list or tuple.') 
TypeError: `inputs` should be a list or tuple.

有什么办法可以在网络中间传递我自己的输入并获得输出,而不是在开始时提供输入并从最后获得输出?任何帮助将不胜感激.

Is there any way I can pass my own inputs in middle of network and get the output instead of giving an input at the start and getting output from the end? Any help will be highly appreciated.

推荐答案

首先,您必须了解在 Keras 中,当您在输入上应用层时,在该层内创建一个新节点,它连接输入和输出张量.每层可能有多个节点,将不同的输入张量连接到相应的输出张量.为了构建模型,遍历这些节点并创建模型的新图,其中包含从输入张量到达输出张量所需的所有节点(即您在创建模型时指定的:model = Model(inputs=[...], 输出=[...]).

First you must learn that in Keras when you apply a layer on an input, a new node is created inside this layer which connects the input and output tensors. Each layer may have multiple nodes connecting different input tensors to their corresponding output tensors. To build a model, these nodes are traversed and a new graph of the model is created which consists all the nodes needed to reach output tensors from input tensors (i.e. which you specify when creating a model: model = Model(inputs=[...], outputs=[...]).

现在您想提供模型的中间层并获得模型的输出.由于这是一个新的数据流路径,我们需要为与这个新计算图相对应的每一层创建新节点.我们可以这样做:

Now you would like to feed an intermediate layer of a model and get the output of the model. Since this is a new data-flow path, we need to create new nodes for each layer corresponding to this new computational graph. We can do it like this:

idx = 3  # index of desired layer
input_shape = model.layers[idx].get_input_shape_at(0) # get the input shape of desired layer
layer_input = Input(shape=input_shape) # a new input tensor to be able to feed the desired layer

# create the new nodes for each layer in the path
x = layer_input
for layer in model.layers[idx:]:
    x = layer(x)

# create the model
new_model = Model(layer_input, x)

幸运的是,您的模型由一个分支组成,我们可以简单地使用 for 循环来构建新模型.但是,对于更复杂的模型,这样做可能并不容易,您可能需要编写更多代码来构建新模型.

Fortunately, your model consists of one-branch and we could simply use a for loop to construct the new model. However, for more complex models it may not be easy to do so and you may need to write more codes to construct the new model.

这篇关于Keras 向中间层提供输入并获得最终输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆