tensorflow.js - How to get indices of a specific value in a tensor in tensorflow-js?
问题描述
For example, If I have a 2D tensor of [[1,3],[2,1]]
, how can I get indices of the value 1
? (It should return [[0,0],[1,1]]
).
I took a look into tf.where
but the API is complicated and I don't think that would solve the problem for me
解决方案
You can achieve this using tf.whereAsync.
Just create a mask which checks if the values in the input Tensor are of the value 1 and turn them into boolean values.
Mask:
"Tensor
[[true , false],
[false, true ]]"
tf.whereAsync()
returns the coordinates of true elements of the condition which in this case come from the mask.
(async function getData() {
const x = tf.tensor2d([[1, 3], [2, 1]])
const mask = x.equal([1]).asType('bool');
const coords = await tf.whereAsync(mask);
coords.print();
}());
Input:
"Tensor
[[1, 3],
[2, 1]]"
Output:
"Tensor
[[0, 0],
[1, 1]]"
推荐阅读
- time-series - 如何将数值运算应用于预测对象?
- api - 反应原生多边形位置
- c# - C# - 为什么使用 EventHandler 会出错
? - amazon-web-services - AppSync - 当数组属性具有与之关联的`@key`指令时,无法使用数组创建架构
- java - 将 Pulsar appender 与 Ignite logger 一起使用时,具有名称的 Producer 已连接到主题
- c - 如何在微控制器固件中实现延迟
- php - 无法使用 Symfony 进程从 PHP-FPM 运行任何命令
- linkedin - LinkedIn Share API 为我们的 URL 抛出 500 错误
- angular - 如何处理 spring security + angular 中的 401 错误?
- c++ - LNK1104 cannot open file boost_thread-vc140-mt-gd-1_61.lib