2017-05-09 2221 views
4

我尝试在使用Keras的python程序中乘以两个矩阵。Keras中的矩阵乘法

import keras.backend as K 
import numpy as np 
A = np.random.rand(10,500) 
B = np.random.rand(500,6000) 

x = K.placeholder(shape=A.shape) 
y = K.placeholder(shape=B.shape) 
xy = K.dot(x, y) 

xy.eval(A,B) 

我知道这不能工作,但我也不知道如何才能使它工作。

+0

为什么你需要凯拉斯来做到这一点?你可以用numpy做到这一点 –

+0

是的,但我已经在我的程序中与Keras合作,并且Keras有GPU支持。 – snoozzz

回答

8

您需要使用变量而不是占位符。

import keras.backend as K 
import numpy as np 
A = np.random.rand(10,500) 
B = np.random.rand(500,6000) 

x = K.variable(value=A) 
y = K.variable(value=B) 

z = K.dot(x,y) 

# Here you need to use K.eval() instead of z.eval() because this uses the backend session 
K.eval(z) 
+1

太棒了!精美的作品。谢谢。 – snoozzz

+0

当然,如果它为你工作,请务必接受答案:)谢谢 –