Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)


Posted in Python onFebruary 05, 2020

Tensorflow二维、三维、四维矩阵运算(矩阵相乘,点乘,行/列累加)

1. 矩阵相乘 Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

根据矩阵相乘的匹配原则,左乘矩阵的列数要等于右乘矩阵的行数。

在多维(三维、四维)矩阵的相乘中,需要最后两维满足匹配原则。

可以将多维矩阵理解成:(矩阵排列,矩阵),即后两维为矩阵,前面的维度为矩阵的排列。

比如对于(2,2,4)来说,视为2个(2,4)矩阵。

对于(2,2,2,4)来说,视为2*2个(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()))

Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

2. 点乘 Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

点乘指的是shape相同的两个矩阵,对应位置元素相乘,得到一个新的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()))

Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

另外,点乘的其中一方可以是一个常数,也可以是一个和矩阵行向量等长(即列数)的向量。

Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

因为在点乘过程中,会自动将常数或者向量进行扩维。

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()))

Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)

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矩阵运算实例(矩阵相乘,点乘,行/列累加)

以上这篇Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python Trie树实现字典排序
Mar 28 Python
python之wxPython应用实例
Sep 28 Python
Python全局变量用法实例分析
Jul 19 Python
详解python基础之while循环及if判断
Aug 24 Python
python多行字符串拼接使用小括号的方法
Mar 19 Python
python 3.7.0 下pillow安装方法
Aug 27 Python
Python Numpy中数据的常用保存与读取方法
Apr 01 Python
Python在字符串中处理html和xml的方法
Jul 31 Python
Django项目创建及管理实现流程详解
Oct 13 Python
用python发送微信消息
Dec 21 Python
详解python 条件语句和while循环的实例代码
Dec 28 Python
Python使用openpyxl模块处理Excel文件
Jun 05 Python
Tensorflow累加的实现案例
Feb 05 #Python
详谈tensorflow gfile文件的用法
Feb 05 #Python
TensorFlow实现从txt文件读取数据
Feb 05 #Python
TensorFlow 读取CSV数据的实例
Feb 05 #Python
Python tkinter和exe打包的方法
Feb 05 #Python
tensorflow对图像进行拼接的例子
Feb 05 #Python
Python抓新型冠状病毒肺炎疫情数据并绘制全国疫情分布的代码实例
Feb 05 #Python
You might like
解析php利用正则表达式解决采集内容排版的问题
2013/06/20 PHP
php Imagick获取图片RGB颜色值
2014/07/28 PHP
跟我学Laravel之路由
2014/10/15 PHP
php中adodbzip类实例
2014/12/08 PHP
浅谈PHP中类和对象的相关函数
2017/04/26 PHP
打印json对象的内容及JSON.stringify函数应用
2013/03/29 Javascript
超级好用的jQuery圆角插件 Corner速成
2014/08/31 Javascript
JavaScript中函数表达式和函数声明及函数声明与函数表达式的不同
2015/11/15 Javascript
jquery表单提交带错误信息提示效果
2017/03/09 Javascript
Angular使用$http.jsonp发送跨站请求的方法
2017/03/16 Javascript
vue项目中实现图片预览的公用组件功能
2018/10/26 Javascript
Vue安装浏览器开发工具的步骤详解
2019/05/12 Javascript
JavaScript实现单英文金山打字通
2020/07/24 Javascript
解决Layui 表格自适应高度的问题
2019/11/15 Javascript
VUE 直接通过JS 修改html对象的值导致没有更新到数据中解决方法分析
2019/12/02 Javascript
python中的内置函数getattr()介绍及示例
2014/07/20 Python
python一键升级所有pip package的方法
2017/01/16 Python
python自动化脚本安装指定版本python环境详解
2017/09/14 Python
python3+PyQt5使用数据库表视图
2018/04/24 Python
Python 生成 -1~1 之间的随机数矩阵方法
2018/08/04 Python
wxPython色环电阻计算器
2019/11/18 Python
Python中url标签使用知识点总结
2020/01/16 Python
python框架Django实战商城项目之工程搭建过程图文详解
2020/03/09 Python
python实现五子棋程序
2020/04/24 Python
django的autoreload机制实现
2020/06/03 Python
浅析几个CSS3常用功能的写法
2014/06/05 HTML / CSS
Mytheresa中国官网:德国时尚奢侈品商城
2017/08/04 全球购物
Under Armour安德玛德国官网:美国高端运动科技品牌
2019/03/09 全球购物
请解释接口的显式实现有什么意义
2012/05/26 面试题
渗透攻击的测试步骤
2014/06/07 面试题
公司同意接收函
2014/01/13 职场文书
优秀党员学习焦裕禄精神思想汇报范文
2014/09/10 职场文书
寒山寺导游词
2015/02/03 职场文书
详解Python类和对象内容
2021/06/22 Python
Python之Matplotlib绘制热力图和面积图
2022/04/13 Python
MySQL安装失败的原因及解决步骤
2022/06/14 MySQL