Tensorflow.js tf.metrics.categoricalAccuracy()函数
Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
tf.metrics.categoricalAccuracy()函数用于返回两个张量之间的分类精度。它需要两个张量作为参数。
句法:
tf.metrics.categoricalAccuracy(a, b);
参数:
- a:第一个指定的张量。
- b:第二个指定张量。它必须具有相同的数据类型 “一种”。
返回值:它返回两个指定张量“a”和“b”的分类精度。
示例 1:
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Initializing two tensors
const a = tf.tensor2d([[1, 0, 0, 1], [0, 1, 0, 1]]);
const b = tf.tensor2d([
[0.1, 0.6, 0.01, 0.05],
[0.1, 0.02, 0.05, 0.3]
]);
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
// Print tensor
accuracy.print();
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Initializing two tensors
const a = tf.tensor([1, 0, 0, 1]);
const b = tf.tensor([1, 0.6, 0.01, 0.05]);
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
// Print tensor
accuracy.print();
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Initializing two tensors
const a = tf.tensor([0, 0, 0, 1]);
const b = tf.tensor([0.1, 0.8, 0.05, 0.05]);
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
// Print tensor
accuracy.print();
输出:
Tensor
[0, 0]
示例 2:
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Initializing two tensors
const a = tf.tensor([1, 0, 0, 1]);
const b = tf.tensor([1, 0.6, 0.01, 0.05]);
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
// Print tensor
accuracy.print();
输出:
Tensor
1
示例 3:
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Initializing two tensors
const a = tf.tensor([0, 0, 0, 1]);
const b = tf.tensor([0.1, 0.8, 0.05, 0.05]);
// Calling the .categoricalAccuracy() function
const accuracy = tf.metrics.categoricalAccuracy(a, b);
// Print tensor
accuracy.print();
输出:
Tensor
0
参考: https://js.tensorflow.org/api/latest/#metrics.categoricalAccuracy