with tf.device('/gpu:0'):
x = tf.placeholder(tf.float32, shape=(None, 20, 64))
y = LSTM(32)(x) # all ops in the LSTM layer will live on GPU:0
with tf.device('/cpu:0'):
x = tf.placeholder(tf.float32, shape=(None, 20, 64))
y = LSTM(32)(x) # all ops in the LSTM layer will live on CPU:0
import tensorflow as tf
from keras import backend as K
num_cores = 4
if GPU:
num_GPU = 1
num_CPU = 1
if CPU:
num_CPU = 1
num_GPU = 0
config = tf.ConfigProto(intra_op_parallelism_threads=num_cores,
inter_op_parallelism_threads=num_cores,
allow_soft_placement=True,
device_count = {'CPU' : num_CPU,
'GPU' : num_GPU}
)
session = tf.Session(config=config)
K.set_session(session)
在这里,使用 booleansGPU和 CPU,我们通过严格定义 Tensorflow 会话允许访问的 GPU 和 CPU 数量来指示是否要使用 GPU 或 CPU 运行代码。变量 num_GPU和 num_CPU定义此值。然后,num_cores设置可通过 intra_op_parallelism_threads和 inter_op_parallelism_threads使用的 CPU 核数。
The intra_op_parallelism_threads variable dictates the number of threads a parallel operation in a single node in the computation graph is allowed to use (intra). While the inter_ops_parallelism_threads variable defines the number of threads accessible for parallel operations across the nodes of the computation graph (inter).
allow_soft_placement允许在 CPU 上运行操作,如果满足以下任何一个条件:
操作没有 GPU 实现
没有已知或注册的 GPU 设备
there is a need to co-locate with other inputs from the CPU