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

导入Keras时报AlreadyExistsError错误无法正常导入的问题求助

问题根因

该报错为TensorFlow 2.6.0版本的已知兼容性问题:TensorFlow 2.6内置的tensorflow.keras模块已经提前注册了/tensorflow/api/keras/dropout/temp_rate_is_zero指标,当环境中同时安装了版本不匹配的独立Keras包时,独立Keras导入过程中会重复注册同名指标,直接触发该报错。
首次安装TensorFlow 2.6.0时可正常使用,是因为初始状态下没有额外安装独立Keras包,后续使用过程中如果手动执行过pip install keras操作,或者其他依赖包自动安装了版本不匹配的独立Keras,就会触发该问题。

稳定解决方案
  • 方案1(推荐):卸载独立Keras包,统一使用TensorFlow内置Keras
    执行卸载命令:
    pip uninstall -y keras
    后续代码中仅需要将原有的import keras导入语句修改为from tensorflow import keras,原有Keras相关的API调用无需任何调整即可正常运行。该方案可以彻底避免后续TensorFlow和Keras版本不匹配的问题。
  • 方案2:锁定独立Keras版本与TensorFlow版本匹配
    TensorFlow 2.6.0对应的兼容独立Keras版本为2.6.0,执行以下命令固定版本即可:
    pip uninstall -y keras
    pip install keras==2.6.0
    
    该方案无需修改原有代码的导入逻辑,适合需要兼容历史代码的场景。
  • 方案3:升级到更高版本的TensorFlow正式稳定版
    该版本冲突问题在TensorFlow 2.7及以上正式版本中已经被官方修复,直接执行升级命令即可:
    pip install --upgrade tensorflow>=2.7

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 07:15:03