Configuring Keras to use Tensorflow instead of Theano

坚强是说给别人听的谎言 提交于 2019-12-13 13:30:47

问题


I'm trying to configure Keras install under an Anaconda virtualenv with all of that running under Ubuntu 17.04. I've installed keras-gpu via conda, and have generated a bootstrap ~/.keras directory by running python -c 'import keras'; finally, I've updated my keras.json within that directory to include tensorflow as the backend rather than theano.

I've also tried those steps with the regular, non-GPU keras available on conda.

The issue I'm getting is that the backend option in my keras.json is being read (since invalid values raise exceptions), but is being overruled by an environment variable that gets exported by Anaconda itself -- according to grep, there are a few instances of:

export KERAS_BACKEND=tensorflow
export KERAS_BACKEND=theano

... Scattered around a number of files in ~/miniconda3/pkgs/keras-2.0.2-py36_1/.

I'm hesitant to manually edit these files since they're automatically put there by the package manager, but I also want to avoid explicitly specifying KERAS_BACKEND=tensorflow at the start of each session, and I'd like to avoid solutions involving tools like direnv.

How can I get conda's keras to use tensorflow by default?


回答1:


The problem is probably in the file activate.sh of the keras package on conda-forge. The export statements in this file are unnecessary and should be removed IMO. There's just no reason to restrict linux users to use theano as Keras backend (or TensorFlow for Mac OSX).

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

You could solve the problem by:

  1. Removing those environment settings from activate.sh.
  2. Removing currently installed keras and keras-gpu, and then install keras with conda install -c defaults keras: the non-conda-forge version of keras seems to be okay. I didn't find any of those env settings on my machine.
  3. pip install keras: removing currently installed keras and keras-gpu, and then install the python package only.


来源:https://stackoverflow.com/questions/46572361/configuring-keras-to-use-tensorflow-instead-of-theano

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!