1

I'm brand new to Tensorflow, but I'm trying to figure out why these results end in ...001, ...002, etc.

I'm following the tutorial here: https://www.tensorflow.org/get_started/get_started

Code:

"""This is a Tensorflow learning script."""
import tensorflow as tf

sess = tf.Session()

W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W*x + b

sess.run(tf.global_variables_initializer()) #This is the same as the above 2 lines

print(sess.run(linear_model, {x: [1, 2, 3, 4]}))

It looks like a simple math function where if I was using 2 as an input, it would be (0.3 * 2) + -0.3 = 0.3.

Output:

[ 0. 0.30000001 0.60000002 0.90000004]

I would expect:

[ 0. 0.3 0.6 0.9]

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71

2 Answers2

1

That's probably a floating point error, because you introduced your variables as a tf.float32 dtype. You could use tf.round (https://www.tensorflow.org/api_docs/python/tf/round) but it doesn't seem to have round-to-the-nearest decimal place capability yet. For that, check out the response in: tf.round() to a specified precision.

sobes
  • 376
  • 2
  • 7
  • I've added the function and added a line `linear_model = my_tf_round(linear_model, 1)` and it has affected the results, but they're now `[ 0. 0.30000001 0.60000002 0.89999998]`. Not quite sure what's going on...I'd think they'd make a better example to start with. – Alex Kwitny Dec 24 '17 at 20:05
  • 1
    For your particular example, you could simply replace the line `linear_model = W*x + b` with `linear_model = tf.cast(10*(W*x + b), tf.int32)/10`. I just tried running your code with that replacement, and it gave me the output `[ 0. 0.3 0.6 0.9]`. – sobes Dec 25 '17 at 04:42
1

The issue is that a floating point variable (like tf.float32) simply cannot store exactly 0.3 due to being stored in binary. It's like trying to store exactly 1/3 in decimal, it'd be 0.33... but you'd have to go out to infinity to get the exact number (which isn't possible our mortal realm!).

See the python docs for more in depth review of the subject.

Tensorflow doesn't have a way to deal with decimal numbers yet (as far as I know)! But once the numbers are returned to python you could round & then convert to a Decimal.

Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43
  • 1
    I know binary well enough to convert from decimal but I didn't even think about storing floats...great info. – Alex Kwitny Dec 25 '17 at 05:40
  • Tom Scott actually has [a fantastic video](https://www.youtube.com/watch?v=PZRI1IfStY0) on the subject, (where I learned about this). Or while googling I found a [great stackoverflow answer](https://stackoverflow.com/a/2100502/7607701) on the topic. But the tl;dr is that floats are kinda like scientific notation in binary, they are actually two binary numbers X & Y where the number is: X + 2^Y. – Aaron N. Brock Dec 25 '17 at 05:55