Data type mismatch in streaming F1 score calculation in Tensorflow

泪湿孤枕 提交于 2019-12-24 20:25:10

问题


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



来源:https://stackoverflow.com/questions/56425049/data-type-mismatch-in-streaming-f1-score-calculation-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!