淺談keras中的batch_dot,dot方法和TensorFlow的matmul
概述
在使用keras中的keras.backend.batch_dot和tf.matmul實(shí)現(xiàn)功能其實(shí)是一樣的智能矩陣乘法,比如A,B,C,D,E,F,G,H,I,J,K,L都是二維矩陣,中間點(diǎn)表示矩陣乘法,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)
會(huì)輸出:
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)
這個(gè)矩陣乘法會(huì)沿著兩個(gè)矩陣最后兩個(gè)維度進(jìn)行乘法,不是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)
keras的dot方法是Theano中的復(fù)制
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)
以上這篇淺談keras中的batch_dot,dot方法和TensorFlow的matmul就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決python刪除文件的權(quán)限錯(cuò)誤問題
下面小編就為大家分享一篇解決python刪除文件的權(quán)限錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04Pandas中Series和DataFrame的索引實(shí)現(xiàn)
這篇文章主要介紹了Pandas中Series和DataFrame的索引實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python實(shí)現(xiàn)簡(jiǎn)單socket通信的方法
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單socket通信的方法,結(jié)合實(shí)例形式分析了socket通信服務(wù)端與客戶端的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04python正則表達(dá)式匹配不包含某幾個(gè)字符的字符串方法
今天小編就為大家分享一篇python正則表達(dá)式匹配不包含某幾個(gè)字符的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07教你Pycharm安裝使用requests第三方庫的詳細(xì)教程
PyCharm安裝第三方庫是十分方便的,無需pip或其他工具,平臺(tái)就自帶了這個(gè)功能而且操作十分簡(jiǎn)便,今天通過本文帶領(lǐng)大家學(xué)習(xí)Pycharm安裝使用requests第三方庫的詳細(xì)教程,感興趣的朋友一起看看吧2021-07-07Python Web框架Flask中使用百度云存儲(chǔ)BCS實(shí)例
這篇文章主要介紹了Python Web框架Flask中使用百度云存儲(chǔ)BCS實(shí)例,本文調(diào)用了百度云存儲(chǔ)Python SDK中的相關(guān)類,需要的朋友可以參考下2015-02-02Python多項(xiàng)式回歸的實(shí)現(xiàn)方法
這篇文章主要介紹了Python多項(xiàng)式回歸的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例
角點(diǎn)通常被定義為兩條邊的交點(diǎn),本文主要介紹了OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03