tensorflow2.2.0的错误:
WARNING:tensorflow:The dtype of the source tensor must be floating (e.g. tf.float32) when calling GradientTape.gradient, got tf.int32
2020-10-19 17:18:38.582782: W tensorflow/core/framework/op_kernel.cc:1741] Invalid argument: TypeError: Cannot convert 0.0 to EagerTensor of dtype int32
Traceback (most recent call last):
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 241, in __call__
return func(device, token, args)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 142, in __call__
for (x, dtype) in zip(ret, self._out_dtypes)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 142, in <listcomp>
for (x, dtype) in zip(ret, self._out_dtypes)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/script_ops.py", line 118, in _convert
return constant_op.constant(0.0, dtype=dtype)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 262, in constant
allow_broadcast=True)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 270, in _constant_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/home/hlx/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
TypeError: Cannot convert 0.0 to EagerTensor of dtype int32
原因:
The dtype of the source tensor must be floating (e.g. tf.float32) when calling GradientTape.gradient, got tf.