Tensor flow toggle between CPU/GPU

前端 未结 6 1128
情歌与酒
情歌与酒 2021-01-30 13:30

Having installed tensorflow GPU (running on a measly NVIDIA GeForce 950), I would like to compare performance with the CPU.

I am running the tensorFlow MNIST tutorial c

相关标签:
6条回答
  • 2021-01-30 13:45

    Quite a long time elapsed. Recent versions of Tensorflow (at least from 2.0 on) don't require installing both versions with and without gpu support, so you can launch two separate jupyter-notebook instances. Following @Yaroslav's advice:

    $ CUDA_VISIBLE_DEVICES="" jupyter-notebook &
    $ jupyter-notebook &
    

    You will then have two separate jupyter clients open in your browser, typically http://localhost:8888/ and http://localhost:8889/, respectively without and with GPU support, where you can run the same .ipynb notebook and measure the performance difference.

    0 讨论(0)
  • 2021-01-30 13:45

    To turn off GPU, simply add this at the top of your script.

    import os
    os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
    

    (Comment it out when you want to use the GPU again)

    0 讨论(0)
  • 2021-01-30 13:46
    # Check if the server/ instance is having GPU/ CPU from python code
    import sys
    import tensorflow as tf
    from tensorflow.python.client import device_lib
    
    # device_lib.list_local_devices()     ## this command list all the processing device GPU and CPU
    
    
    device_name = [x.name for x in device_lib.list_local_devices() if x.device_type == 'GPU']
    if device_name[0] == "/device:GPU:0":
        device_name = "/gpu:0"
        #print('GPU')
    else:
        #print('CPU')
        device_name = "/cpu:0"
    
    with tf.device(device_name):
        a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
        b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
        c = tf.matmul(a, b)
    with tf.Session() as sess:
        print (sess.run(c))    
    
    0 讨论(0)
  • 2021-01-30 13:53

    Another option is to install the cpu version and the gpu version of tensorflow in two virtual environments, detailed instructions on how to install tensorflow in virtual environments are listed here https://www.tensorflow.org/get_started/os_setup; in this way, you can have the same code running in two terminals windows one uses CPU and the other uses GPU.

    0 讨论(0)
  • 2021-01-30 14:04

    try setting tf.device to cpu:0

    with tf.Session() as sess:
         with tf.device("/cpu:0"):
    
    0 讨论(0)
  • 2021-01-30 14:12

    To make GPU invisible

    export CUDA_VISIBLE_DEVICES=""
    

    To return to normal

    unset CUDA_VISIBLE_DEVICES
    
    0 讨论(0)
提交回复
热议问题