TensorflowJS:计算多个张量之间的距离或相似度的最佳方法?

时间:2019-06-10 06:55:47

标签: javascript tensorflow tensorflowjs

我正在编写一种算法,该算法需要比较两个不同的张量数组datasetcentroidsdataset的张量比centroids多+1000,并且所有张量的维数相同([1 x n])。

我当前的解决方案(下面的代码)如下:对于dataset中的每个张量,找到该张量与centroids中所有张量之间的距离,然后存储最近的{{1 }}。

centroid

这有效,但是速度很慢。这也是一个嵌套循环,感觉效率低下。

是否有一种更好的方法来使用tensorflowjs(即通过某种相似性矩阵?)。

谢谢!

P.S。 -如果特定的解决方案需要特定的距离函数,那么我完全愿意改变我的观点。目前,我的距离函数如下:

dataset.forEach(d => {
      const distances = centroids.map(centroid => getEuclidianDistance(d.tensor, centroid));
      const centroidIndex = distances.indexOf(Math.min(...distances));
      d.centroid = centroidIndex;
    })

1 个答案:

答案 0 :(得分:1)

几个月前,我也有类似的要求,在给定2d点的情况下,我希望从一系列最接近该点的线段中查找。我试图强迫tensorflowjs有效地执行此操作,但最终偶然发现了gpu.js,后者更适合于编译自定义GPU内核函数。

在下面的示例中,我有代表11(X,Y)坐标的数组和另一对代表5(X,Y)坐标的数组。结果将是一个矩阵11x5,该矩阵计算两组点之间的每个距离。关键功能是“内核”,由gpu.js编译以在GPU内核上运行,并且实质上计算了从11个坐标和5个坐标得到的一对点之间的距离。从理论上讲,此内核功能将放置在许多GPU内核上,以加快性能。即,在这种情况下,一次执行全部55次。 (我说“从理论上讲”,因为据我所知gpu.js利用了webGL着色器贴图功能,并且不能完全确定堆栈中涉及的虚拟化层的影响,这些虚拟化层会导致GPU内核实际执行这项工作。 ..)

结果是一个11x5矩阵,其中包含到每个点对组合的距离,然后将此11x5矩阵通过管道传递到“ kernelMin”,这会变慢一些,因为它正在遍历结果中寻找最小值,并捕获最小值的索引。话虽如此,应该有11个并发GPU内核在努力寻找5个坐标中最接近的坐标。

const kernel = gpu.createKernel(function(x0, y0, x1, y1) {
  let dx = x1[this.thread.y][0] - x0[0][this.thread.x];
  let dy = y1[this.thread.y][0] - y0[0][this.thread.x];
  return Math.sqrt(dx * dx + dy * dy);
}).setPipeline(true).setOutput([11,5]);

const result1 = kernel(
  GPU.input(
    new Float32Array([0,10,20,30,40,50,60,70,80,90,100]),
    [11,1]
  ),
  GPU.input(
    new Float32Array([100,100,100,100,100,100,100,100,100,100,100]),
    [11,1]
  ),
  GPU.input(
    new Float32Array([0,30,50,70,100]),
    [1,5]
  ),
  GPU.input(
    new Float32Array([10,10,10,10,10]),
    [1,5]
  )
);

console.log(result1.toArray());

const kernelMin = gpu.createKernel(function(a) {
  let minVal = 1000000;
  let minIdx = 0;
  for (let y = 0; y < 5; y++) {
    if (a[y][this.thread.x] < minVal) {
      minVal = a[y][this.thread.x];
      minIdx = y;
    }
  }
  return [minVal,minIdx];
}).setOutput([11]);

const result2 = kernelMin(result1);

console.log(result2);

最终输出是...

0: Float32Array(2) [90, 0]
1: Float32Array(2) [90.55384826660156, 0]
2: Float32Array(2) [90.55384826660156, 1]
3: Float32Array(2) [90, 1]
4: Float32Array(2) [90.55384826660156, 1]
5: Float32Array(2) [90, 2]
6: Float32Array(2) [90.55384826660156, 2]
7: Float32Array(2) [90, 3]
8: Float32Array(2) [90.55384826660156, 3]
9: Float32Array(2) [90.55384826660156, 4]
10: Float32Array(2) [90, 4]

请注意,为清楚起见,我已将矩阵大小硬编码到示例中。 Gpu.js显然接受变量。另外,根据您的情况,根据矩阵的大小,您可能必须根据容纳距离的完整交叉矩阵所需的GPU RAM数量将问题分解为多个块。

我意识到这不是tensorflowjs,但是希望这会有所帮助。

编辑-通过TensorFlow.JS

花了几分钟移植到tensorflow.js。核心概念是建立x和y值的矩阵,以准备进行大量计算。

const x0 = tf.tensor1d([0,10,20,30,40,50,60,70,80,90,100]);
const y0 = tf.tensor1d([100,100,100,100,100,100,100,100,100,100,100]);

const x1 = tf.tensor1d([0,30,50,70,100]);
const y1 = tf.tensor1d([10,10,10,10,10]);

const x0mat = x0.tile([5]).reshape([5,11]);
const y0mat = y0.tile([5]).reshape([5,11]);
const x1mat = x1.tile([11]).reshape([11,5]).transpose();
const y1mat = y1.tile([11]).reshape([11,5]).transpose();

x0mat.print();
x1mat.print();
const xDeltas = x1mat.squaredDifference(x0mat);

y0mat.print();
y1mat.print();
const yDeltas = y1mat.squaredDifference(y0mat);

const distance = xDeltas.add(yDeltas).sqrt();
distance.print();

distance.argMin(1).print();
distance.min(1).print();

结果...

Tensor - x0mat
    [[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
     [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
     [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
     [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
     [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]]
Tensor - x1mat
    [[0  , 0  , 0  , 0  , 0  , 0  , 0  , 0  , 0  , 0  , 0  ],
     [30 , 30 , 30 , 30 , 30 , 30 , 30 , 30 , 30 , 30 , 30 ],
     [50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 , 50 ],
     [70 , 70 , 70 , 70 , 70 , 70 , 70 , 70 , 70 , 70 , 70 ],
     [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]]
Tensor - y0mat
    [[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
     [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
     [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
     [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100],
     [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]]
Tensor - y1mat
    [[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
     [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
     [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
     [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
     [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]]
Tensor - distance
    [[90         , 90.5538483 , 92.1954422 , 94.8683319, 98.4885788 , 102.9562988, 108.1665344, 114.01754 , 120.415947 , 127.2792206, 134.5362396],
     [94.8683319 , 92.1954422 , 90.5538483 , 90        , 90.5538483 , 92.1954422 , 94.8683319 , 98.4885788, 102.9562988, 108.1665344, 114.01754  ],
     [102.9562988, 98.4885788 , 94.8683319 , 92.1954422, 90.5538483 , 90         , 90.5538483 , 92.1954422, 94.8683319 , 98.4885788 , 102.9562988],
     [114.01754  , 108.1665344, 102.9562988, 98.4885788, 94.8683319 , 92.1954422 , 90.5538483 , 90        , 90.5538483 , 92.1954422 , 94.8683319 ],
     [134.5362396, 127.2792206, 120.415947 , 114.01754 , 108.1665344, 102.9562988, 98.4885788 , 94.8683319, 92.1954422 , 90.5538483 , 90         ]]
Tensor - argMin of distance
    [0, 3, 5, 7, 10]
Tensor - min of distance
    [90, 90, 90, 90, 90]

该代码按步骤进行分解以显示基本概念。我相信它可以进一步压缩和优化。

相关问题