简体   繁体   English

如何将 Keras 的后端更改为 Theano?

[英]How to change the backend of Keras to Theano?

I installed keras.我安装了keras。 When I import keras, it says tensorflow not installed.当我导入 keras 时,它说未安装 tensorflow。 I tried changing the backend in keras.json file to theano.我尝试将 keras.json 文件中的后端更改为 theano。 But this doesn't help as it still says no module named tensorflow.但这无济于事,因为它仍然说没有名为 tensorflow 的模块。 Please help me fix this.请帮我解决这个问题。

You can use an env variable as follow:您可以按如下方式使用 env 变量:

import os
os.environ["KERAS_BACKEND"] = "theano"

Did you try this ?你试过这个吗?

you will find the Keras configuration file at:您将在以下位置找到 Keras 配置文件:

$HOME/.keras/keras.json

The default configuration file looks like this:默认配置文件如下所示:

{
"image_data_format": "channels_last",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}

Simply change the field backend to "theano" , "tensorflow" , or "cntk" , and Keras will use the new configuration next time you run any Keras code.只需将字段backend更改为"theano""tensorflow""cntk""cntk"将在您下次运行任何 Keras 代码时使用新配置。

You can also define the environment variable KERAS_BACKEND and this will override what is defined in your config file :您还可以定义环境变量 KERAS_BACKEND,这将覆盖配置文件中定义的内容:

KERAS_BACKEND=tensorflow python -c "from keras import backend"

There is two ways to switch keras backend from tensorflow to theano, or theano to tensorflow.有两种方法可以将 keras 后端从 tensorflow 切换到 theano,或者将 theano 切换到 tensorflow。

1- Go to /home/user/.keras folder and open keras.json file. 1- 转到 /home/user/.keras 文件夹并打开 keras.json 文件。 it looks like看起来像

{ {

"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last"
}
from here you can switch from tensorflow to theano as backend, by default here 
tensorflow is keras backend. but if you your keras is showing theano as backend and you want tensorflow as backend and in this file already  tensorflow is backend then you need to follow.

second option.第二种选择。

  1. when you are using virtual conda environment then go to当您使用虚拟 conda 环境时,请转到

    "/home/user/anaconda3/envs/opensim-rl/etc/conda/activate.d" “/home/user/anaconda3/envs/opensim-rl/etc/conda/activate.d”

    In this folder you get a file "keras_activate.sh" file and change it here.在此文件夹中,您将获得一个文件“keras_activate.sh”文件并在此处进行更改。

    from export KERAS_BACKEND="theano" to export KERAS_BACKEND="tensorflow".从导出 KERAS_BACKEND="theano" 到导出 KERAS_BACKEND="tensorflow"。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM