I am trying to use the output of one keras model as the input of another model. As the output is type Tensor, it can not be used in model.predict() method as it wants numpy array
How can i do this
You'll probably get a cleaner answer on StackOverflow as this is not a Keras issue/bug related question. In short, models are like any other layer but with added loss and optimiser. You can use the first model like a layer when building the second one. There are examples in the functional API documentation, the shared vision model for example. Hope that helps.
@nuric in my case it gives an error like this.
ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 736). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model
I am trying to use a pre-trained model as a loss function.
nn4_small2_pretrained = create_model()
nn4_small2_pretrained.trainable = False
nn4_small2_pretrained.load_weights('weights/nn4.small2.v1.h5')
def face_recognition_loss(img, pred):
return K.sum(K.square(nn4_small2_pretrained(img) - nn4_small2_pretrained(pred)))
This is a different error, your shape is (batch_size, timesteps, 736), to be able to Flatten that tensor into (batch_size, timesteps*736) you need to know the size of the second dimension. You can either fix that and use flatten or use layers that can handle variable size lengths like RNNs.
You'll probably get a cleaner answer on StackOverflow as this is not a Keras issue/bug related question. In short, models are like any other layer but with added loss and optimiser. You can use the first model like a layer when building the second one. There are examples in the functional API documentation, the shared vision model for example. Hope that helps.
There is no documentation that says "Models are layers with added loss and optimiser." explicitly. Is this inferred knowledge ?
Yes, inferred from the source code: https://github.com/keras-team/keras/blob/master/keras/engine/network.py#L38
Most helpful comment
You'll probably get a cleaner answer on StackOverflow as this is not a Keras issue/bug related question. In short, models are like any other layer but with added loss and optimiser. You can use the first model like a layer when building the second one. There are examples in the functional API documentation, the shared vision model for example. Hope that helps.