How to get the output of a layer inside the network?


#1

Hi there,

I’m running in circles on solving this problem. Given the network:

 from mxnet import nd, gluon
   
 net = nn.Sequential()
 net.add(
        nn.Conv2D(channels=32, kernel_size=(5, 5), padding=(5 // 2, 5 // 2), activation='relu'),
        nn.Conv2D(channels=32, kernel_size=(5, 5), padding=(5 // 2, 5 // 2), activation='relu'),
        nn.MaxPool2D(pool_size=(2, 2), strides=(2, 2)),
        nn.Conv2D(channels=64, kernel_size=(5, 5), padding=(5 // 2, 5 // 2), activation='relu'),
        nn.Conv2D(channels=64, kernel_size=(5, 5), padding=(5 // 2, 5 // 2), activation='relu'),
        nn.MaxPool2D(pool_size=(2, 2), strides=(2, 2)),
        nn.Conv2D(channels=128, kernel_size=(3, 3), padding=(3 // 2, 3 // 2), activation='relu'),
        nn.Conv2D(channels=128, kernel_size=(3, 3), padding=(3 // 2, 3 // 2), activation='relu'),
        nn.MaxPool2D(pool_size=(2, 2), strides=(2, 2)),
        nn.Flatten(),
        nn.Dense(2, activation='relu'),
        nn.Dense(10)
 )

I’d like to get the output of layer nn.Dense(2, activation='relu') after a forward pass (or any other arbitrary layer inside the network). How do I do that? In Tensorflow you can ask for the output with the function my_tensor = tf.get_tensor_by_name("...") and do a forward pass by calling sess.run(my_tensor, feed_dict{...}), but I cannot find a similar function in MXNet. What is the appropriate way in MXNet?

Many thanks in advance,
Blake


#2

Sequential stores a chain of neural network layers. So you can just access them by their index. For instance:

print net[0:2]

Sequential(
  (0): Conv2D(None -> 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
  (1): Conv2D(None -> 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))

This gives the output after the 2nd layer.


#3

Oh, that is so easy! Many thanks for your swift answer.