【问题标题】:print all layers output打印所有层输出
【发布时间】:2018-07-23 16:19:33
【问题描述】:
给定以下模型,如何打印所有层的值?
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>
【问题讨论】:
标签:
javascript
tensorflow.js
【解决方案1】:
要打印图层,需要使用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>
【解决方案2】:
你可以这样
for(var i = 0; i < tf.layers.length; i++)
model.predict(tf.layers[i].value).print();
// OR
model.predict(tf.layers[i].inputs).print();
我不知道你的数组的结构如何,但类似的东西可以工作。