TensorFlow CPU切换到GPU,CPU/GPU互换
with tf.Session() as ses: with tf.device("/gpu:1"): matrix1=tf.constant([[3.,3.]]) matrix2=tf.constant([[2.],[2.]]) product=tf.matmul(matrix1,matrix2) 方法二: <span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf sess = tf.Session(config=tf.ConfigProto(device_count={<span class="hljs-string">'gpu'</span>:<span class="hljs-number">0</span>})) <span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf <span class="hljs-keyword">import</span> keras.backend.tensorflow_backend <span class="hljs-keyword">as</span> KTF KTF.set_session(tf.Session(config=tf.ConfigProto(device_count={<span class="hljs-string">'gpu'</span>:<span class="hljs-number">0</span>}))) 方法三:使用CUDA_VISIBLE_DEVICES命令行参数,代码如下: CUDA_VISIBLE_DEVICES="" python3 train.py Environment Variable Syntax Results CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible CUDA_VISIBLE_DEVICES="0,1" Same as above, quotation marks are optional CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked CUDA_VISIBLE_DEVICES="" No GPU will be visible
字符 | 对应的操作 |
---|---|
"/cpu:0" |
The CPU of your machine |
"/gpu:0" |
The GPU of yout machine ,if you have one |
等等依次类推: