简体   繁体   English

尝试使用.get_layer方法在Keras中创建模型时,图形断开连接

[英]Graph disconnected when trying to create models in Keras with .get_layer method

In the normal code, I do something like this, and everything works fine: 在普通的代码中,我这样做,一切正常:

from keras.layers import Input, Dense
from keras.models import Model
import keras.backend as K
import numpy as np
import tensorflow as tf
from sklearn.datasets import make_blobs

X, y = make_blobs(500,50,2)

def make_network1():
    input_layer = Input((50,))
    layer1 = Dense(100,name='network1_dense1')(input_layer)
    output = Dense(50,name='network1_dense2')(layer1)
    model = Model(input_layer,output)

    return model

def make_network2():
    input_layer = Input((50,))
    layer1 = Dense(100,name='network2_dense1')(input_layer)
    output = Dense(1,name='network2_output')(layer1)
    model = Model(input_layer,output)

    return model

network1 = make_network1()
network2 = make_network2()
output = network2(network1.output)

model = Model(network1.input, output)

Now, I want to experiment with the .get_layer method and .output attribute in Keras by replacing the last line of code with: 现在,我想用.get_layer方法和.output属性进行试验,方法是将最后一行代码替换为:

model = Model(network1.input, network2.get_layer('network2_output').output)

Then it gives me the following error: 然后它给了我以下错误:

Graph disconnected: cannot obtain value for tensor Tensor("input_4:0", shape=(?, 50), dtype=float32) at layer "input_4". 图表已断开连接:无法在“ input_4”层获取张量Tensor(“ input_4:0”,shape =(?, 50),dtype = float32)的值。 The following previous layers were accessed without issue: [] 顺利访问了以下先前的层:[]

My Question 我的问题

However, shouldn't be output and network2.get_layer('network2_output').output the same thing? 但是,不应该outputnetwork2.get_layer('network2_output').output一样吗? When I try to print both of them out, it says: 当我尝试将它们都打印出来时,它说:

Tensor("model_14/network2_output/BiasAdd:0", shape=(?, 1), dtype=float32) 张量(“ model_14 / network2_output / BiasAdd:0”,shape =(?, 1),dtype = float32)

and

Tensor("network2_output_1/BiasAdd:0", shape=(?, 1), dtype=float32) Tensor(“ network2_output_1 / BiasAdd:0”,shape =(?, 1),dtype = float32)

And network2 has been connected to the output of network1 already, I don't get why it is disconnected. network2已经连接到输出network1了,我不明白为什么它被断开。 How to make the code works with the .get_layer and .output methods? 如何使代码与.get_layer.output方法配合使用?

I am using keras==2.24 and tensorflow-gpu==1.5. 我正在使用keras == 2.24和tensorflow-gpu == 1.5。

After running this line: 运行此行后:

output = network2(network1.output)

the network2 model has two computation flows: one is the original one constructed when running make_network2() , and another is the computation flow with network1.output as the input constructed when running the above line. network2模型具有两个计算流程:一个是运行make_network2()时构造的原始流程,另一个是在运行上述行时以network1.output作为输入构造的计算流程。 Therefore, it would have two outputs corresponding to each of these two computation flows: 因此,它将具有两个输出,分别对应于这两个计算流程:

>>> network2.get_output_at(0)
<tf.Tensor 'network2_output_4/BiasAdd:0' shape=(?, 1) dtype=float32>

>>> network2.get_output_at(1)
<tf.Tensor 'model_14/network2_output/BiasAdd:0' shape=(?, 1) dtype=float32>

Therefore, when you want to go from the network1.input to the output of network2 model, you must use the second output which is connected to the network1.input : 因此,当你想从去network1.input到达输出network2模式,你必须使用它连接到第二输出network1.input

model = Model(network1.input, network2.get_output_at(1))

Essentially, network2.get_output_at(1) is equivalent to output obtained in this line: output = network2(network1.output) . 从本质上讲, network2.get_output_at(1)相当于output在该品系获得的: output = network2(network1.output)

shouldn't be output and network2.get_layer('network2_output').output the same thing? 不应该输出和network2.get_layer('network2_output')。output一样吗?

No!, they are not the same thing. 不,他们不是同一回事。 Let me explain what is happening here 让我解释一下这里发生了什么

network1 = make_network1()
network2 = make_network2()
output = network2(network1.output)

First you are creating two model's with one input for each layer and then you are replacing the second model's input with last layers output of the first model. 首先,您要创建两个模型,每个层有一个输入,然后用第一个模型的最后一层输出替换第二个模型的输入。 This way you are making inputs of the output variable to be the first model's input. 这样,您就可以将output变量的输入作为第一个模型的输入。 So the network1.inputs and output are connected. 因此network1.inputsoutput已连接。 But on the following line there is no connection between network1.input and network2.get_layer('network2_output').output 但是,从下面的行之间不存在连接network1.inputnetwork2.get_layer('network2_output').output

model = Model(network1.input, network2.get_layer('network2_output').output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM