TypeError:'Add'Op的输入'y'的float32类型与参数'x'tf的int32类型不匹配.cast是无用的

时间:2019-09-09 09:21:12

标签: python tensorflow

今天,我正在使用TensorFlow进行一些基本计算,突然出现如下错误。

TypeError: Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.

我试图使用tf.cast()来验证张量的类型,但是,这似乎是无用的,它基于're','re2','re3这四个变量的类型根据输出,'和're4'只是float32:

re1: Tensor("mul:0", shape=(), dtype=float32)
re2: Tensor("mul_1:0", shape=(), dtype=float32)
re3: Tensor("mul_4:0", shape=(), dtype=float32)
re4: Tensor("mul_7:0", shape=(), dtype=float32)

现在,我不知道如何处理该问题。有人可以帮我吗? 任何建议将不胜感激!

这是一些相关代码

re=(fc4[i][2]-fc4[i][0])*(fc4[i][3]-fc4[i][1])
re2=(bbox[i][2] - bbox[i][0]) * (bbox[i][3] - bbox[i][1])
re3=pro_x * (fc4[i][2] - fc4[i][0]) * pro_y * (fc4[i][3] - fc4[i][1])
re4=(pro_x*(fc4[i][2]-fc4[i][0])*pro_y*(fc4[i][3]-fc4[i][1]))  print("re1:",re)  
print("re2:",re2)
print("re3:",re3)  
print("re4:",re4)
overate=re4/(re+re2-re3)

完整错误是

Traceback (most recent call last):
  File "/home/hp/anaconda3/envs/mypython37/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 527, in _apply_op_helper
    preferred_dtype=default_dtype)
  File "/home/hp/anaconda3/envs/mypython37/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1224, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/hp/anaconda3/envs/mypython37/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1018, in _TensorTensorConversionFunction
    (dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("truediv_3:0", shape=(), dtype=float32)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/snap/pycharm-community/147/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/snap/pycharm-community/147/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/hp/Downloads/GOTURN-Tensorflow-master/got-10k/train/train2dup.py", line 215, in <module>
    aot=ao(tracknet.fc4,tracknet.bbox,BATCH_SIZE)
  File "/home/hp/Downloads/GOTURN-Tensorflow-master/got-10k/train/train2dup.py", line 168, in ao
    sum=tf.add(sum,overate)
  File "/home/hp/anaconda3/envs/mypython37/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 387, in add
    "Add", x=x, y=y, name=name)
  File "/home/hp/anaconda3/envs/mypython37/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 563, in _apply_op_helper
    inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.

1 个答案:

答案 0 :(得分:1)

修改

sum=tf.add(sum,overate)

收件人:

sum=tf.add(tf.cast(sum, overate.dtype),overate)