Are there different between origin model and Model(origin model's input, origin model's output)?

I would like to use middle layers output in the loss function, so I use

dis_loss = Model(input=dis_model.get_input_at(0), output=dis_model.get_output_at(-1))
func_x = dis_loss(y_true)
func_x = dis_loss(y_pred) 

Then, I got an error message as following.

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_layer_1:0", shape=(?, 224, 224, 3), dtype=float32) at layer "input_layer". The following previous layers were accessed without issue: []

I had checked the name of dis_model.get_input_at(0), and dis_model.layer[0].input and they are same. first layer's name of dis_model.summary() is also same.

However I try to directly use dis_loss = dis_model, and then the model compiled successfully. Are the two models different? Input and output are from the same model, shouldn't they connect?

1 answer

  • answered 2018-11-08 10:17 Abby

    2 input layers would cause 2 output layers, so

    dis_loss = Model(input=dis_model.get_input_at(0), output=dis_model.get_output_at(-1))

    has to be revised as

    dis_loss = Model(input=dis_model.get_input_at(0), output=dis_model.get_output_at(0))

    When combing 2 model,

    gen_x = gen_model.output
    dis_z = dis_model(gen_x)
    d_on_g = model(input=gen_x.input, output=[gen_x, dis_z])

    it creates second output, so dis_model.get_output_at(0) is dense_1/Tanh:0 which is dis_model's output, so it connect to the dis_model's input.

    dis_model.get_output_at(1) is model_2/dense_1/Tanh:0 which is d_on_g's output, so it coneect to d_on_g's input which is equal to gen_model's input.