简体   繁体   中英

Tensorflow clamp values outside specific range

I have been using tensorflow to implement a Convolutional neural network, I have a requirement that the the output values be less than a given value MAX_VAL

I tried creating a matrix filled with MAX_VAL and then using tf.select and tf.greater :

filled = tf.fill(output.get_shape(),MAX_VAL)
modoutput = tf.select(tf.greater(output, filled), filled, output)

But this doesn't work because the shape of output is not known statically: It is [?, 30] and tf.fill requires an explicit shape.

Any idea how do i implement this?

There is an alternative solution that uses tf.fill() like your initial version. Instead of using Tensor.get_shape() to get the static shape of output , use the tf.shape() operator to get the dynamic shape of output when the step runs:

output = ...
filled = tf.fill(tf.shape(output), MAX_VAL)
modoutput = tf.select(tf.greater(output, filled), filled, output)

(Note also that the tf.clip_by_value() operator might be useful for your purposes.)

I figured out a way to do it.

Instead of using tf.fill I used tf.ones_like

filled = MAX_VAL*tf.ones_like(output)
modoutput = tf.select(tf.greater(output, filled), filled, output)

Please mention if there is a faster or better way to do this is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM