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

后端 未结 3 1869
难免孤独
难免孤独 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:26

    As for weights, I had a none-Sequential model. What I did was using model.summary() to get the desired layers name and then model.get_layer("layer_name").get_weights() to get the weights.

    0 讨论(0)
  • 2021-02-15 18:33

    Just recreate the first part of the model up until the layer for which you would like the output (in your case only the first dense layer). Afterwards you can load the trained weights of the first part in your newly created model and compile it.

    The output of the prediction with this new model will be the output of the layer (in your case the first dense layer).

    from keras.models import Sequential
    from keras.layers import Dense, Activation
    import numpy as np
    
    model = Sequential([
        Dense(32, input_dim=784), # first number is output_dim
        Activation('relu'),
        Dense(10), # output_dim, input_dim is taken for granted from above
        Activation('softmax'),
    ])
    model.compile(optimizer='adam', loss='categorical_crossentropy')
    
    #create some random data
    n_features = 5
    samples = np.random.randint(0, 10, 784*n_features).reshape(-1,784)
    labels = np.arange(10*n_features).reshape(-1, 10)
    
    #train your sample model
    model.fit(samples, labels)
    
    #create new model
    new_model= Sequential([
        Dense(32, input_dim=784), # first number is output_dim
        Activation('relu')])
    
    #set weights of the first layer
    new_model.set_weights(model.layers[0].get_weights())
    
    #compile it after setting the weights
    new_model.compile(optimizer='adam', loss='categorical_crossentropy')
    
    #get output of the first dens layer
    output = new_model.predict(samples)
    
    0 讨论(0)
  • 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])
    
    0 讨论(0)
提交回复
热议问题