【问题标题】:tensorflowjs how to get inner layer output in a cnn predictiontensorflowjs如何在cnn预测中获得内层输出
【发布时间】:2018-06-20 07:32:15
【问题描述】:

我正在查看来自tfjs 的 tensorflow.js CNN 示例。

测试仓库可以在这里找到:testing repo

有什么方法可以从每一层获取输出?

 async showPredictions() {
    const testExamples = 1;
    // const testExamples = 100;
    const batch = this.data.nextTestBatch(testExamples);

    tf.tidy(() => {
        const output: any = this.model.predict(batch.xs.reshape([-1, 28, 28, 1]));

        output.print();
        const axis = 1;
        const labels = Array.from(batch.labels.argMax(axis).dataSync());
        const predictions = Array.from(output.argMax(axis).dataSync());

        // ui.showTestResults(batch, predictions, labels);
    });
}

以上是 tfjs 示例中的预测方法,但只打印了最后一层。如何在预测中从每一层(包括卷积层、最大池化层和完全连接层)获取输出?

【问题讨论】:

    标签: javascript tensorflow machine-learning deep-learning tensorflow.js


    【解决方案1】:

    要获取所有内层,可以使用模型的属性layers。获得层后,您可以使用每个层的属性inputoutput 来定义新模型或使用apply 方法。

    herethere 提出了类似的问题

    【讨论】:

      【解决方案2】:

      此 Observable Notebook 提供了一个详细示例,说明如何在 TensorFlow.js 中获取 tf.Model 实例的内部激活: https://beta.observablehq.com/@nsthorat/visualizing-activations-of-mobilenet-with-tensorflow-js

      其背后的基本思想是使用与原始模型相同的输入但不同的输出来构建新的tf.Model 实例。这些输出是原始tf.Model 实例的各个层的输出。类似的东西

      const newModel = tf.model({inputs: oldModel.inputs, outputs: oldModel.layers[n].output); 
      const layerActivation = newModel.predict(inputValue);
      

      【讨论】:

        【解决方案3】:

        感谢您的回答。

        我找到了另一种获取输入和输出数据的方法。我已经修改了节点模块中的 tfjs 文件,并将最后的输入数据和输出数据附加到每一层。因此,在每次预测之后,我可以直接访问每一层的输入和输出。

        这种方式只在tfjs 0.11.6之前有效,要修改的文件是:

        /node_modules/@tensorflow/tfjs-layers/dist-es6/engine/executor.js
        

        添加这两行:

        fetch.sourceLayer.outputData = output[0].dataSync(); 
        fetch.sourceLayer.inputData = inputValues[0].dataSync();
        

        【讨论】:

          【解决方案4】:

          I guess this is the solution!!

          我试图以常规方式制作模型,但无法获得两个输出,否则如果您使用 APPLY 方法,可能会获得两个输出,如下所示: const [firstLayer, secondLayer] = model.predict( xs);

          const input_nodes = 5;
          const hidden_nodes = 8;
          const output_nodes = 4;    
          const input = tf.input({shape: [input_nodes]});
          const denseLayer1 = tf.layers.dense({ units: hidden_nodes, activation: 'sigmoid' });
          const denseLayer2 = tf.layers.dense({ units: output_nodes, activation: 'softmax' });
          const output1 = denseLayer1.apply(input);
          const output2 = denseLayer2.apply(output1);
          const model = tf.model({inputs: input, outputs: [output1, output2]});
          
          let inputs = [1, 2, 3, 4, 5];
          const xs = tf.tensor2d([inputs]);
          const [firstLayer, secondLayer] = model.predict(xs);
          
          console.log(denseLayer1.getWeights().length)
          denseLayer1.getWeights()[1].print()
          console.log(denseLayer2.getWeights().length)
          
          // PRINT
          
          firstLayer.print();
          secondLayer.print();
          
          // OR console
          
          console.log(firstLayer.flatten().arraySync());
          console.log(secondLayer.flatten().arraySync());
          

          【讨论】:

          • 欢迎来到 SO!请参阅“Explaining entirely code-based answers”。虽然这在技术上可能是正确的,但它并不能解释为什么它可以解决问题或应该是选择的答案。我们应该在帮助解决问题的同时进行教育。
          猜你喜欢
          • 2020-07-16
          • 1970-01-01
          • 2018-03-12
          • 1970-01-01
          • 1970-01-01
          • 2020-03-19
          • 2020-11-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多