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

时间:2018-12-17 23:38:57

标签: tensorflow math matrix linear-algebra tensorflow.js

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

例如输入矩阵

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个小的差异可以解释。矩阵乘法的工作原理如下:
Matrix multiplication
resultMatrix [row] [column] = sum(A [row] [每列x] * B [row x] [column])

操作顺序应执行以下操作:

resultMatrix [row] [column] = sum(min(A [row] [每列x],B [row x] [column]))
其中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)

有任何批处理/优化建议吗?

1 个答案:

答案 0 :(得分:3)

这是您可以执行的操作:

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]]"