婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

TensorFlow中tf.batch_matmul()的用法

熱門標簽:朝陽手機外呼系統 北京電銷外呼系統加盟 儋州電話機器人 地圖標注面積 北瀚ai電銷機器人官網手機版 市場上的電銷機器人 佛山400電話辦理 小蘇云呼電話機器人 所得系統電梯怎樣主板設置外呼

TensorFlow中tf.batch_matmul()用法

如果有兩個三階張量,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。剩下的兩個維度為數據的維度。

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

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

TensorFlow如何實現batch_matmul

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

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

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

方法1: 利用tf.matmul()

對tensor B 進行增維和擴展

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]) #先進行增維再擴展
C = tf.matmul(A, B_exp)

方法2: 利用tf.reshape()

對tensor A 進行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維進行展開的特性

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)

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

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

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

巨人網絡通訊聲明:本文標題《TensorFlow中tf.batch_matmul()的用法》,本文關鍵詞  TensorFlow,中,tf.batch,matmul,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《TensorFlow中tf.batch_matmul()的用法》相關的同類信息!
  • 本頁收集關于TensorFlow中tf.batch_matmul()的用法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 武威市| 名山县| 黄陵县| 巴彦县| 仙居县| 黔西县| 湾仔区| 凤台县| 竹山县| 屏山县| 旬邑县| 新绛县| 上林县| 远安县| 克什克腾旗| 大石桥市| 克山县| 大余县| 吉木萨尔县| 上林县| 色达县| 津南区| 蚌埠市| 正阳县| 陆河县| 南华县| 都安| 左权县| 临高县| 莆田市| 衡水市| 永年县| 鄂温| 固始县| 二手房| 东台市| 台州市| 上饶市| 临沂市| 临桂县| 塘沽区|