TypeError: Cannot convert 1.0 to EagerTensor of dtype int64

在使用TensorFlow 2.2.0时遇到错误:TypeError: Cannot convert 0.0 to EagerTensor of dtype int32。问题根源在于调用GradientTape.gradient时源张量的dtype必须为浮点类型,但实际得到的是tf.int32。解决方案是将int32类型的变量转换为float32。参考链接提供了解决此问题的具体例子。
摘要由CSDN通过智能技术生成

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.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值