How to turn entire keras model into theano function

前端 未结 2 1663
执笔经年
执笔经年 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 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

提交回复
热议问题