Tensorflow js, How to get the output of each layer? [duplicate]
I am trying to create a simple neural network viewer like the diagram below. I can get the trained weights, but where are node values stored in a tensorflow js layer when prediction has run? In other words, I can get the line values, but not the circled values. In a simple network these are as simple as the x and y passed into the fit method.
Solution 1:
getWeigths allows to retrieve the weights of a layer
Using tf.model
, one can output the prediction of each layer
const input = tf.input({shape: [5]});
const denseLayer1 = tf.layers.dense({units: 10, activation: 'relu'});
const denseLayer2 = tf.layers.dense({units: 2, activation: 'softmax'});
const output1 = denseLayer1.apply(input);
const output2 = denseLayer2.apply(output1);
const model = tf.model({inputs: input, outputs: [output1, output2]});
const [firstLayer, secondLayer] = model.predict(tf.ones([2, 5]));
console.log(denseLayer1.getWeights().length) // 2 W and B for a dense layer
denseLayer1.getWeights()[1].print()
console.log(denseLayer2.getWeights().length) // also 2
// output of each layer WX + B
firstLayer.print();
secondLayer.print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
</head>
<body>
</body>
</html>
One can also do the same thing using tf.sequential()
const model = tf.sequential();
// first layer
model.add(tf.layers.dense({units: 10, inputShape: [4]}));
// second layer
model.add(tf.layers.dense({units: 1}));
// get all the layers of the model
const layers = model.layers
layers[0].getWeights()[0].print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
</head>
<body>
</body>
</html>
However with tf.sequential
, one cannot get the prediction of each layer as the way one can with tf.model
using the output
passed as parameter in the config of the model