0

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?

Tom
  • 18,685
  • 15
  • 71
  • 81
Vitor Bento
  • 384
  • 4
  • 17

1 Answers1

2

You can't represent what you want with floats. They can't hold an exact number like that, they have limited precision. What you're seeing is the closest you can get. You can only tweak them for display with some formatting.

Tomasz Kalisiak
  • 101
  • 2
  • 7
  • It is ok for me, if the operation is not exacly, so it is ok lost some precision in the process. I just cant do it ? I dont want just print it . I need do some operation with the tensor later. I mean i need compare the both later. – Vitor Bento Aug 08 '21 at 23:50
  • 1
    You need to understand the difference between floating-point and fixed-point arithmetic (there are articles about both on Wikipedia). The loss of precision means that a number like 0.999 might not be representable. You generally shouldn't check equality between floating point numbers. Expressions that should give the same output mathematically might differ when evaluated on floats. Set some margin of error and use that as "approximately equal" instead. – Tomasz Kalisiak Aug 09 '21 at 00:49
  • This may be helpful: https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python – Tomasz Kalisiak Aug 09 '21 at 01:00