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

相关推荐
原来是小珠呀4 小时前
Maven介绍安装与IDEA使用(JavaWeb)
pycharm·maven·intellij-idea
howard20055 小时前
在IPython和PyCharm里通过PySpark实现词频统计
pycharm·pyspark·ipython·词频统计
残雪飞扬5 小时前
Pycharm社区版安装
ide·python·pycharm
盼小辉丶10 小时前
TensorFlow深度学习实战——胶囊网络
深度学习·tensorflow·keras
傻啦嘿哟17 小时前
Python高效实现Word转HTML:从基础到进阶的全流程方案
人工智能·python·tensorflow
盼小辉丶2 天前
使用互信息进行无监督学习
深度学习·keras·无监督学习
Jay_Franklin2 天前
Python中使用sqlite3模块和panel完成SQLite数据库中PDF的写入和读取
数据库·笔记·python·pycharm·sqlite·pdf·py
合作小小程序员小小店2 天前
web网页开发,在线%聚类,微博,舆情%系统,基于python,pycharm,django,nlp,kmeans,mysql
python·pycharm·kmeans·聚类·sklearn·kmean
七号练习生.c2 天前
CSS入门
前端·css·tensorflow
重生之我要当编程大佬3 天前
关于打不开pycharm的解决方法(一)
ide·python·pycharm