tensorflowjs 如何在 cnn 预测中获取内层输出
tensorflowjs how to get inner layer output in a cnn prediction
我正在查看来自 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例子中的预测方法,只是打印了最后一层。如何在预测中从每一层(包括转换层、最大池化层和全连接层)获得输出?
这个 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);
感谢您的回答。
我找到了另一种获取输入和输出数据的方法。我修改了节点模块中的 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();
要获取所有内层,可以使用模型的属性 layers
。获得图层后,您可以使用每个图层的属性 input
和 output
来定义新模型或使用 apply 方法。
有人问过类似的问题 and
我正在尝试以传统方式制作模型,但无法获得两个输出,否则如果您使用 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());
我正在查看来自 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例子中的预测方法,只是打印了最后一层。如何在预测中从每一层(包括转换层、最大池化层和全连接层)获得输出?
这个 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);
感谢您的回答。
我找到了另一种获取输入和输出数据的方法。我修改了节点模块中的 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();
要获取所有内层,可以使用模型的属性 layers
。获得图层后,您可以使用每个图层的属性 input
和 output
来定义新模型或使用 apply 方法。
有人问过类似的问题
我正在尝试以传统方式制作模型,但无法获得两个输出,否则如果您使用 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());