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