(已解决)报错 ValueError: Tensor conversion requested dtype float32 for Tensor with dtype resource

记录一个花了一整天才解决的问题:
(Tensorflow 1.14, python 3.6)

自己写的深度学习网络,在build graph阶段报错:ValueError: Tensor conversion requested dtype float32 for Tensor with dtype resource

查看 tf.variable 的 dtype 发现全都是“float32", 而正常情况下(对应我的tf版本)应该是"float32_ref”。不清楚二者有何不同,但float32这个类型导致我生成的graph里按变量名读不到指定的 tensor 内容。
例如:
直接读网络里的某个tf变量v,输出为: <tf.Variable ‘conv/kernel:0’ shape=(64, 64) dtype=float32>
而使用get_tensor_by_name()从生成的graph里读同一个tf变量,输出为: Tensor(“conv/kernel:0”, shape=(), dtype=resource)
dtype莫名其妙变成了resource,shape信息也读不到。

原因:

tensorflow.python.keras.layers 与 tf.layers 里的 Layer 不同.
我的网络里用到了keras的层,比如 Dense, Embedding,导致数据类型变成了 float32 而不是 float32_ref。
(检查发现把 tensorflow.python.keras.layers.Dense 替换成 tensorflow.layers.Dense, 就可以使相关的变量类型变成 float32_ref,也可以从gra

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值