tf.reduce_sum:
1 #coding=utf8 2 import tensorflow as tf 3 import numpy as np 4 5 tf.compat.v1.disable_eager_execution() 6 7 x = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) # shape = 2*2*3 8 x_p = tf.compat.v1.placeholder(dtype=tf.int32, shape=[None, None, None], name='input') 9 10 #y = tf.reduce_sum(x_p, 0) # 按行 11 #y = tf.reduce_sum(x_p, 1) #按列 12 y = tf.reduce_sum(x_p, 2) #修改这里 13 14 15 with tf.compat.v1.Session() as sess: 16 y = sess.run(y, feed_dict={x_p: x}) 17 print(y)
tf.reduce_sum(x_p, 0):
[[ 8 10 12]
[14 16 18]]
y = tf.reduce_sum(x_p, 1)
[[ 5 7 9]
[17 19 21]]
tf.reduce_sum(x_p, 2)
[[ 6 15]
[24 33]]
tf.reduce_mean:
import tensorflow as tf import numpy as np x = tf.constant([[1,2],[3,4]]) with tf.Session() as sess: print(sess.run(tf.reduce_mean(x))) #所有求平均 print(sess.run(tf.reduce_mean(x, 0))) #按 行 求和 print(sess.run( tf.reduce_mean(x, 1)))# 按 列 求平均 print(sess.run(tf.reduce_max(x))) print(sess.run(tf.reduce_max(x, 0))) print(sess.run(tf.reduce_max(x, 1)))
#########输出##########
2 [2 3] [1 3] 4 [3 4] [2 4]
[tf]tf.gather_nd的用法
https://www.jianshu.com/p/6e4398d9b4c8
tf.transpose
tf.transpose的第二个参数perm=[0,1,2],0代表三维数组的高(即为二维数组的个数),1代表二维数组的行,2代表二维数组的列。
tf.transpose(x, perm=[1,0,2])代表将三位数组的高和行进行转置