矩阵乘法但 min() 的总和而不是乘积的总和

matrix multiplication but sum of min() instead of sum of product

我正在寻找实现以下目标的一系列矩阵运算:

给定例如输入矩阵

0 0 0 0
1 1 1 1
2 2 4 4
3 0 5 5

将输出以下内容:

0 0 0 0
0 4 4 4
0 4 12 10
0 4 10 13

应该怎么办? 很清楚矩阵乘法是如何工作的,只有 1 个小差异需要解释。矩阵乘法是这样的:

resultMatrix[行][列] = sum(A[行][每列 x]*B[行 x][列])

操作顺序应如下:

resultMatrix[行][列] = sum(min(A[行][每列 x],B[行 x][列]))
其中 B 是 A 的转置。

我可以使用哪些 TensorflowJS 函数来实现此目的?

感谢@jdehesa 的回答,当在 2881x2980 形状上使用您的答案中的代码时,我显然达到了极限并且 TensorFlowJS 抛出以下错误:

Error: Requested texture size [193027x128140] greater than WebGL maximum on this browser / GPU [16384x16384].
    at validateTextureSize (tf-core.esm.js?45ef:17)
    at createAndConfigureTexture (tf-core.esm.js?45ef:17)
    at createFloat32MatrixTexture (tf-core.esm.js?45ef:17)
    at e.createFloat32MatrixTexture (tf-core.esm.js?45ef:17)
    at e.acquireTexture (tf-core.esm.js?45ef:17)
    at e.acquireTexture (tf-core.esm.js?45ef:17)
    at e.uploadToGPU (tf-core.esm.js?45ef:17)
    at e.compileAndRun (tf-core.esm.js?45ef:17)
    at e.minimum (tf-core.esm.js?45ef:17)
    at ENV.engine.runKernel.$a (tf-core.esm.js?45ef:17)

有什么 batch/optimization 想法吗?

以下是您可以如何做到这一点:

const a = tf.tensor2d(
  [[0, 0, 0, 0],
   [1, 1, 1, 1],
   [2, 2, 4, 4],
   [3, 0, 5, 5]]);
const b = a.transpose();
const m = a.expandDims(-1).minimum(b);
const result = m.sum(1);

result.print();

输出:

"Tensor
    [[0, 0, 0 , 0 ],
     [0, 4, 4 , 3 ],
     [0, 4, 12, 10],
     [0, 3, 10, 13]]"