如何产生一个包含非零元素索引的张量?

用户名

我有一个张量

 [[0, 0, 0, 1, 0, 0],
  [0, 3, 0, 0, 0, 1],
  [0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0],
  [0, 0, 0, 2, 0, 0],
  [0, 0, 0, 0, 0, 0]]

如何生成仅包含前者非零索引的张量?

[[0,3],[1,1],[1,5],[4,3]]

我正在使用tfjs,因此它似乎没有boolean_mask或其他似乎在索引中使用的其他有用功能。是否可以.data()在数组上不使用转义填充和映射/过滤功能?(我只是将tensorflow用于其快速线性代数方法,这样就不会很糟糕,对吧?)

教育

没错,目前没有运算符可以使用掩码定义分区。您可以将此答案视为解决方法。但是,即使使用掩码,这里的问题也无法轻易解决,因为创建分区的前提是事先知道要检索的索引。

为了解决这个问题,可以在输入张量和具有相同形状且所有元素都等于1的张量之间使用逻辑和。这很有用,因为如果返回张量,则返回与输入形状相同形状的张量,值为0。初始元素为0,否则为1.使用argMax,可以检索等于1的元素的索引

let t = tf.tensor2d([[0, 0, 0, 1, 0, 0], [0, 3, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 2, 0, 0], [0, 0, 0, 0, 0, 0]])
let a = t.as1D()

const b = tf.cast(a, 'bool')
let helper = b.logicalAnd(tf.cast(tf.ones([a.size]), 'bool'))
const n = helper.sum().dataSync()[0]
const noNull = []
for(let i = 0; i < n ; i++) {
  let ind = tf.argMax(helper)
  let s = ind.dataSync()[0]
  noNull.push(tf.argMax(helper).dataSync()[0])
  if (s === 0) {
     const [x, y] = helper.split([1, helper.size - 1])
     helper = tf.concat(tf.tensor1d([0]), y)
  } else if (s === helper.size) {
    const [x, y] = helper.split([helper.size -1, 1])
    helper = tf.concat(x, tf.tensor1d([0]))
  } else {
    const [x, _, y] = helper.split([s, 1, helper.size - s - 1])
    helper = tf.concat([x,tf.tensor1d([0]), y])
  }
}

const indexToCoords = (index, shape) => {
  const pseudoShape = shape.map((a, b, c) => c.slice(b + 1).reduce((a, b) => a * b, 1))
  let coords = []
  let ind = index
  for (let i = 0; i < shape.length; i++) {
    coords.push(Math.floor(ind / pseudoShape[i]))
    ind = ind % pseudoShape[i]
  }
  return coords
}

const coords = noNull.map(e => indexToCoords(e, t.shape))

console.log(coords)
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
  </head>

  <body>
  </body>
</html>

在此线程之后,有一种更简单的方法可以实现相同的目的

(async function() {
  const x = tf.tensor2d(
    [[0, 0, 0, 1, 0, 0],
    [0, 3, 0, 0, 0, 1],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 2, 0, 0],
    [0, 0, 0, 0, 0, 0]]);

  const mask = x.greater([0]).asType('bool');
  const coords = await tf.whereAsync(mask);

  coords.print();
}());
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
  </head>

  <body>
  </body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在熊猫组中查找第一个非零元素

如何获得最后一个非零元素的位置

返回r中的最后一个非零元素索引

将每行的最后一个非零元素设置为零-NumPy

返回列表中最后一个非零元素的索引

从右到第一个非零元素出现之前,我如何计算具有零值的连续列数

从向量中擦除所有元素,直到第一个非零元素C ++

识别由熊猫中的多个列组成的组中的第一个非零元素

在矩阵的每一行仅生成一个非零元素

Pytorch:我如何在2D张量的每一行中找到第一个非零元素的索引?

在张量流中创建除一个非零元素外的所有零

R:如何按组查找数据框中的第一个非零元素

识别熊猫组中的第一个非零元素

如何找到至少包含一个非零元素的列的索引?

Python-查找每个元素左侧第一个非零元素的索引

如何找到第一个非零元素和最后一个非零元素并修剪它

张量流张量中的唯一非零元素

如何获取一个numpy数组的非零元素的数量?

Scala:从第一个非零元素中切出一个列表

用数组中的下一个非零元素替换0

一种返回Perl中最后一个非零元素的索引的优雅方法?

R-在data.table中查找第一个非零元素的索引

返回矩阵每一行的最后K个非零元素的索引

在std :: vector中找到最后一个非零元素

创建一个张量,其中另一个张量在 Pytorch 中具有非零元素

R:随机采样向量中的一个非零元素,并用0替换其他元素

查找从列底部开始的第一个非零元素的索引

dplyr:按组查找第一个非零元素和最后一个非零元素和修剪向量

每行的第一个非零元素