Tensorflow矩陣運(yùn)算實(shí)例(矩陣相乘,點(diǎn)乘,行/列累加)
Tensorflow二維、三維、四維矩陣運(yùn)算(矩陣相乘,點(diǎn)乘,行/列累加)
1. 矩陣相乘
根據(jù)矩陣相乘的匹配原則,左乘矩陣的列數(shù)要等于右乘矩陣的行數(shù)。
在多維(三維、四維)矩陣的相乘中,需要最后兩維滿(mǎn)足匹配原則。
可以將多維矩陣?yán)斫獬桑海ň仃嚺帕?,矩陣),即后兩維為矩陣,前面的維度為矩陣的排列。
比如對(duì)于(2,2,4)來(lái)說(shuō),視為2個(gè)(2,4)矩陣。
對(duì)于(2,2,2,4)來(lái)說(shuō),視為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è)和矩陣行向量等長(zhǎng)(即列數(shù))的向量。
即
因?yàn)樵邳c(diǎn)乘過(guò)程中,會(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)乘,行/列累加)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)提取音樂(lè)頻譜的方法詳解
你有沒(méi)有經(jīng)常好奇一些音樂(lè)軟件的頻譜特效是怎么做的,為什么做的這么好看?有沒(méi)有想試試自己提取音樂(lè)頻譜并可視化展現(xiàn)出來(lái)?本文就來(lái)教你如何利用Python提取音樂(lè)頻譜,快來(lái)學(xué)習(xí)一下吧2022-06-06在Python中移動(dòng)目錄結(jié)構(gòu)的方法
這篇文章主要介紹了在Python中移動(dòng)目錄結(jié)構(gòu)的方法,需要的朋友可以參考下2016-01-01python pandas.DataFrame選取、修改數(shù)據(jù)最好用.loc,.iloc,.ix實(shí)現(xiàn)
今天小編就為大家分享一篇python pandas.DataFrame選取、修改數(shù)據(jù)最好用.loc,.iloc,.ix實(shí)現(xiàn)。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python調(diào)用java的Webservice示例
這篇文章主要介紹了python調(diào)用java的Webservice具體方法,包含java端和python實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-03-03Python+OpenCV 圖像邊緣檢測(cè)四種實(shí)現(xiàn)方法
本文主要介紹了通過(guò)OpenCV中Sobel算子、Schaar算子、Laplacian算子以及Canny分別實(shí)現(xiàn)圖像邊緣檢測(cè)并總結(jié)了四者的優(yōu)缺點(diǎn),感興趣的同學(xué)可以參考一下2021-11-11