Keras中的矩阵乘法 [英] Matrix multiplication in Keras

查看:372
本文介绍了Keras中的矩阵乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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)

这篇关于Keras中的矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆