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

查看:159
本文介绍了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.

推荐答案

首先,当在输入

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天全站免登陆