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使用BeautifulSoup分页网页中超链接的方法
Apr 04 Python
Python装饰器实现几类验证功能做法实例
May 18 Python
Python抓取聚划算商品分析页面获取商品信息并以XML格式保存到本地
Feb 23 Python
python操作excel的方法
Aug 16 Python
Python基于百度云文字识别API
Dec 13 Python
python 中如何获取列表的索引
Jul 02 Python
python GUI图形化编程wxpython的使用
Jul 19 Python
Python一键查找iOS项目中未使用的图片、音频、视频资源
Aug 12 Python
Python logging设置和logger解析
Aug 28 Python
python 哈希表实现简单python字典代码实例
Sep 27 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
Apr 08 Python
Python xlrd/xlwt 创建excel文件及常用操作
Sep 24 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 Xdebug 调试扩展的安装与使用.
2010/03/13 PHP
将时间以距今多久的形式表示,PHP,js双版本
2012/09/25 PHP
php并发对MYSQL造成压力的解决方法
2013/02/21 PHP
PHP eval函数使用介绍
2013/12/08 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
在thinkphp5.0路径中实现去除index.php的方式
2019/10/16 PHP
PHP使用PhpSpreadsheet操作Excel实例详解
2020/03/26 PHP
CheckBoxList多选样式jquery、C#获取选择项
2013/09/06 Javascript
js与jquery获取父级元素,子级元素,兄弟元素的实现方法
2014/01/09 Javascript
node.js调用C++开发的模块实例
2015/07/03 Javascript
js实现上一页下一页的效果【附代码】
2016/03/10 Javascript
Vue表单验证插件Vue Validator使用方法详解
2017/04/07 Javascript
web3.js增加eth.getRawTransactionByHash(txhash)方法步骤
2018/03/15 Javascript
vue实现文字横向无缝走马灯组件效果的实例代码
2019/04/09 Javascript
通过vue手动封装on、emit、off的代码详解
2019/05/29 Javascript
微信小程序使用自定义组件导航实现当前页面高亮
2020/01/02 Javascript
ElementUI中el-tree节点的操作的实现
2020/02/27 Javascript
实例讲解Python中函数的调用与定义
2016/03/14 Python
python 写入csv乱码问题解决方法
2016/10/23 Python
使用python生成目录树
2018/03/29 Python
python中使用iterrows()对dataframe进行遍历的实例
2018/06/09 Python
python使用BeautifulSoup与正则表达式爬取时光网不同地区top100电影并对比
2019/04/15 Python
python创建n行m列数组示例
2019/12/02 Python
python实现单张图像拼接与批量图片拼接
2020/03/23 Python
python logging模块的使用详解
2020/10/23 Python
浅谈HTML5新增和废弃的标签
2019/04/28 HTML / CSS
html svg生成环形进度条的实现方法
2019/09/23 HTML / CSS
高中生学习生活的自我评价
2013/10/09 职场文书
优秀大学生事迹材料
2014/12/24 职场文书
升学宴学生答谢词
2015/01/05 职场文书
婚礼上证婚人致辞
2015/07/28 职场文书
干部理论学习心得体会
2016/01/21 职场文书
2019新员工试用期转正工作总结范文
2019/08/21 职场文书
python四种出行路线规划的实现
2021/06/23 Python
灵能百分百第三季什么时候来?
2022/03/15 日漫
Redis高并发缓存架构性能优化
2022/05/15 Redis