Data type mismatch in streaming F1 score calculation in Tensorflow
Asked Answered
P

1

0

I was trying to use this code as it is on Tensorflow 1.13.1. However, it throws the following error:

sherlock@mybox:~/cs273/autocat/bert$ python streaming2.py 
Traceback (most recent call last):
  File "streaming2.py", line 233, in <module>
    tf_f1 = tf_f1_score(t, p)
  File "streaming2.py", line 161, in tf_f1_score
    f1s[2] = tf.reduce_sum(f1 * weights)
  File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 812, in binary_op_wrapper
    return func(x, y, name=name)
  File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1078, in _mul_dispatch
    return gen_math_ops.mul(x, y, name=name)
  File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 5860, in mul
    "Mul", x=x, y=y, name=name)
  File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 547, in _apply_op_helper
    inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Mul' Op has type float64 that does not match type int64 of argument 'x'.

Tried fixing the casts for some time, but failed to find a minimal change that makes the code work. Can anyone please help me on this?

Pennington answered 3/6, 2019 at 10:2 Comment(0)
R
0

I could reproduce your error: it happens with Python 2 but not 3.

So either switch to Python 3 or change the code with tf.cast

f1 = tf.cast(f1, tf.float64)
f1s[2] = tf.reduce_sum(f1 * weights)

and maybe in other locations but that's the idea

Raised answered 3/6, 2019 at 10:27 Comment(2)
I am afraid only casting f1 might not be correct. If I remember correctly, f1 is being computed as an int tensor, so there might be a possibility of a truncation error even before casting.Pennington
I wonder why Tensorflow doesn't provide a standard API for such a common task!Pennington

© 2022 - 2024 — McMap. All rights reserved.