在python中,假设
a = np.array(range(0,12)).reshape(2,2,3)
b = np.array(range(0,6)).reshape(3,2)
c = np.matmul(a,b) // a @ b我们有
a: array([[[ 0, 1, 2],
[ 3, 4, 5]],
[[ 6, 7, 8],
[ 9, 10, 11]]])
b: array([[0, 1],
[2, 3],
[4, 5]])
c: array([[[10, 13],
[28, 40]],
[[46, 67],
[64, 94]]])有人能帮助我在没有for循环的情况下在java nd4j中实现等价的操作吗?我试过broadcast.mul,但事实证明,broadcast.mul是按元素进行乘法的。我没有找到任何关于mmul的广播操作。
发布于 2018-12-05 15:24:54
我自己想出来的。答案如下所示,以防有人需要答案。利用Nd4j.tensorMmul,可以很容易地实现矩阵广播。例如:
val a = Nd4j.create(0d to 11d by 1d toArray, Array[Int](2, 2, 3))
val b = Nd4j.create(0d to 5d by 1d toArray, Array[Int](3, 2))
Nd4j.tensorMmul(a, b, Array(Array(2), Array(0))) // matrix broadcast这是scala的代码。对于java,只需更改代码就可以创建数组。
https://stackoverflow.com/questions/53626094
复制相似问题