【问题标题】:Weights and predictions of each layer每层的权重和预测
【发布时间】:2019-01-21 16:19:26
【问题描述】:

我正在尝试创建一个简单的神经网络查看器,如下图所示。我可以获得训练后的权重,但是当预测运行时,节点值存储在 tensorflow js 层的什么位置?换句话说,我可以得到线的值,但不能得到圈出来的值。在一个简单的网络中,这些就像传递给 fit 方法的 x 和 y 一样简单。

【问题讨论】:

标签: neural-network tensorflow.js


【解决方案1】:

getWeigths 允许检索层的权重

使用tf.model,可以输出每一层的预测

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/tfjs@0.12.0"> </script>
  </head>

  <body>
  </body>
</html>

使用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/tfjs@0.12.0"> </script>
  </head>

  <body>
  </body>
</html>

但是使用tf.sequential,无法像使用tf.model 在模型配置中作为参数传递的output 那样获得每一层的预测

【讨论】:

  • 有趣。因此,apply 方法似乎是不使用 tf.sequential 时将图层相互映射的方法。是否有一个选项可以从 ts.sequential 而不是 tf.model 中的图层中获取值,因为 tf.sequential 已经按照添加的顺序将图层相互映射?有意义吗?
猜你喜欢
  • 2017-12-01
  • 2020-12-16
  • 1970-01-01
  • 2020-01-12
  • 2019-02-27
  • 2017-10-19
  • 2021-10-09
  • 1970-01-01
  • 2018-10-08
相关资源
最近更新 更多