gpt4 book ai didi

python - 如何让 Keras 在 Anaconda 中使用 Tensorflow 后端?

转载 作者:IT老高 更新时间:2023-10-28 21:06:25 28 4
gpt4 key购买 nike

我已经在我的 Anaconda 环境中安装了 tensorflow-gpu。他们都运作良好。

现在我正在尝试使用 Tensorflow 后端安装 Keras。根据instruction我只是跑:

pip install keras

但是它没有安装keras,然后我试了一下:

conda install -c conda-forge keras=2.0.2

然后我现在可以在 python 中导入 keras。但问题是,它总是使用 Theano 后端。我正在尝试改变这一点,但不知道该怎么做。

我也尝试编辑文件 ~/.keras,但实际上默认后端已经是 tensorflow。

请帮忙.. 非常感谢!

最佳答案

发生这种情况是因为 keras conda-forge 包在 ${CONDA_PREFIX}/etc/conda/activate.d/keras_activate.sh 中放置了一个文件,该文件设置了环境变量 KERAS_BACKEND

(root) [root@starlabs ~]# cat $CONDA_PREFIX/etc/conda/activate.d/keras_activate.sh
#!/bin/bash
if [ "$(uname)" == "Darwin" ]
then
# for Mac OSX
export KERAS_BACKEND=tensorflow
elif [ "$(uname)" == "Linux" ]
then
# for Linux
export KERAS_BACKEND=theano
fi

从文件中可以看出,在 Linux 中,它将值设置为 'theano' 并根据官方文档:

the environment variable KERAS_BACKEND will override what is defined in your config file

要解决此问题,您可以编辑此文件并将“theano”更改为“tensorflow”(在重新安装或更改环境时可能会被覆盖),或者执行以下操作:

export KERAS_BACKEND=tensorflow
python /path/to/python/program.py

关于python - 如何让 Keras 在 Anaconda 中使用 Tensorflow 后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43327464/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com