Python TypeError:“Add”Op的输入“y”的类型float32与参数“x”的类型int32不匹配。强制转换无效
今天,我用TensorFlow做了一些基本的计算,它突然出现了如下的错误Python TypeError:“Add”Op的输入“y”的类型float32与参数“x”的类型int32不匹配。强制转换无效,python,tensorflow,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”四个变量的类型只是浮点数32: re1: Tensor("mul:0", shape=(), dtyp
TypeError: Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.
我已经尝试使用tf.cast来验证张量的类型,但是,这似乎是无用的,因为根据输出,“re”、“re2”、“re3”和“re4”四个变量的类型只是浮点数32:
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'.
修改:
sum=tf.addsum,overate
致:
sum=tf.addtf.castsum,overate.dtype,overate
您从哪一行面对错误?&什么是fc4和bbox?请提供您的完整错误log@Kris谢谢你的阅读!!这是我的fault@Alpha谢谢好的,等一下!!