两个三维矩阵的乘法怎样计算呢"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))
运行结果:
计算结果表明,两个三维矩阵相乘,对应位置的最后两个维度的矩阵乘法。
再验证高维的张量乘法:
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多维张量计算实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。