Reputation: 13
today, I'm using TensorFlow to do some basic calculation, it suddenly has an error like below.
TypeError: Input 'y' of 'Add' Op has type float32 that does not match type int32 of argument 'x'.
I have tried to use tf.cast() to verify the type of the tensor, however, it seems to be useless which is based on that the types of the four variables of 're', 're2', 're3' and 're4' are just float32 according to the output:
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)
now, I don't know how to deal with the problem. anyone can give me a hand? Any suggestion will be appreciated!!
here are some relative codes
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)
the complete error is
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'.
Upvotes: 0
Views: 1636
Reputation: 2585
Modify:
sum=tf.add(sum,overate)
To:
sum=tf.add(tf.cast(sum, overate.dtype),overate)
Upvotes: 2