没有合适的资源?快使用搜索试试~ 我知道了~
浅谈keras中的batch_dot,dot方法和TensorFlow的matmul
4 下载量 71 浏览量
2020-12-17
10:54:34
上传
评论
收藏 55KB PDF 举报
温馨提示
试读
2页
概述 在使用keras中的keras.backend.batch_dot和tf.matmul实现功能其实是一样的智能矩阵乘法,比如A,B,C,D,E,F,G,H,I,J,K,L都是二维矩阵,中间点表示矩阵乘法,AG 表示矩阵A 和G 矩阵乘法(A 的列维度等于G 行维度),WX=Z import keras.backend as K import tensorflow as tf import numpy as np w = K.variable(np.random.randint(10,size=(10,12,4,5))) k = K.variable(np.random.randint(
资源详情
资源评论
资源推荐
浅谈浅谈keras中的中的batch_dot,dot方法和方法和TensorFlow的的matmul
概述概述
在使用keras中的keras.backend.batch_dot和tf.matmul实现功能其实是一样的智能矩阵乘法,比如A,B,C,D,E,F,G,H,I,J,K,L
都是二维矩阵,中间点表示矩阵乘法,AG 表示矩阵A 和G 矩阵乘法(A 的列维度等于G 行维度),WX=Z
import keras.backend as K
import tensorflow as tf
import numpy as np
w = K.variable(np.random.randint(10,size=(10,12,4,5)))
k = K.variable(np.random.randint(10,size=(10,12,5,8)))
z = K.batch_dot(w,k)
print(z.shape) #(10, 12, 4, 8)
import keras.backend as K
import tensorflow as tf
import numpy as np
w = tf.Variable(np.random.randint(10,size=(10,12,4,5)),dtype=tf.float32)
k = tf.Variable(np.random.randint(10,size=(10,12,5,8)),dtype=tf.float32)
z = tf.matmul(w,k)
print(z.shape) #(10, 12, 4, 8)
示例
from keras import backend as K
a = K.ones((3,4,5,2))
b = K.ones((2,5,3,7))
c = K.dot(a, b)
print(c.shape)
会输出:
ValueError: Dimensions must be equal, but are 2 and 3 for ‘MatMul’ (op: ‘MatMul’) with input shapes: [60,2],
[3,70].
from keras import backend as K
a = K.ones((3,4))
b = K.ones((4,5))
c = K.dot(a, b)
print(c.shape)#(3,5)
或者
import tensorflow as tf
a = tf.ones((3,4))
b = tf.ones((4,5))
c = tf.matmul(a, b)
print(c.shape)#(3,5)
如果增加维度:
from keras import backend as K
a = K.ones((2,3,4))
b = K.ones((7,4,5))
c = K.dot(a, b)
weixin_38609571
- 粉丝: 8
- 资源: 908
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0