[英]Restrict NN outputs' maximum to be positive in Tensorflow/Keras
I have a neural network that outputs a number of outputs num_out
.我有一个输出多个输出num_out
的神经网络。
I know that if I want all outputs to be positive, I could apply a relu
activation function (or others) on the output layer.我知道如果我希望所有输出都是正的,我可以在输出层上应用relu
激活函数(或其他)。
However, my goal is to ensure that only the maximum value among all num_out
outputs is positive.但是,我的目标是确保所有num_out
输出中只有最大值为正。 I cannot find the way to enforce that.我找不到强制执行的方法。
One way to solve your problem is to use tf.where
and tf.reduce_max
:解决问题的一种方法是使用tf.where
和tf.reduce_max
:
import tensorflow as tf
x = tf.constant([
[-1, -2, -3],
[-4, -5, -6]
])
max_val = tf.reduce_max(x, keepdims=True)
new_max_val = tf.where(tf.greater(max_val, 0), max_val, tf.math.negative(max_val))
result = tf.where(tf.equal(x, max_val), tf.ones(tf.shape(x), dtype=x.dtype) * new_max_val, x)
print('Max value: ', max_val)
print('New Max value: ', new_max_val)
print('Result: ', result)
Max value: tf.Tensor([[-1]], shape=(1, 1), dtype=int32)
New Max value: tf.Tensor([[1]], shape=(1, 1), dtype=int32)
Result: tf.Tensor(
[[ 1 -2 -3]
[-4 -5 -6]], shape=(2, 3), dtype=int32)
I first find the max value in tensor x
and then convert it into a positive value if it is negative, otherwise it stays the same.我首先在张量x
找到最大值,然后如果它是负数,则将其转换为正值,否则保持不变。 Afterwards, I update the tensor x
with the new value.之后,我用新值更新张量x
。 If the highest value in your tensor is positive, then nothing changes:如果张量中的最高值为正,则没有任何变化:
x = tf.constant([
[-1, -2, -3],
[ 4, 5, 6]
])
# -->
Max value: tf.Tensor([[6]], shape=(1, 1), dtype=int32)
New Max value: tf.Tensor([[6]], shape=(1, 1), dtype=int32)
Result: tf.Tensor(
[[-1 -2 -3]
[ 4 5 6]], shape=(2, 3), dtype=int32)
If you want to make sure that all other elements except the maximum value are negative, then change this line:如果要确保除最大值之外的所有其他元素都是负数,请更改此行:
result = tf.where(tf.equal(x, max_val), tf.ones(tf.shape(x), dtype=x.dtype) * new_max_val, tf.abs(x)*-1)
Max value: tf.Tensor([[6]], shape=(1, 1), dtype=int32)
New Max value: tf.Tensor([[6]], shape=(1, 1), dtype=int32)
Result: tf.Tensor(
[[-1 -2 -3]
[-4 -5 6]], shape=(2, 3), dtype=int32)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.