Tensorflow.js tf.inTopKAsync() 函数
Tensorflow.js是由谷歌开发的开源库,用于在浏览器或Node环境中运行机器学习模型和深度学习神经网络。
.inTopKAsync() 函数 用于检查目标是否在给定的前K个预测中。
语法:
tf.inTopKAsync(predictions, targets, k?)
参数:
- 预测: 它是一个2D或更高维度的张量输入,其最后一个尺寸不小于 k ,可以是tf.Tensor、TypedArray或Array类型。
- 目标: 它是一个1D或更高维度的张量输入,可以是tf.Tensor、TypedArray或Array类型。
- k: 它是要考虑用于计算精度的顶部元素的替代数。默认值为1,类型为数字。
返回值: 返回Promise tf.Tensor对象。
示例1:
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining predictions and targets
const pred = tf.tensor2d([
[11, 22, 33, 55],
[33, 66, 22, -11]
]);
const targ = tf.tensor1d([1, 1]);
// Calling tf.inTopKAsync() method
const res = await tf.inTopKAsync(pred, targ);
// Printing output
res.print();
输出:
Tensor
[false, true]
示例2:
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling tf.inTopKAsync() method with
// all its parameters
const res = await tf.inTopKAsync(
tf.tensor2d([[1.1, 2.2], [3.3, 6.6]]),
tf.tensor1d([0, 1]), 2);
// Printing output
res.print();
输出:
Tensor
[true, true]
参考: https://js.tensorflow.org/api/latest/#inTopKAsync