Keras如果是使用Theano后端的话,应该是自动不使用GPU只是用CPU的,启动GPU使用Theano内部命令即可。
对于Tensorflow后端的Keras以及Tensorflow会自动使用可见的GPU,而我需要其必须只运行在CPU上。网上查到三种方法,最后一种比较方便:
- 使用tensorflow的
with tf.device('/cpu:0'):
函数。简单操作就是把所有命令都放在前面所述的域里面。 - 使用tensorflow声明Session时的参数:
关于tensorflow中Session中的部分参数设置,以及Keras如何设置其调用的Tensorflow的Session,可以参见Keras设定GPU使用内存大小(Tensorflow backend)。
对于Tensorflow,声明Session的时候加入device_count={'gpu':0}
即可,代码如下:
import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(device_count={'gpu':0}))
- 1
- 2
- 3
对于Keras,则调用后端函数,设置其使用如上定义的Session即可,代码如下:
- 1
- 2
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF
KTF.set_session(tf.Session(config=tf.ConfigProto(device_count={'gpu':0})))
- 1
- 2
- 3
- 4
对于多线程以及GPU内存设置等可以参见Keras设定GPU使用内存大小(Tensorflow backend);更多详细内容请见tensorflow官网。
3 . 第三种是使用CUDA_VISIBLE_DEVICES命令行参数,代码如下:
CUDA_VISIBLE_DEVICES="" python3 train.py
转自https://blog.csdn.net/silent56_th/article/details/72628606
nohup ./start.sh & 默认输出到nohup.out文件
nohup ./start.sh >output 2>&1 & 指定输出到output文件
CUDA_VISIBLE_DEVICES=1 nohup python train.py &