https://keras.io/getting-started/faq/ 查看Keras 官方文档
If you are running on the TensorFlow backend, your code will automatically run on GPU if any available GPU is detected.If you are running on the Theano backend, you can use one of the following methods:
Method 1: use Theano flags.
THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py
The name 'gpu' might have to be changed depending on your device's identifier (e.g. gpu0
, gpu1
, etc).
Method 2: set up your .theanorc
: Instructions
Method 3: manually set theano.config.device
, theano.config.floatX
at the beginning of your code:
import theano
theano.config.device = 'gpu'
theano.config.floatX = 'float32'
因为在服务器上跑CNN,在$HOME 下我使用的vim ~.theanorc, 添加
[global] floatX = float32 device = gpu0 [lib] cnmem = 1之后keras就会使用GPU而不是CPU跑网络。
运行成功,会打印
Using Theano backend.
Using gpu device 0: GeForce GTX 1080