tensorflow多维张量计算实例

作者:PROMINEM 时间:2022-02-05 00:43:48 

两个三维矩阵的乘法怎样计算呢?我通过实验发现,tensorflow把前面的维度当成是batch,对最后两维进行普通的矩阵乘法。也就是说,最后两维之前的维度,都需要相同。

首先计算shape为(2, 2, 3)乘以shape为(2, 3, 2)的张量。


import tensorflow as tf
import numpy as np
a = tf.constant(np.arange(1, 13, dtype=np.float32), shape=[2, 2, 3])
b = tf.constant(np.arange(1, 13, dtype=np.float32), shape=[2, 3, 2])
c = tf.matmul(a, b)
# c = tf.matmul(a, b)
sess = tf.Session()
print("a*b = ", sess.run(c))
c1 = tf.matmul(a[0, :, :], b[0, :, :])
print("a[1]*b[1] = ", sess.run(c1))

运行结果:

tensorflow多维张量计算实例

计算结果表明,两个三维矩阵相乘,对应位置的最后两个维度的矩阵乘法。

再验证高维的张量乘法:


import tensorflow as tf
import numpy as np
a = tf.constant(np.arange(1, 36, dtype=np.float32), shape=[3, 2, 2, 3])
b = tf.constant(np.arange(1, 36, dtype=np.float32), shape=[3, 2, 3, 2])
c = tf.matmul(a, b)
# c = tf.matmul(a, b)
sess = tf.Session()
print("a*b = ", sess.run(c))
c1 = tf.matmul(a[0, 0, :, :], b[0, 0, :, :])
print("a[1]*b[1] = ", sess.run(c1))

运行结果:

tensorflow多维张量计算实例

来源:https://blog.csdn.net/weixin_42445581/article/details/82791811

标签:tensorflow,多维,张量,计算
0
投稿

猜你喜欢

  • asp如何在ADO中客户端利用好缓存技术?

    2010-06-17 12:50:00
  • ASP 精华源码收集(五年总结)第1/20页

    2011-04-07 11:15:00
  • 像线程一样管理进程的Python multiprocessing库

    2023-06-01 15:37:39
  • ASP存储过程开发应用详解第1/2页

    2011-04-07 11:16:00
  • Python调用高德API实现批量地址转经纬度并写入表格的功能

    2023-12-26 03:22:20
  • Jupyter notebook快速入门教程(推荐)

    2021-02-13 13:53:57
  • 仿阿里巴巴搜索导航设计效果

    2008-04-15 15:01:00
  • 30个最常用css选择器解析

    2011-06-16 20:36:37
  • python能在浏览器能运行吗

    2023-05-11 14:32:07
  • PHP+redis实现添加处理投票的方法

    2023-11-22 04:38:19
  • Python中 Lambda表达式全面解析

    2021-07-05 04:58:01
  • SQL游标原理和使用方法

    2008-12-22 10:50:00
  • Python中Turtle库改变画笔(海龟)方向的两种方法总结

    2022-04-21 11:09:52
  • python集合的创建、添加及删除操作示例

    2022-07-09 13:29:38
  • Python实现连点器的示例代码

    2023-04-17 00:11:29
  • 打开电脑上的QQ的python代码

    2022-08-18 04:21:28
  • Linux上MySql远程备份方案

    2010-11-25 17:23:00
  • Python脚本传参数argparse模块的使用

    2023-02-28 23:07:21
  • 通过Python实现电脑定时关机的两种方法

    2023-05-09 03:23:50
  • 教你怎么用PyCharm为同一服务器配置多个python解释器

    2022-01-29 10:22:21
  • asp之家 网络编程 m.aspxhome.com