I need a tensor to consist of numbers rounded to 3 decimal places:
For example, I need 0.9989 to become 0.999.
Here is a good answer, but it doesn't work for me. I don't know why it does not work in Tensorflow 2.0
tf.round() to a specified precision
my code:
max_per_dim_rede1=tf.cast(max_per_dim_rede1 ,tf.float32)
max_per_dim_rede2=tf.cast(max_per_dim_rede2 ,tf.float32)
max_per_dim_rede1=my_tf_round(max_per_dim_rede1)
max_per_dim_rede1=my_tf_round(max_per_dim_rede1)
def my_tf_round(x, decimals = 3):
multiplier = tf.constant(10**decimals, dtype=x.dtype)
return tf.math.round(x * multiplier) / multiplier
Before round:
max_per_dim_rede1 [0.379137 0.566415608 0.653300881 0.386984855 0.567309678 0.49462229 0.360385835 0.485670954 0.719773293 0.32398814 0.683747768 0.340885848 ... 0.427117109 0.309505373 0.265250593 0.614662766 0.750278294 0.634733856 0.55743891 0.852669597 0.525657892 0.619692624 0.463500887 0.319094926]
max_per_dim_rede2 [0.391448647 0.549464703 0.353492677 0.287505835 0.481978029 0.636479914 0.510595262 0.811914325 0.548603892 0.396977246 0.697723746 0.374327421 ... 0.464759499 0.243383855 0.181745842 0.393151551 0.560529053 0.756349742 0.509183 0.897165596 0.440935612 0.386274785 0.746069133 0.240405202]
After round:
max_per_dim_rede1 [0.379 0.566000044 0.653000057 0.387000024 0.567000031 0.495000035 0.36 0.486000031 0.72 0.324 0.684 0.341000021 ... 0.427000016 0.31 0.265000015 0.615 0.75000006 0.63500005 0.557000041 0.853000045 0.526 0.62 0.464000016 0.319]
max_per_dim_rede2 [0.391448647 0.549464703 0.353492677 0.287505835 0.481978029 0.636479914 0.510595262 0.811914325 0.548603892 0.396977246 0.697723746 0.374327421 ... 0.464759499 0.243383855 0.181745842 0.393151551 0.560529053 0.756349742 0.509183 0.897165596 0.440935612 0.386274785 0.746069133 0.240405202]
As you can see, some cases work and others do not work. Anyone know why?