Tensorfow.js 求张量的中值

Tensorfow.js find median of tensor

我需要在 angular 应用程序中找到通过 tensorflow.js 读取为张量的图像的中值。谁能建议如何在 tfjs 中找到中位数?

喜欢-

let x = tf.tensor([1, 2, 3, 4, 5, 8, 9]);
console.log(x.median());

这应该打印 4。

我尝试在 javascript 中找到等效的 tensorflow 概率统计,但到目前为止没有运气。

中位数是序列排列后的中间数。张量需要先排序。索引 sizeTensor / 2 处的值将是中值。

t= tf.tensor([1, 2, 3, 4, 5, 8, 9]);
tf.topk(t, t.size).values.slice(t.size / 2, 1).print() // will print 4

为了使上面的内容与高维张量一起工作——例如图像,我们需要首先重塑为一维张量

给定img图像的张量

t = img.reshape([-1])
// do the same as above