[英]Pytorch: How can I find indices of first nonzero element in each row of a 2D tensor?
[英]How do I subtract the max element of each row of a 2D tensor from all elements of that row
最初,我用全局最大值问这个问题,但是在放入尺寸时,仅减去tf.reduce_max()
的解决方案不起作用。 我想要类似mytensor - tf.reduce_max(mytensor, 1)
东西mytensor - tf.reduce_max(mytensor, 1)
但这会产生尺寸错误。
我不能将tf.constant(value = tf.reduce_max(mytensor,1) , shape = mytensor.get_shape()[1])
与指定值一起使用,因为reduce_max()
的输出是张量而不是常数。
对于全局最大值,您可以执行以下操作:
import tensorflow as tf
inp = tf.constant([[1, 2, 3],[4,5,6]
])
res=tf.reduce_max(inp)
res1=inp-res
sess = tf.Session()
print(sess.run(res))
print(sess.run(res1))
然后res是6而res1是
[[-5 -4 -3]
[-2 -1 0]]
如果要减去每一行中的最大元素,则可以完成以下工作:
import tensorflow as tf
inp = tf.constant([[1, 2, 3],[6,6,6]
])
res=tf.reduce_max(inp,1)
res1=inp-tf.reshape(res,[-1,1])
sess = tf.Session()
print(sess.run(res1))
然后res1
是
[[-2 -1 0]
[ 0 0 0]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.