How to turn entire keras model into theano function

前端 未结 2 1650
执笔经年
执笔经年 2021-01-05 19:32

I want to turn my keras model into a theano function so that I can compute the gradients on the inputs. I thought this might be cool for visualizing the network. I want to

相关标签:
2条回答
  • 2021-01-05 19:52

    Following the FAQ, try:

    from keras import backend as K
    get_last_layer_output = K.function([model.layers[0].input],
                                       [model.layers[-1].output])
    

    For the most recent version of Keras (1.0), use

    from keras import backend as K
    get_last_layer_output = K.function([model.layers[0].input],
                                       [model.layers[-1].get_output(train=False)])
    
    0 讨论(0)
  • 2021-01-05 20:06

    For "old" keras(0.3.x for example):

    I don't use this version but examples like this one should work.

    For "new" keras(1.0+):

    Since you use Dropout layer, you will need to add another input K.learning_phase() and give it the value 0 (0 for testing, 1 for training.)

    code:

    from keras import backend as K
    K.function([model.layers[0].input, K.learning_phase()], [model.layers[-1].output])
    

    Reference: keras FAQ

    0 讨论(0)
提交回复
热议问题