该
tf.reduce_max()运营商提供的正是这种功能。默认情况下,它计算给定张量的全局最大值,但您可以指定的列表
reduction_indices,其含义与
axisNumPy中的含义相同。要完成您的示例:
x = tf.constant([[1, 220, 55], [4, 3, -1]])x_max = tf.reduce_max(x, reduction_indices=[1])print sess.run(x_max) # ==> "array([220, 4], dtype=int32)"
如果使用计算argmax
tf.argmax(),可以获取从不同的张量的值
y通过压平
y使用
tf.reshape(),转换argmax索引到矢量索引如下,并使用
tf.gather()以提取适当的值:
ind_max = tf.argmax(x, dimension=1)y = tf.constant([[1, 2, 3], [6, 5, 4]])flat_y = tf.reshape(y, [-1]) # Reshape to a vector.# N.B. Handles 2-D case only.flat_ind_max = ind_max + tf.cast(tf.range(tf.shape(y)[0]) * tf.shape(y)[1], tf.int64)y_ = tf.gather(flat_y, flat_ind_max)print sess.run(y_) # ==> "array([2, 6], dtype=int32)"



