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

 更新时间:2020年02月05日 16:15:02   作者:Kenn7  
今天小编就为大家分享一篇Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

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

1. 矩阵相乘

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

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

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

比如对于(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()))

2. 点乘

点乘指的是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()))

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

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

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

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矩阵运算实例(矩阵相乘,点乘,行/列累加)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python采集天天基金数据掌握最新基金动向

    Python采集天天基金数据掌握最新基金动向

    这篇文章主要介绍了Python采集天天基金数据掌握最新基金动向,本次案例实现流程为发送请求、获取数据、解析数据、多页爬取、保存数据,接下来来看看具体的操作过程吧
    2022-01-01
  • Pytorch数据类型与转换(torch.tensor,torch.FloatTensor)

    Pytorch数据类型与转换(torch.tensor,torch.FloatTensor)

    这篇文章主要介绍了Pytorch数据类型转换(torch.tensor,torch.FloatTensor),之前遇到转为tensor转化为浮点型的问题,今天整理下,我只讲几个我常用的,对Pytorch数据类型转换相关知识感兴趣的朋友一起看看吧
    2023-02-02
  • 详解Appium+Python之生成html测试报告

    详解Appium+Python之生成html测试报告

    这篇文章主要介绍了详解Appium+Python之生成html测试报告,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 在pycharm中文件取消用 pytest模式打开的操作

    在pycharm中文件取消用 pytest模式打开的操作

    这篇文章主要介绍了在pycharm中文件取消用 pytest模式打开的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Python中字典的基本知识初步介绍

    Python中字典的基本知识初步介绍

    这篇文章主要介绍了Python中字典的基本知识初步介绍,是Python入门中的基础知识,需要的朋友可以参考下
    2015-05-05
  • python使用pil生成图片验证码的方法

    python使用pil生成图片验证码的方法

    这篇文章主要介绍了python使用pil生成图片验证码的方法,涉及Python操作Image,ImageDraw,ImageFont等模块的相关技巧,需要的朋友可以参考下
    2015-05-05
  • python生成密码字典的方法

    python生成密码字典的方法

    今天小编就为大家分享一篇python生成密码字典的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python paramiko模块使用解析(实现ssh)

    Python paramiko模块使用解析(实现ssh)

    这篇文章主要介绍了Python paramiko模块使用解析(实现ssh),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Jupyter加载文件的实现方法

    Jupyter加载文件的实现方法

    这篇文章主要介绍了Jupyter加载文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • python实现会员信息管理系统(List)

    python实现会员信息管理系统(List)

    这篇文章主要为大家详细介绍了python实现会员信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03

最新评论