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

相关推荐
serve the people4 小时前
TensorFlow 基础训练循环(简化版 + 补全代码)
人工智能·python·tensorflow
海市公约8 小时前
Python操作SQLite数据库:从基础语法到完整项目实战
数据库·ide·python·程序人生·架构·pycharm·sqlite
serve the people10 小时前
TensorFlow 中雅可比矩阵计算方式
人工智能·矩阵·tensorflow
艾莉丝努力练剑12 小时前
【Python基础:语法第三课】Python 函数详解:定义、参数、返回值与作用域
服务器·人工智能·windows·python·pycharm
团圆吧12 小时前
md2pdf.py:高效 Markdown 转 PDF 全能工具
python·pdf·tensorflow
Q_Q5110082851 天前
python+django/flask+vue农业电商服务系统
spring boot·python·pycharm·django·flask
thinkpad12345678901 天前
ubuntu22.04+miniconda+ros2的坑(1)
pycharm·anaconda·ros2
LCG米1 天前
从训练到部署:基于PyTorch与TensorFlow Lite的端侧AI花卉分类系统完整指南
人工智能·pytorch·tensorflow
唐古乌梁海1 天前
【PyCharm】PyCharm 常用技巧与快捷键总结
ide·python·pycharm
serve the people1 天前
tensorflow 衔接 tf.Module 讲解 Keras 的核心设计
人工智能·tensorflow·keras