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列表操作使用示例分享
Feb 21 Python
python判断图片宽度和高度后删除图片的方法
May 22 Python
关于Pycharm无法debug问题的总结
Jan 19 Python
Linux下远程连接Jupyter+pyspark部署教程
Jun 21 Python
python二进制文件的转译详解
Jul 03 Python
在Python中通过threshold创建mask方式
Feb 19 Python
python模块如何查看
Jun 16 Python
关于python scrapy中添加cookie踩坑记录
Nov 17 Python
matplotlib之多边形选区(PolygonSelector)的使用
Feb 24 Python
Python echarts实现数据可视化实例详解
Mar 03 Python
Python如何快速找到多个字典中的公共键(key)
Apr 29 Python
Python可视化神器pyecharts之绘制地理图表练习
Jul 07 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数组函数序列之array_slice() - 在数组中根据条件取出一段值,并返回
2011/11/07 PHP
php中如何使对象可以像数组一样进行foreach循环
2013/08/09 PHP
CI框架在CLI下执行占用内存过大问题的解决方法
2014/06/17 PHP
jQuery一步一步实现跨浏览器的可编辑表格,支持IE、Firefox、Safari、Chrome、Opera
2009/08/28 Javascript
JS拖动技术 关于setCapture使用
2010/12/09 Javascript
Javascript中的高阶函数介绍
2015/03/15 Javascript
JavaScript中textRange对象使用方法小结
2015/03/24 Javascript
javascript制作照片墙及制作过程中出现的问题
2016/04/04 Javascript
Javascript基础之数组的使用
2016/05/13 Javascript
JavaScript计算器网页版实现代码分享
2016/07/15 Javascript
开源免费天气预报接口API及全国所有地区代码(国家气象局提供)
2016/12/26 Javascript
jQuery实现遍历复选框的方法示例
2017/03/06 Javascript
three.js实现3D视野缩放效果
2017/11/16 Javascript
微信小程序实现购物车代码实例详解
2019/08/29 Javascript
vant 时间选择器--开始时间和结束时间实例
2020/11/04 Javascript
微信小程序实现点击导航条切换页面
2020/11/19 Javascript
Python类的专用方法实例分析
2015/01/09 Python
Python 性能优化技巧总结
2016/11/01 Python
用Eclipse写python程序
2018/02/10 Python
解决Python网页爬虫之中文乱码问题
2018/05/11 Python
Linux下python制作名片示例
2018/07/20 Python
Python使用GitPython操作Git版本库的方法
2020/02/29 Python
利用Opencv实现图片的油画特效实例
2021/02/28 Python
美国定制钻石订婚戒指:Ritani
2017/12/08 全球购物
Kipling意大利官网:世界著名的时尚休闲包袋品牌
2019/06/05 全球购物
安全教育心得体会
2013/12/29 职场文书
自荐信格式简述
2014/01/25 职场文书
财务人员求职自荐书范文
2014/02/10 职场文书
旅游节目策划方案
2014/05/26 职场文书
电气工程及其自动化专业求职信
2014/06/23 职场文书
导游词400字
2015/02/13 职场文书
《认识钟表》教学反思
2016/02/16 职场文书
人民调解协议书
2016/03/21 职场文书
2016年法制宣传月活动总结
2016/04/01 职场文书
python实现高效的遗传算法
2021/04/07 Python
JavaScript中时间格式化新思路toLocaleString()
2021/11/07 Javascript