What's the purpose of keras.backend.function()

前端 未结 3 648
说谎
说谎 2020-12-29 20:06

The Keras manual doesn\'t say too much:

keras.backend.function(inputs, outputs, updates=None)

Instantiates a Keras function.
Arguments
inputs: List of place         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 20:34

    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.

提交回复
热议问题