You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入tensorflow_hub遇循环导入错误,已尝试常见方案仍未解决

解决ImportError: cannot import name 'dnn_logit_fn_builder'的循环导入问题

问题根源

你直接导入tensorflow_estimator内部私有模块的写法,会触发循环导入——tensorflow_hub初始化时也会调用tensorflow_estimator相关模块,导致模块未完全初始化就互相引用。

解决方案

  • 调整导入顺序
    先完成tensorflow_hub的导入,再加载目标函数,让依赖模块先完成初始化:

    import tensorflow as tf
    import tensorflow_hub as hub
    from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
    
  • 使用公共API导入(推荐)
    避免直接引用tensorflow_estimator的内部私有路径,改用TensorFlow的公共API入口导入,这种写法更稳定,也能规避内部循环依赖:

    import tensorflow as tf
    import tensorflow_hub as hub
    from tensorflow.estimator.canned.dnn import dnn_logit_fn_builder
    
  • 重装匹配版本的依赖
    如果上述方法无效,彻底卸载现有组件后安装经过验证的稳定版本组合:

    pip uninstall -y tensorflow tensorflow-estimator tensorflow-hub
    pip install tensorflow==2.15.0 tensorflow-estimator==2.15.0 tensorflow-hub==0.16.1
    

内容的提问来源于stack exchange,提问作者peeyush

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 08:45:39