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 文件和输入输出小结
Oct 09 Python
Python+django实现文件上传
Jan 17 Python
python实现字典(dict)和字符串(string)的相互转换方法
Mar 01 Python
python中reduce()函数的使用方法示例
Sep 29 Python
Python获取本机所有网卡ip,掩码和广播地址实例代码
Jan 22 Python
Pyqt实现无边框窗口拖动以及窗口大小改变
Apr 19 Python
Python求解任意闭区间的所有素数
Jun 10 Python
python 批量修改/替换数据的实例
Jul 25 Python
Python定时任务APScheduler的实例实例详解
Jul 22 Python
Python.append()与Python.expand()用法详解
Dec 18 Python
python小程序基于Jupyter实现天气查询的方法
Mar 27 Python
pyautogui自动化控制鼠标和键盘操作的步骤
Apr 01 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中改变图片的尺寸大小的代码
2011/07/17 PHP
获取URL文件名后缀
2013/10/24 PHP
php实现上传图片生成缩略图示例
2014/04/13 PHP
php判断对象是派生自哪个类的方法
2015/06/20 PHP
javascript写的一个链表实现代码
2009/10/25 Javascript
利用JavaScript脚本实现滚屏效果的方法
2015/07/07 Javascript
Javascript实现的简单右键菜单类
2015/09/23 Javascript
jQuery弹出层后禁用底部滚动条(移动端关闭回到原位置)
2016/08/29 Javascript
轻松实现jquery选项卡切换效果
2016/10/10 Javascript
提高JavaScript执行效率的23个实用技巧
2017/03/01 Javascript
react实现一个优雅的图片占位模块组件详解
2017/10/30 Javascript
浅谈JavaScript 代码简洁之道
2019/01/09 Javascript
jQuery实现的导航条点击后高亮显示功能示例
2019/03/04 jQuery
微信小程序后台持续定位功能使用详解
2019/08/23 Javascript
webpack5 联邦模块介绍详解
2020/07/08 Javascript
python中readline判断文件读取结束的方法
2014/11/08 Python
Python中使用haystack实现django全文检索搜索引擎功能
2017/08/26 Python
Python 最大概率法进行汉语切分的方法
2018/12/14 Python
Python实现DDos攻击实例详解
2019/02/02 Python
基于python实现百度翻译功能
2019/05/09 Python
详解将Python程序(.py)转换为Windows可执行文件(.exe)
2019/07/19 Python
python爬虫 基于requests模块的get请求实现详解
2019/08/20 Python
softmax及python实现过程解析
2019/09/30 Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
2020/03/06 Python
简单了解python列表和元组的区别
2020/05/14 Python
python中四舍五入的正确打开方式
2021/01/18 Python
6种非常炫酷的CSS3按钮边框动画特效
2016/03/16 HTML / CSS
英国No.1文具和办公用品在线:Euroffice
2016/09/21 全球购物
自荐信如何“自荐”
2013/10/24 职场文书
法制宣传日活动总结
2014/04/29 职场文书
预备党员综合考察材料
2014/05/31 职场文书
新闻传播专业求职信
2014/07/22 职场文书
校本培训个人总结
2015/02/28 职场文书
教师继续教育反思周记
2015/06/25 职场文书
祝寿主持词
2015/07/02 职场文书
MySQL 覆盖索引的优点
2021/05/19 MySQL