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>