打印所有图层输出

print all layers output

给定以下模型,如何打印所有层值?

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 output = denseLayer2.apply(denseLayer1.apply(input));
    const model = tf.model({inputs: input, outputs: output});
    model.predict(tf.ones([2, 5])).print();
    
    
 
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"> </script>
  </head>

  <body>
  </body>
</html>

你可以这样

    for(var i = 0; i < tf.layers.length; i++)
      model.predict(tf.layers[i].value).print();
      // OR
      model.predict(tf.layers[i].inputs).print();

我不知道你的数组结构如何,但类似的东西可以工作。

要打印层,需要在模型配置中定义要输出的层,使用 outputs 属性。在 model.predict() 上使用解构赋值可以检索中间层以输出

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]));
        firstLayer.print();
        secondLayer.print()
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"> </script>
  </head>

  <body>
  </body>
</html>