最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加)

 更新時(shí)間:2020年02月05日 16:15:02   作者:Kenn7  
今天小編就為大家分享一篇Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

Tensorflow二維、三維、四維矩陣運(yùn)算(矩陣相乘,點(diǎn)乘,行/列累加)

1. 矩陣相乘

根據(jù)矩陣相乘的匹配原則,左乘矩陣的列數(shù)要等于右乘矩陣的行數(shù)。

在多維(三維、四維)矩陣的相乘中,需要最后兩維滿足匹配原則。

可以將多維矩陣?yán)斫獬桑海ň仃嚺帕?,矩陣),即后兩維為矩陣,前面的維度為矩陣的排列。

比如對(duì)于(2,2,4)來說,視為2個(gè)(2,4)矩陣。

對(duì)于(2,2,2,4)來說,視為2*2個(gè)(2,4)矩陣。

import tensorflow as tf
 
a_2d = tf.constant([1]*6, shape=[2, 3])
b_2d = tf.constant([2]*12, shape=[3, 4])
c_2d = tf.matmul(a_2d, b_2d)
a_3d = tf.constant([1]*12, shape=[2, 2, 3])
b_3d = tf.constant([2]*24, shape=[2, 3, 4])
c_3d = tf.matmul(a_3d, b_3d)
a_4d = tf.constant([1]*24, shape=[2, 2, 2, 3])
b_4d = tf.constant([2]*48, shape=[2, 2, 3, 4])
c_4d = tf.matmul(a_4d, b_4d)
 
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print("# {}*{}={} \n{}".
  format(a_2d.eval().shape, b_2d.eval().shape, c_2d.eval().shape, c_2d.eval()))
 print("# {}*{}={} \n{}".
  format(a_3d.eval().shape, b_3d.eval().shape, c_3d.eval().shape, c_3d.eval()))
 print("# {}*{}={} \n{}".
  format(a_4d.eval().shape, b_4d.eval().shape, c_4d.eval().shape, c_4d.eval()))

2. 點(diǎn)乘

點(diǎn)乘指的是shape相同的兩個(gè)矩陣,對(duì)應(yīng)位置元素相乘,得到一個(gè)新的shape相同的矩陣。

a_2d = tf.constant([1]*6, shape=[2, 3])
b_2d = tf.constant([2]*6, shape=[2, 3])
c_2d = tf.multiply(a_2d, b_2d)
a_3d = tf.constant([1]*12, shape=[2, 2, 3])
b_3d = tf.constant([2]*12, shape=[2, 2, 3])
c_3d = tf.multiply(a_3d, b_3d)
a_4d = tf.constant([1]*24, shape=[2, 2, 2, 3])
b_4d = tf.constant([2]*24, shape=[2, 2, 2, 3])
c_4d = tf.multiply(a_4d, b_4d)
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print("# {}*{}={} \n{}".
  format(a_2d.eval().shape, b_2d.eval().shape, c_2d.eval().shape, c_2d.eval()))
 print("# {}*{}={} \n{}".
  format(a_3d.eval().shape, b_3d.eval().shape, c_3d.eval().shape, c_3d.eval()))
 print("# {}*{}={} \n{}".
  format(a_4d.eval().shape, b_4d.eval().shape, c_4d.eval().shape, c_4d.eval()))

另外,點(diǎn)乘的其中一方可以是一個(gè)常數(shù),也可以是一個(gè)和矩陣行向量等長(即列數(shù))的向量。

因?yàn)樵邳c(diǎn)乘過程中,會(huì)自動(dòng)將常數(shù)或者向量進(jìn)行擴(kuò)維。

a_2d = tf.constant([1]*6, shape=[2, 3])
k = tf.constant(2)
l = tf.constant([2, 3, 4])
b_2d_1 = tf.multiply(k, a_2d) # tf.multiply(a_2d, k) is also ok
b_2d_2 = tf.multiply(l, a_2d) # tf.multiply(a_2d, l) is also ok
a_3d = tf.constant([1]*12, shape=[2, 2, 3])
b_3d_1 = tf.multiply(k, a_3d) # tf.multiply(a_3d, k) is also ok
b_3d_2 = tf.multiply(l, a_3d) # tf.multiply(a_3d, l) is also ok
a_4d = tf.constant([1]*24, shape=[2, 2, 2, 3])
b_4d_1 = tf.multiply(k, a_4d) # tf.multiply(a_4d, k) is also ok
b_4d_2 = tf.multiply(l, a_4d) # tf.multiply(a_4d, l) is also ok
 
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print("# {}*{}={} \n{}".
  format(k.eval().shape, a_2d.eval().shape, b_2d_1.eval().shape, b_2d_1.eval()))
 print("# {}*{}={} \n{}".
  format(l.eval().shape, a_2d.eval().shape, b_2d_2.eval().shape, b_2d_2.eval()))
 print("# {}*{}={} \n{}".
  format(k.eval().shape, a_3d.eval().shape, b_3d_1.eval().shape, b_3d_1.eval()))
 print("# {}*{}={} \n{}".
  format(l.eval().shape, a_3d.eval().shape, b_3d_2.eval().shape, b_3d_2.eval()))
 print("# {}*{}={} \n{}".
  format(k.eval().shape, a_4d.eval().shape, b_4d_1.eval().shape, b_4d_1.eval()))
 print("# {}*{}={} \n{}".
  format(l.eval().shape, a_4d.eval().shape, b_4d_2.eval().shape, b_4d_2.eval()))

4. 行/列累加

a_2d = tf.constant([1]*6, shape=[2, 3])
d_2d_1 = tf.reduce_sum(a_2d, axis=0)
d_2d_2 = tf.reduce_sum(a_2d, axis=1)
a_3d = tf.constant([1]*12, shape=[2, 2, 3])
d_3d_1 = tf.reduce_sum(a_3d, axis=1)
d_3d_2 = tf.reduce_sum(a_3d, axis=2)
a_4d = tf.constant([1]*24, shape=[2, 2, 2, 3])
d_4d_1 = tf.reduce_sum(a_4d, axis=2)
d_4d_2 = tf.reduce_sum(a_4d, axis=3)
 
with tf.Session() as sess:
 tf.global_variables_initializer().run()
 print("# a_2d 行累加得到shape:{}\n{}".format(d_2d_1.eval().shape, d_2d_1.eval()))
 print("# a_2d 列累加得到shape:{}\n{}".format(d_2d_2.eval().shape, d_2d_2.eval()))
 print("# a_3d 行累加得到shape:{}\n{}".format(d_3d_1.eval().shape, d_3d_1.eval()))
 print("# a_3d 列累加得到shape:{}\n{}".format(d_3d_2.eval().shape, d_3d_2.eval()))
 print("# a_4d 行累加得到shape:{}\n{}".format(d_4d_1.eval().shape, d_4d_1.eval()))
 print("# a_4d 列累加得到shape:{}\n{}".format(d_4d_2.eval().shape, d_4d_2.eval()))

以上這篇Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python采集天天基金數(shù)據(jù)掌握最新基金動(dòng)向

    Python采集天天基金數(shù)據(jù)掌握最新基金動(dòng)向

    這篇文章主要介紹了Python采集天天基金數(shù)據(jù)掌握最新基金動(dòng)向,本次案例實(shí)現(xiàn)流程為發(fā)送請(qǐng)求、獲取數(shù)據(jù)、解析數(shù)據(jù)、多頁爬取、保存數(shù)據(jù),接下來來看看具體的操作過程吧
    2022-01-01
  • Pytorch數(shù)據(jù)類型與轉(zhuǎn)換(torch.tensor,torch.FloatTensor)

    Pytorch數(shù)據(jù)類型與轉(zhuǎn)換(torch.tensor,torch.FloatTensor)

    這篇文章主要介紹了Pytorch數(shù)據(jù)類型轉(zhuǎn)換(torch.tensor,torch.FloatTensor),之前遇到轉(zhuǎn)為tensor轉(zhuǎn)化為浮點(diǎn)型的問題,今天整理下,我只講幾個(gè)我常用的,對(duì)Pytorch數(shù)據(jù)類型轉(zhuǎn)換相關(guān)知識(shí)感興趣的朋友一起看看吧
    2023-02-02
  • 詳解Appium+Python之生成html測(cè)試報(bào)告

    詳解Appium+Python之生成html測(cè)試報(bào)告

    這篇文章主要介紹了詳解Appium+Python之生成html測(cè)試報(bào)告,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • 在pycharm中文件取消用 pytest模式打開的操作

    在pycharm中文件取消用 pytest模式打開的操作

    這篇文章主要介紹了在pycharm中文件取消用 pytest模式打開的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Python中字典的基本知識(shí)初步介紹

    Python中字典的基本知識(shí)初步介紹

    這篇文章主要介紹了Python中字典的基本知識(shí)初步介紹,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • python使用pil生成圖片驗(yàn)證碼的方法

    python使用pil生成圖片驗(yàn)證碼的方法

    這篇文章主要介紹了python使用pil生成圖片驗(yàn)證碼的方法,涉及Python操作Image,ImageDraw,ImageFont等模塊的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • python生成密碼字典的方法

    python生成密碼字典的方法

    今天小編就為大家分享一篇python生成密碼字典的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python paramiko模塊使用解析(實(shí)現(xiàn)ssh)

    Python paramiko模塊使用解析(實(shí)現(xiàn)ssh)

    這篇文章主要介紹了Python paramiko模塊使用解析(實(shí)現(xiàn)ssh),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Jupyter加載文件的實(shí)現(xiàn)方法

    Jupyter加載文件的實(shí)現(xiàn)方法

    這篇文章主要介紹了Jupyter加載文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python實(shí)現(xiàn)會(huì)員信息管理系統(tǒng)(List)

    python實(shí)現(xiàn)會(huì)員信息管理系統(tǒng)(List)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)會(huì)員信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論

西吉县| 巴东县| 佛学| 丹东市| 临沭县| 庆云县| 洪江市| 和静县| 彰化县| 墨竹工卡县| 梨树县| 高淳县| 铁力市| 隆昌县| 汕头市| 海安县| 道真| 阳东县| 天门市| 嫩江县| 容城县| 衡水市| 琼中| 深圳市| 澄江县| 商都县| 弥渡县| 红安县| 拉孜县| 厦门市| 江孜县| 昌吉市| 土默特右旗| 土默特左旗| 渭源县| 如东县| 文登市| 江油市| 南宁市| 惠东县| 博客|