理解keras中的batch_dot,dot方法和TensorFlow的matmul

理解keras中的K.batch_dot和TensorFlow的tf.matmul

概述

在使用keras中的keras.backend.batch_dot和tf.matmul实现功能其实是一样的智能矩阵乘法,比如 A , B , C , D , E , F , G , H , I , J , K , L A,B,C,D,E,F,G,H,I, J,K,L A,B,C,D,E,F,G,HI,J,K,L都是二维矩阵,中间点表示矩阵乘法, A G AG AG表示矩阵 A A A G G G矩阵乘法( A A A的列维度等于 G G G行维度), W X = Z WX=Z 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)
print(c.shape)#(2, 3, 7, 5)

这个矩阵乘法会沿着两个矩阵最后两个维度进行乘法,不是element-wise矩阵乘法

from keras import backend as K
a = K.ones((1, 2, 3 , 4))
b = K.ones((8, 7, 4, 5))
c = K.dot(a, b)
print(c.shape)#(1, 2, 3, 8, 7, 5)

c a , b , c , i , j , k = ∑ r w a , b , c , r x i , j , r , k c_{a,b,c,i,j,k}=\sum_rw_{a,b,c,r}x_{i,j,r,k} ca,b,c,i,j,k=rwa,b,c,rxi,j,r,k

keras的dot方法是Theano中的复制

from keras import backend as K
a = K.ones((1, 2, 4))
b = K.ones((8, 7, 4, 5))
c = K.dot(a, b)
print(c.shape)# (1, 2, 8, 7, 5).
from keras import backend as K
a = K.ones((9, 8, 7, 4, 2))
b = K.ones((9, 8, 7, 2, 5))
c = K.batch_dot(a, b)
print(c.shape) #(9, 8, 7, 4, 5)

或者

import tensorflow as tf
a = tf.ones((9, 8, 7, 4, 2))
b = tf.ones((9, 8, 7, 2, 5))
c = tf.matmul(a, b)
print(c.shape) #(9, 8, 7, 4, 5)

参考

[1]: tf.keras.backend.batch_dot函数
[2]: keras batch_dot
[3]: Understand batch matrix multiplication
[4]: batch_dot

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值