脚本专栏 
首页 > 脚本专栏 > 浏览文章

tensorflow多维张量计算实例

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

两个三维矩阵的乘法怎样计算呢"htmlcode">

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多维张量计算实例

以上这篇tensorflow多维张量计算实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:Python连接Oracle之环境配置、实例代码及报错解决方法详解
下一篇:利用python中集合的唯一性实现去重
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网