javascript – 如何生成包含非零元素索引的张量?

我有张力之类的

 [[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()逃生舱口和数组上的映射/过滤吗? (我只是使用张量流来实现其快速线性代数方法,这样就不会是最差的,对吧?)

最佳答案 是的,目前没有操作符可以使用掩码来定义分区.您可以将此
answer视为解决方法.但即使使用掩码,这里的问题也无法轻易解决,因为创建分区假设事先知道要检索的索引.

为了解决这个问题,可以在输入张量和相同形状的张量之间使用logicalAnd,所有元素都等于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/tfjs@0.13.0"> </script>
  </head>

  <body>
  </body>
</html>

thread之后,有一种更简单的方法来实现同样的目标

(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/tfjs@0.13.0"> </script>
  </head>

  <body>
  </body>
</html>
点赞