Tensorflow 2.16.0+在PyCharm中找不到keras的报错解决

在PyCharm(2024.2版本)中,直接使用from tensorflow import keras会提示"Cannot find reference 'keras' in 'init .py' ",找不到keras,如下图所示。

查阅相关资料,可以发现在tf2.16之后,默认的keras后端升级为了3.0,且直接引用即可。

参考链接:PyCharm cannot parse any content under tensorflow.keras 2.16.2 #73110

以及:Introducing Keras 3.0

TF 2.15: Keras2.0

python 复制代码
import tensorflow as tf
from tensorflow import keras

TF 2.16: Keras 3.0

python 复制代码
import tensorflow as tf
import keras

在reddit上也有人讨论:

Because in the old days Keras was entirely separate to Tensorflow. It had a backend for Theano and maybe some other stuff. Then Francois and the TF team at Google decided to bring Keras into TF as their main high level API, trying to address the fragmentation that had been created in TF land. Since TF usage is dwindling in research, and possibly showing signs of similar in industry, Keras is now multi-backend again, supporting TensorFlow, PyTorch, and JAX.

  1. Very old code will import keras directly, and be referring to Keras 1. This code will usually use > Theano or TensorFlow 1.x as well.
  2. More recent code will import tensorflow.keras and be referring to Keras 2. This is the majority of the code you'll find.
  3. Very recent code (i.e. this year I think) will import keras directly again, or maybe keras_core, and be referring to Keras 3, the latest version.

也就是说,在最新版(2.16.0+)的Tensorflow中,只需要直接引用import keras即可正常使用keras3.0。经测试,使用keras.models或keras.layers均可正常使用,也会有代码补全。

相关推荐
智码看视界1 天前
深度学习:环境搭建PyTorch vs TensorFlow 选型
pytorch·深度学习·tensorflow·gpu·环境搭建·cuda·框架选型
GPU实战笔记2 天前
PyCharm 接云 GPU:远端项目连接与远程解释器别混为一谈
python·pycharm·ssh·远程开发·云gpu
GPU实战笔记2 天前
TensorBoard 空面板排查:日志写入、--logdir 与访问入口分层
深度学习·tensorflow·模型训练·tensorboard
运维@小兵4 天前
安装Pycharm并破解
ide·python·pycharm
承渊政道4 天前
【Python编程—从入门到实践】(让程序记得、恢复并解释失败:Python 文件、异常与JSON系统教程)
开发语言·python·pycharm·json·异常处理
西柚不烦恼5 天前
PyCharm 虚拟环境与 Jenkins 自动化部署实践
pycharm·自动化·jenkins
陈年老古董5 天前
PyQt5 入门指南:环境搭建、PyCharm 外部工具配置与登录界面实战
开发语言·qt·pycharm
源图客5 天前
Python环境安装与PyCharm的第一个项目
开发语言·python·pycharm
zx_741484815 天前
PyQt5 环境搭建保姆级教程:PyCharm + QtDesigner + PyUIC 完整配置
开发语言·qt·pycharm·pyqt
夜雪一千7 天前
如何取消PyCharm自动识别test开头文件并进入测试模式
ide·python·pycharm