keras 切换后端 TensorFlow,cntk,theano

本文介绍了Keras后端切换的方法,包括在Linux和Windows系统下找到并修改keras.json文件中的后端配置,还可通过定义环境变量覆盖配置。同时提到Keras能使用外部后端,需满足特定函数要求。此外,详细说明了keras.json文件包含的数据格式、模糊常量、浮点精度等设置。

参考

https://keras.io/#configuring-your-keras-backend

https://keras.io/backend/


Switching from one backend to another

If you have run Keras at least once, you will find the Keras configuration file at:

$HOME/.keras/keras.json

If it isn't there, you can create it.


linux

~/.keras/目录下 keras.json文件 如果不存在可以手动创建

windows

在系统盘中用户(users)当前用户名下的.keras文件夹中,有个keras.json文件。


打开后更改"backend": "xxxxxx"

xxxxx表示需要更换的后端,如"theano", "tensorflow", or "cntk",



NOTE for Windows Users: Please replace $HOME with %USERPROFILE%.

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.

You can also define the environment variable KERAS_BACKEND and this will override what is defined in your config file :

KERAS_BACKEND=tensorflow python -c "from keras import backend"
Using TensorFlow backend.

In Keras it is possible to load more backends than "tensorflow", "theano", and "cntk". Keras can use external backends as well, and this can be performed by changing the keras.json configuration file, and the "backend" setting. Suppose you have a Python module called my_module that you wanted to use as your external backend. The keras.json configuration file would be changed as follows:

{
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "my_package.my_module"
}

An external backend must be validated in order to be used, a valid backend must have the following functions: placeholder, variable and function.

If an external backend is not valid due to missing a required entry, an error will be logged notifying which entry/entries are missing.


keras.json details

The keras.json configuration file contains the following settings:

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

You can change these settings by editing $HOME/.keras/keras.json.

  • image_data_format: String, either "channels_last" or "channels_first". It specifies which data format convention Keras will follow. (keras.backend.image_data_format() returns it.)
  • For 2D data (e.g. image), "channels_last" assumes (rows, cols, channels) while "channels_first" assumes (channels, rows, cols).
  • For 3D data, "channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels) while "channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).
  • epsilon: Float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
  • floatx: String, "float16", "float32", or "float64". Default float precision.
  • backend: String, "tensorflow", "theano", or "cntk".

转载于:https://www.cnblogs.com/XUEYEYU/p/10913200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值