3

I would like to specifically know how to get the neurons in a neural network are getting activated (the outputs of each neuron after the activation function)

How can I get the activations of all the neurons of a sequential model when I give input during the models' inference in Tensorflow 2?

RabbitBadger
  • 539
  • 7
  • 19

1 Answers1

4

Try something like this:

intermediate_output = tf.keras.Model(model.input, 
                                     model.get_layer('conv2_block1_3_conv').output)

You can get a list of the layers with model.layers and pick the one you want.

list(map(lambda x: x.name, model.layers))
['input_2',
 'conv1_pad',
 'conv1_conv',
 'conv1_bn',
 'conv1_relu',
 'pool1_pad',
 'pool1_pool',
 'conv2_block1_1_conv',
 'conv2_block1_1_bn',
 'conv2_block1_1_relu',
 'conv2_block1_2_conv',
 'conv2_block1_2_bn',
 'conv2_block1_2_relu',
 'conv2_block1_0_conv',
 'conv2_block1_3_conv', ...

Full example:

import tensorflow as tf
from skimage import data
import matplotlib.pyplot as plt

model = tf.keras.applications.resnet.ResNet50(
    weights='imagenet', include_top=True)

model.build(input_shape=(None, 224, 224, 3))

image = tf.image.resize(data.chelsea(), (224, 224))/255

enter image description here

intermediate_output = tf.keras.Model(model.input, 
                                    model.get_layer('conv2_block1_3_conv').output)

extracted = intermediate_output(image[None, ...])
<tf.Tensor: shape=(1, 56, 56, 256), dtype=float32, numpy=
array([[[[ 0.23296243, -0.19640587, -0.8846314 , ..., -0.3091477 ,
          -0.51000404, -0.00218517],
         [ 0.27896926, -0.22646117, -0.91138077, ..., -0.4151363 ,
          -0.73324907,  0.05706196],
         [ 0.27908558, -0.2267507 , -0.9121696 , ..., -0.41521263,
          -0.73362166,  0.05721636],
         [ 0.04438811,  0.49058744, -1.5047315 , ..., -0.15204512,
          -1.2029954 , -0.29269713],
         [ 0.04450325,  0.4905177 , -1.505692  , ..., -0.15128748,
          -1.2025517 , -0.29254213],
         [ 0.05638191,  0.22808033, -1.5240382 , ...,  0.0052015 ,
          -0.8789809 , -0.19639899]]]], dtype=float32)>
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143