我的问题是两个相关的部分:
如何计算沿张量某个轴的最大值?例如,如果我有
x = tf.constant([[1,220,55],[4,3,-1]])
我想要类似的东西
x_max = tf.max(x, axis=1) print sess.run(x_max) output: [220,4]
我知道有 a
tf.argmax
和 atf.maximum
,但都没有给出沿单个张量轴的最大值。现在我有一个解决方法:x_max = tf.slice(x, begin=[0,0], size=[-1,1]) for a in range(1,2): x_max = tf.maximum(x_max , tf.slice(x, begin=[0,a], size=[-1,1]))
但它看起来不太理想。有一个更好的方法吗?
给定
argmax
一个张量的索引,我如何使用这些索引索引另一个张量?使用x
上面的示例,我该如何执行以下操作:ind_max = tf.argmax(x, dimension=1) #output is [1,0] y = tf.constant([[1,2,3], [6,5,4]) y_ = y[:, ind_max] #y_ should be [2,6]
我知道切片,就像最后一行一样,在 TensorFlow 中还不存在(#206)。
我的问题是:对于我的具体情况,最好的解决方法是什么(可能使用其他方法,如收集、选择等)?
附加信息:我知道
x
并且y
将只是二维张量!