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

相关推荐
z千鑫9 小时前
【人工智能】PyTorch、TensorFlow 和 Keras 全面解析与对比:深度学习框架的终极指南
人工智能·pytorch·深度学习·aigc·tensorflow·keras·codemoss
醉酒柴柴13 小时前
【代码pycharm】动手学深度学习v2-07 自动求导
ide·深度学习·pycharm
搏博14 小时前
Python3.9.13与深度学习框架TensorFlow的完整详细安装教程
python·tensorflow
lisacumt18 小时前
【spark】pycharm 内使用pyspark连接有kerberos集群执行sql
python·pycharm·spark
喝哈喝哈1 天前
pycharm中配置pyqt5
python·qt·pycharm
虞书欣的62 天前
Python小游戏28——水果忍者
开发语言·人工智能·游戏·小程序·pycharm
大大大反派2 天前
深度学习三大框架对比与实战:PyTorch、TensorFlow 和 Keras 全面解析
pytorch·深度学习·tensorflow
Swift社区2 天前
使用 AI 在医疗影像分析中的应用探索
typescript·tensorflow·openai
deflag2 天前
第T8周:Tensorflow实现猫狗识别(1)
人工智能·tensorflow·neo4j
大多_C2 天前
首次实现!在Docker容器中运行macOS项目,自动化下载与Web体验
人工智能·python·深度学习·macos·docker·自动化·tensorflow