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均可正常使用,也会有代码补全。

相关推荐
Thanks_ks5 小时前
【第 006 讲】搭建 PyCharm 开发环境:PyCharm 的下载与安装 | 初始设置与常用配置
ide·python·pycharm·开发工具·jetbrains·开发环境·集成开发环境
汤米粥5 天前
cmd手动创建的Django项目怎么用社区版Pycharm运行起来
python·pycharm·django
fanruitian8 天前
pycharm 使用anaconda
ide·python·pycharm
guocy-ww9 天前
JetBrains 全家桶(IDEA、PyCharm等)合法免费方案 审核中
pycharm·intellij-idea
人工智能导论实践课12 天前
【GUI桌面开发3】PyCharm配置外部工具
ide·python·pycharm
Dev _12 天前
PyCharm旧进程仍在后台运行,导致新实例无法启动或连接
ide·python·pycharm
qq_3660862216 天前
idea项目重新启动时报错找不到jar
pycharm·intellij-idea·jar
1-1=016 天前
Python PyCharm
开发语言·python·pycharm
Time_Memory_cici17 天前
在Windows系统安装Pycharm以及PyTorch方法
pytorch·windows·pycharm
心运软件18 天前
Fashion-MNIST 图像分类实战:TensorFlow 五模型对比与 Flask 可视化
分类·flask·tensorflow