如何检查已安装的Keras版本?

97

问题和标题所述相同。

我更喜欢不打开Python,而是使用MacOS或Ubuntu。

7个回答

162

Python库的作者将版本号放在<module>.__version__中。您可以通过在命令行上运行以下内容来打印它:

print(<module>.__version__)
python -c 'import keras; print(keras.__version__)'

如果是Windows终端,请像下面这样使用双引号将代码片段括起来

python -c "import keras; print(keras.__version__)"

10
对于 Python3 版本,请使用以下命令:python3 -c 'import keras; print(keras.__version__)' - Akash Kandpal

42

您可以写:

python
import keras
keras.__version__

19

最简单的方法是使用pip命令:

pip list | grep Keras

6

检查 Keras 版本的简单命令:

(py36) C:\WINDOWS\system32>python
Python 3.6.8 |Anaconda custom (64-bit) 

>>> import keras
Using TensorFlow backend.
>>> keras.__version__
'2.2.4'

OP说Linux或者MacOS。 - Robert

5

pip show tensorflow

C:\>pip show tensorflow
Name: tensorflow
Version: 2.4.1
Summary: TensorFlow is an open source machine learning framework for 
everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Requires: google-pasta, wheel, absl-py, flatbuffers, numpy, astunparse, opt- 
einsum, six, termcolor, typing-extensions, wrapt, grpcio, tensorboard, 
protobuf, tensorflow-estimator, gast, h5py, keras-preprocessing
Required-by:

同样的方法可以尝试一下:

pip show keras

如果已安装,则会提供详细信息。

1

对于终端,运行以下命令:(如果使用 Python3 版本,请将 "python" 替换为 "python3")

python -c 'import keras; print(keras.__version__)'

如果您想检查代码内部,请包含以下内容:

import keras
print(keras.__version__)

0
如果您正在使用独立的Keras(pip install keras),那么很简单:
import keras
print(keras.__version__)

对于Keras Core,Keras 3的预览版本(`pip install keras_core`),同样也很简单:
import keras_core as keras
print(keras.__version__)

如果你正在使用tf.keras——与TensorFlow捆绑在一起的Keras实现(pip install tensorflow)——那么Keras API的版本取决于TensorFlow的版本。你可以尝试这样做:
import tensorflow as tf
print(tf.keras.__version__)  # works for TF version 1.9 to 2.13

如果TensorFlow版本为2.14或更高,则不幸的是tf.keras.__version__未定义。
以下是TensorFlow版本和Keras API版本之间的映射关系:
tf.__version__ tf.keras.__version__
≤1.8.* 未定义
1.9.* 2.1.6-tf
1.10.* 2.1.6-tf
1.11.* 2.1.6-tf
1.12.* 2.1.6-tf
1.13.* 2.2.4-tf
1.14.* 2.3.0-tf(如果tf2.enabled(),否则2.2.4-tf)
1.15.* 2.3.0-tf(如果tf2.enabled(),否则2.2.4-tf)
2.0.* 2.3.0-tf(如果tf2.enabled(),否则2.2.4-tf)
2.1.* 2.3.0-tf(如果tf2.enabled(),否则2.2.4-tf)
2.2.* 2.3.0-tf(如果tf2.enabled(),否则2.2.4-tf)
2.3.* 2.4.0
2.4.* 2.4.0
2.5.* 2.5.0
2.6.* 2.6.0
2.7.* 2.6.0
2.8.* 2.6.0
2.9.* 2.6.0
2.10.* 2.6.0
2.11.* 2.6.0
2.12.* 2.6.0
2.13.* 2.6.0
2.14.* 未定义
2.15.* 未定义

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接