主頁 > 知識(shí)庫 > TensorFlow中tf.batch_matmul()的用法

TensorFlow中tf.batch_matmul()的用法

熱門標(biāo)簽:朝陽手機(jī)外呼系統(tǒng) 北京電銷外呼系統(tǒng)加盟 儋州電話機(jī)器人 地圖標(biāo)注面積 北瀚ai電銷機(jī)器人官網(wǎng)手機(jī)版 市場上的電銷機(jī)器人 佛山400電話辦理 小蘇云呼電話機(jī)器人 所得系統(tǒng)電梯怎樣主板設(shè)置外呼

TensorFlow中tf.batch_matmul()用法

如果有兩個(gè)三階張量,size分別為

a.shape = [100, 3, 4]
b.shape = [100, 4, 5]
c = tf.batch_matmul(a, b)

則c.shape = [100, 3, 5] //將每一對 3x4 的矩陣與 4x5 的矩陣分別相乘。batch_size不變

100為張量的batch_size。剩下的兩個(gè)維度為數(shù)據(jù)的維度。

不過新版的tensorflow已經(jīng)移除了上面的函數(shù),使用時(shí)換為tf.matmul就可以了。與上面注釋的方式是同樣的。

附: 如果是更高維度。例如(a, b, m, n) 與(a, b, n, k)之間做matmul運(yùn)算。則結(jié)果的維度為(a, b, m, k)。

TensorFlow如何實(shí)現(xiàn)batch_matmul

我們知道,在tensorflow早期版本中有tf.batch_matmul()函數(shù),可以實(shí)現(xiàn)多維tensor和低維tensor的直接相乘,這在使用過程中非常便捷。

但是最新版本的tensorflow現(xiàn)在只有tf.matmul()函數(shù)可以使用,不過只能實(shí)現(xiàn)同維度的tensor相乘, 下面的幾種方法可以實(shí)現(xiàn)batch matmul的可能。

例如: tensor A(batch_size,m,n), tensor B(n,k),實(shí)現(xiàn)batch matmul 使得A * B。

方法1: 利用tf.matmul()

對tensor B 進(jìn)行增維和擴(kuò)展

A = tf.Variable(tf.random_normal(shape=(batch_size, 2, 3)))
B = tf.Variable(tf.random_normal(shape=(3, 5)))
B_exp = tf.tile(tf.expand_dims(B,0),[batch_size, 1, 1]) #先進(jìn)行增維再擴(kuò)展
C = tf.matmul(A, B_exp)

方法2: 利用tf.reshape()

對tensor A 進(jìn)行reshape操作,然后利用tf.matmul()

A = tf.Variable(tf.random_normal(shape=(batch_size, 2, 3)))
B = tf.Variable(tf.random_normal(shape=(3, 5)))
A = tf.reshape(A, [-1, 3])
C = tf.reshape(tf.matmul(A, B), [-1, 2, 5])

方法3: 利用tf.scan()

利用tf.scan() 對tensor按第0維進(jìn)行展開的特性

A = tf.Variable(tf.random_normal(shape=(batch_size, 2, 3)))
B = tf.Variable(tf.random_normal(shape=(3, 5)))
initializer = tf.Variable(tf.random_normal(shape=(2,5)))
C = tf.scan(lambda a,x: tf.matmul(x, B), A, initializer)

方法4: 利用tf.einsum()

A = tf.Variable(tf.random_normal(shape=(batch_size, 2, 3)))
B = tf.Variable(tf.random_normal(shape=(3, 5)))
C = tf.einsum('ijk,kl->ijl',A,B)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 教你使用TensorFlow2識(shí)別驗(yàn)證碼
  • pytorch_pretrained_bert如何將tensorflow模型轉(zhuǎn)化為pytorch模型
  • Tensorflow與RNN、雙向LSTM等的踩坑記錄及解決
  • tensorflow中的數(shù)據(jù)類型dtype用法說明
  • tensorflow基本操作小白快速構(gòu)建線性回歸和分類模型

標(biāo)簽:定西 龍巖 江蘇 商丘 寧夏 云南 酒泉 金融催收

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《TensorFlow中tf.batch_matmul()的用法》,本文關(guān)鍵詞  TensorFlow,中,tf.batch,matmul,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《TensorFlow中tf.batch_matmul()的用法》相關(guān)的同類信息!
  • 本頁收集關(guān)于TensorFlow中tf.batch_matmul()的用法的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章