Tensorflow.js tf.setdiff1dAsync()函数
Tensorflow.js是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
tf.setdiff1dAsync()函数用于查找两个指定数字列表之间的差异。
假设给定了两个张量“a”和“b”,那么这个函数返回一个张量输出,它表示“a”中存在但“b”中不存在的所有值。返回的张量输出按照表示“a”的相同顺序进行排序。此函数还返回张量输出中存在的数字的索引张量。
句法:
tf.setdiff1dAsync (a, b)
参数:此函数接受两个参数,如下所示:
- a:它是要保留的值的一维张量。
- b:它是输出中要排除的值的一维张量。它应该具有与“a”相同的数据类型。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Initializing two Tensors
const a = [10, 20, 30, 40, 50, 60];
const b = [20, 40, 50];
// Calling the .setdiff1dAsync() function over
// the above two specified Tensors as parameters
// and returns the Tensor out and indices for
// the same
const [out, indices] = await tf.setdiff1dAsync(a, b);
// Getting the Tensor of values present in
// Tensor "a" but not in "b"
out.print();
// Getting the Tensor of indices for the
// returned Tensor's values
indices.print();
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Using two Tensors as the parameters for the
// .setdiff1dAsync() function and
// returns the Tensor out and indices
const [out, indices] = await
tf.setdiff1dAsync([0.1, 0, 7.0, 7.1], [.1, 7]);
// Getting the Tensor of values present in
// Tensor first Tensor parameter
// but not in second one
out.print();
// Getting the Tensor of indices for the
// returned Tensor's values
indices.print();
输出:
Tensor
[10, 30, 60]
Tensor
[0, 2, 5]
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Using two Tensors as the parameters for the
// .setdiff1dAsync() function and
// returns the Tensor out and indices
const [out, indices] = await
tf.setdiff1dAsync([0.1, 0, 7.0, 7.1], [.1, 7]);
// Getting the Tensor of values present in
// Tensor first Tensor parameter
// but not in second one
out.print();
// Getting the Tensor of indices for the
// returned Tensor's values
indices.print();
输出:
Tensor
[0, 7.0999999]
Tensor
[1, 3]
参考: https://js.tensorflow.org/api/latest/#setdiff1dAsync