How to get output of hidden layer given an input, weights and biases of the hidden layer in keras?

后端 未结 3 1858
难免孤独
难免孤独 2021-02-15 17:53

Suppose I have trained the model below for an epoch:

model = Sequential([
    Dense(32, input_dim=784), # first number is output_dim
    Activation(\'relu\'),
          


        
3条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 18:45

    The easiest way is to use the keras backend. With the keras backend you can define a function that gives you the intermediate output of a keras model as defined here (https://keras.io/getting-started/faq/#how-can-i-obtain-the-output-of-an-intermediate-layer).

    So in essence:

    get_1st_layer_output = K.function([model.layers[0].input],
                                      [model.layers[1].output])
    layer_output = get_1st_layer_output([X])
    

提交回复
热议问题