栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

tensorflow多维张量计算实例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

tensorflow多维张量计算实例

两个三维矩阵的乘法怎样计算呢?我通过实验发现,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))

运行结果:

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

再验证高维的张量乘法:

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多维张量计算实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/21553.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号