我在查看来自 tfjs 的 TensorFlow.js CNN 示例。
测试仓库在这里可以找到:测试仓库。
有没有办法可以获取每一层的输出?
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 示例中的预测方法,但只打印了最后一层的输出。我如何在预测中获取每一层的输出(包括卷积层、最大池化层和全连接层)?
回答:
要获取所有内部层,你可以使用模型的 layers
属性。一旦你获取了层,你可以使用每个层的 input
和 output
属性来定义一个新模型,或者使用 apply 方法。