How to extract displacement weights in a Keras sequential model?

I use a simple forwarder network using Keras. With only one hidden layer, I would like to draw some conclusions regarding the relevance of each input to each output, and I would like to extract weight.

This is the model:

def build_model(input_dim, output_dim): n_output_layer_1 = 150 n_output = output_dim model = Sequential() model.add(Dense(n_output_layer_1, input_dim=input_dim, activation='relu')) model.add(Dropout(0.25)) model.add(Dense(n_output)) 

To gain weight, I wrote:

 for layer in model.layers: weights = layer.get_weights() weights = np.array(weights[0]) #this is hidden to output first = model.layers[0].get_weights() #input to hidden first = np.array(first[0]) 

Unfortunately, I do not get the column offsets in the matrices, which, as I know, Keras automatically puts into it.

Do you know how to extract displacement scales?

Thank you in advance for your help!

+6
source share
1 answer

get_weights () for a Dense layer returns a list of two elements, the first element contains weights, and the second element contains prejudices. Therefore, you can simply:

 weights = model.layers[0].get_weights()[0] biases = model.layers[0].get_weights()[1] 

Note that weights and offsets are already numpy arrays.

+11
source

Source: https://habr.com/ru/post/1015207/


All Articles