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

相关推荐
过往入尘土14 小时前
服务端与客户端的简单链接
人工智能·python·算法·pycharm·大模型
java1234_小锋16 小时前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 神经网络基础原理
python·深度学习·tensorflow·tensorflow2
小蕾Java1 天前
PyCharm入门级详细使用手册(Python新手快速上手篇)
ide·python·pycharm
动能小子ohhh1 天前
AI智能体(Agent)大模型入门【9】--如何在pycharm等其他编译软件调用ocr工具【只写后端代码不演示】
人工智能·python·深度学习·机器学习·pycharm·ocr
java1234_小锋2 天前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 计算图和 tf.function 简介
python·深度学习·tensorflow·tensorflow2
java1234_小锋2 天前
TensorFlow2 Python深度学习 - TensorFlow2框架入门 - 立即执行模式(Eager Execution)
python·深度学习·tensorflow·tensorflow2
10岁的博客2 天前
PyCharm一键安装SciPy全攻略
ide·pycharm·scipy
盼小辉丶3 天前
TensorFlow深度学习实战(39)——机器学习实践指南
深度学习·机器学习·tensorflow
Python×CATIA工业智造3 天前
Python回调函数中携带额外状态的完整指南:从基础到高级实践
python·pycharm
害恶细君3 天前
【超详细】使用conda配置python的开发环境
开发语言·python·jupyter·pycharm·conda·ipython