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

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

提交回复
热议问题