打印所有图层输出 [英] print all layers output

查看:20
本文介绍了打印所有图层输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given the following model, how to print all layers values ?

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>

推荐答案

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

To print layers, one needs to define the layers to output in the model configuration, using, outputs property. Using destructuring assignement on model.predict() one could retrieve the intermediate layers to 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 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>

这篇关于打印所有图层输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆