Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix multiplication in Keras

Tags:

python

keras

I try to multiply two matrices in a python program using 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)

I know this cannot work, but I also don't know how I can make it work.

like image 758
snoozzz Avatar asked May 09 '17 13:05

snoozzz


1 Answers

You need to use a variable instead of a place holder.

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)
like image 137
Camron_Godbout Avatar answered Oct 23 '22 05:10

Camron_Godbout