Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tf.round() to a specified precision

tf.round(x) rounds the values of x to integer values.

Is there any way to round to, say, 3 decimal places instead?

like image 830
KOB Avatar asked Oct 11 '17 12:10

KOB


People also ask

What is precision round?

Precision of a numeric value describes the number of digits that are used to express that value, including digits to both the left and the right of any decimal point. For example 4.520 has a precision of 4. Zuora supports up to 13 digits to the left of the decimal place, and up to 9 digits to the right.

Is TF round differentiable?

Rounding is a fundamentally nondifferentiable function, so you're out of luck there.


Video Answer


1 Answers

You can do it easily like that, if you don't risk reaching too high numbers:

def my_tf_round(x, decimals = 0):
    multiplier = tf.constant(10**decimals, dtype=x.dtype)
    return tf.round(x * multiplier) / multiplier

Mention: The value of x * multiplier should not exceed 2^32. So using the above method, should not rounds too high numbers.

like image 121
gdelab Avatar answered Oct 04 '22 20:10

gdelab