Data type mismatch in streaming F1 score calculati

2019-08-23 10:16发布

问题:

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?

回答1:

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