Keras中的矩阵乘法

sno*_*zzz 7 python keras

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

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)
Run Code Online (Sandbox Code Playgroud)

我知道这不起作用,但我也不知道如何让它发挥作用.

Cam*_*out 17

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

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)
Run Code Online (Sandbox Code Playgroud)