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 提取文件的小程序
Jul 29 Python
简单介绍Python中的round()方法
May 15 Python
在Linux系统上安装Python的Scrapy框架的教程
Jun 11 Python
基于asyncio 异步协程框架实现收集B站直播弹幕
Sep 11 Python
Python for循环生成列表的实例
Jun 15 Python
python turtle库画一个方格和圆实例
Jun 27 Python
python实现屏保程序(适用于背单词)
Jul 30 Python
PyQt5 如何让界面和逻辑分离的方法
Mar 24 Python
Django Admin 上传文件到七牛云的示例代码
Jun 20 Python
Pycharm2020最新激活码|永久激活(附最新激活码和插件的详细教程)
Sep 29 Python
python 利用panda 实现列联表(交叉表)
Feb 06 Python
pytorch锁死在dataloader(训练时卡死)
May 28 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 如何利用phpexcel导入数据库
2013/08/24 PHP
php无限遍历目录示例
2014/02/21 PHP
PHP实现数字补零功能的2个函数介绍
2014/05/12 PHP
php实现在站点里面添加邮件发送的功能
2020/04/28 PHP
浅谈php的TS和NTS的区别
2019/03/13 PHP
PHP超全局变量实现原理及代码解析
2020/09/01 PHP
PHP safe_mode开启对于PHP系统函数有什么影响
2020/11/10 PHP
处理及遍历XML文档DOM元素属性及方法整理
2013/08/23 Javascript
Javascript 多浏览器兼容总结(实战经验)
2013/10/30 Javascript
JavaScript定时显示广告代码分享
2015/03/02 Javascript
jQuery实现简单的日期输入格式化控件
2015/03/12 Javascript
PHP+MySQL+jQuery随意拖动层并即时保存拖动位置实例讲解
2015/10/09 Javascript
15位和18位身份证JS校验的简单实例
2016/07/18 Javascript
基于vue.js实现图片轮播效果
2016/12/01 Javascript
WebView启动支付宝客户端支付失败的问题小结
2017/01/11 Javascript
JS实现侧边栏鼠标经过弹出框+缓冲效果
2017/03/29 Javascript
JS对象与json字符串相互转换实现方法示例
2018/06/14 Javascript
微信小程序定位当前城市的方法
2018/07/19 Javascript
vue interceptor 使用教程实例详解
2018/09/13 Javascript
vue商城中商品“筛选器”功能的实现代码
2020/07/01 Javascript
js调用网络摄像头的方法
2020/12/05 Javascript
举例详解Python中threading模块的几个常用方法
2015/06/18 Python
Python入门_学会创建并调用函数的方法
2017/05/16 Python
python 对txt中每行内容进行批量替换的方法
2018/07/11 Python
解决pycharm 远程调试 上传 helpers 卡住的问题
2019/06/27 Python
python爬虫豆瓣网的模拟登录实现
2019/08/21 Python
python 装饰器功能与用法案例详解
2020/03/06 Python
2021年的Python 时间轴和即将推出的功能详解
2020/07/27 Python
matplotlib 画动态图以及plt.ion()和plt.ioff()的使用详解
2021/01/05 Python
基于HTML5 audio元素播放声音jQuery小插件
2011/05/11 HTML / CSS
HTML5图片预览实例分享
2014/06/04 HTML / CSS
编写用C语言实现的求n阶阶乘问题的递归算法
2014/10/21 面试题
企业安全生产目标责任书
2014/07/23 职场文书
交通安全月活动总结
2015/05/08 职场文书
学校教学工作总结2015
2015/05/19 职场文书
2019通用版新员工入职培训方案!
2019/07/11 职场文书