Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing neural network weights and neuron activations

After training a network using Keras:

  1. I want to access the final trained weights of the network in some order.

  2. I want to know the neuron activation values for every input passed. For example, after training, if I pass X as my input to the network, I want to know the neuron activation values for that X for every neuron in the network.

Does Keras provide API access to these things? I want to do further analysis based on the neuron activation values.

Update : I know I can do this using Theano purely, but Theano requires more low-level coding. And, since Keras is built on top of Theano, I think there could be a way to do this?

If Keras can't do this, then among Tensorflow and Caffe , which can? Keras is the easiest to use, followed by Tensorflow/Caffe, but I don't know which of these provide the network access I need. The last option for me would be to drop down to Theano, but I think it'd be more time-consuming to build a deep CNN with Theano..

like image 758
sanjeev mk Avatar asked Apr 23 '16 14:04

sanjeev mk


1 Answers

This is covered in the Keras FAQ, you basically want to compute the activations for each layer, so you can do it with this code:

from keras import backend as K

#The layer number
n = 3 
# with a Sequential model
get_nth_layer_output = K.function([model.layers[0].input],
                                  [model.layers[n].output])
layer_output = get_nth_layer_output([X])[0]

Unfortunately you would need to compile and run a function for each layer, but this should be straightforward.

To get the weights, you can call get_weights() on any layer.

nth_weights = model.layers[n].get_weights()
like image 131
Dr. Snoopy Avatar answered Sep 24 '22 23:09

Dr. Snoopy