kennysong
kennysong

Reputation: 2134

How to get activation of a hidden layer in tensorflow.js?

In TensorFlow.js, I have a very simple tf.Sequential model created like this:

let model = tf.sequential();
model.add(tf.layers.dense({inputShape: [784], units: 128, activation: 'relu'}));
model.add(tf.layers.dense({units: 10}));
model.add(tf.layers.softmax());

During prediction time, how can I get the activation of the second tf.layers.dense layer?

Can I just delete model.layers[2] and use model.predict() as normal?

(I know I can do this in advance by defining two model outputs with the functional API, but let's say I have a pre-made tf.Sequential model that I want to inspect the logits of.)

Upvotes: 0

Views: 479

Answers (2)

Andy K
Andy K

Reputation: 301

For more complex models, there's an easier way. If model is the original model, you can create a copy using tf.model({inputs:model.inputs, outputs: model.layers[2].output}), thereby only needing to provide the first and last layer

Upvotes: 1

kennysong
kennysong

Reputation: 2134

I figured out how to do this.

Deleting model.layers[2] doesn't work, since apparently model.predict() doesn't depend on that property.

One way to do this is to create a duplicate tf.Sequential model, copying over all the layers (except the last) from the original.

let m2 = tf.sequential();
m2.add(model.layers[0]);
m2.add(model.layers[1]);

Then m2.predict() will output the logits.

Upvotes: 0

Related Questions