配置 Keras 以使用 Tensorflow 而不是 Theano

Configuring Keras to use Tensorflow instead of Theano

我正在尝试在 Anaconda virtualenv 下配置 Keras 安装,所有这些 运行 在 Ubuntu 17.04 下。我通过 conda 安装了 keras-gpu,并通过 运行 python -c 'import keras' 生成了 bootstrap ~/.keras 目录;最后,我更新了该目录中的 keras.json 以将 tensorflow 作为后端而不是 theano.

我还在 conda.

上使用常规的非 GPU keras 尝试了这些步骤

我遇到的问题是我的 keras.json 中的 backend 选项正在被读取(因为无效值引发异常),但被获取 export 由 Anaconda 本身编辑——根据 grep,有几个实例:

export KERAS_BACKEND=tensorflow
export KERAS_BACKEND=theano

...分散在 ~/miniconda3/pkgs/keras-2.0.2-py36_1/.

中的许多文件中

我对手动编辑这些文件犹豫不决,因为它们是由包管理器自动放置的,但我也想避免在每个会话开始时明确指定 KERAS_BACKEND=tensorflow,我会喜欢避免涉及 direnv.

等工具的解决方案

如何让 condakeras 默认使用 tensorflow

问题可能出在 keras package conda-forge 的文件 activate.sh 中。此文件中的 export 语句是不必要的,应删除 IMO。没有理由限制 linux 用户使用 theano 作为 Keras 后端(或 Mac OSX 使用 TensorFlow)。

#!/bin/bash
if [ "$(uname)" == "Darwin" ]
then
    # for Mac OSX
    export KERAS_BACKEND=tensorflow
elif [ "$(uname)" == "Linux" ]
then
    # for Linux
    export KERAS_BACKEND=theano
fi

您可以通过以下方式解决问题:

  1. 正在从 activate.sh 中删除这些环境设置。
  2. 删除当前安装的 keraskeras-gpu,然后 使用 conda install -c defaults keras 安装 keras: 非 conda-forge 版本的 keras 似乎没问题。我在我的机器上没有找到任何这些环境设置。
  3. pip install keras: 删除当前安装的 keraskeras-gpu,然后只安装 python 包。