The Keras manual doesn\'t say too much:
keras.backend.function(inputs, outputs, updates=None)
Instantiates a Keras function.
Arguments
inputs: List of place
I think this function is just used to extract intermediate result. One can refer to the Keras Documentation about "How can I obtain the output of an intermediate layer?"
One simple way is to create a new Model that will ouput the layers that you are interested in:
from keras.models import Model
model = ... # create the original model
layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict(data)
Another way is to build a Keras function, which will return the output of a certain layer given input. For example:
from keras import backend as K
# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
[model.layers[3].output])
layer_output = get_3rd_layer_output([x])[0]
You can used the returned function object get_3rd_layer_output to get the intermediate result of the third layer.
Think it as a function wrapper. In framework of keras and tensorflow, it wrappers list of tensor as input and does some operations on weights in network (backward propagation). It is specially useful in field of Reinforcement learning, where the loss is computed after actions(model output) and model.fit is too macro to incorporate such op.
I have the following understanding of this function keras.backend.function
. I will explain it with the help of a code snippet from this.
The part of code snippet is as follows
final_conv_layer = get_output_layer(model, "conv5_3")
get_output = K.function([model.layers[0].input],
[final_conv_layer.output, model.layers[-1].output])
[conv_outputs, predictions] = get_output([img])
In this code, there is a model from which conv5_3
layer is extracted (line 1). In the function K.function()
, the first argument is input to this model and second is set of 2 outputs - one for convolution and second for softmax output at the last layer.
As per the Keras/Tensorflow manual, this function runs the computation graph that we have created in the code, taking input from the first parameter and extracting the number of outputs as per the layers mentioned in the second parameter. Thus, conv_outputs are output of final_conv_layer and predictions are output of model.layers[-1]
, i.e. the last layer of the model.