How do I use TensorFlow GPU?

前端 未结 7 1776
予麋鹿
予麋鹿 2020-11-28 06:00

How do I use TensorFlow GPU version instead of CPU version in Python 3.6 x64?

import tensorflow as tf

Pytho

相关标签:
7条回答
  • 2020-11-28 06:36

    Follow this tutorial Tensorflow GPU I did it and it works perfect.

    Attention! - install version 9.0! newer version is not supported by Tensorflow-gpu

    Steps:

    1. Uninstall your old tensorflow
    2. Install tensorflow-gpu pip install tensorflow-gpu
    3. Install Nvidia Graphics Card & Drivers (you probably already have)
    4. Download & Install CUDA
    5. Download & Install cuDNN
    6. Verify by simple program

    from tensorflow.python.client import device_lib print(device_lib.list_local_devices())

    0 讨论(0)
  • 2020-11-28 06:36

    Strangely, even though the tensorflow website 1 mentions that CUDA 10.1 is compatible with tensorflow-gpu-1.13.1, it doesn't work so far. tensorflow-gpu gets installed properly though but it throws out weird errors when running.

    So far, the best configuration to run tensorflow with GPU is CUDA 9.0 with tensorflow_gpu-1.12.0 under python3.6.

    Following this configuration with the steps mentioned in https://stackoverflow.com/a/51307381/2562870 (the answer above), worked for me :)

    0 讨论(0)
  • 2020-11-28 06:42

    Uninstall tensorflow and install only tensorflow-gpu; this should be sufficient. By default, this should run on the GPU and not the CPU. However, further you can do the following to specify which GPU you want it to run on.

    If you have an nvidia GPU, find out your GPU id using the command nvidia-smi on the terminal. After that, add these lines in your script:

    os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
    os.environ["CUDA_VISIBLE_DEVICES"] = #GPU_ID from earlier
    
    config = tf.ConfigProto()
    sess = tf.Session(config=config)
    

    For the functions where you wish to use GPUs, write something like the following:

    with tf.device(tf.DeviceSpec(device_type="GPU", device_index=gpu_id)):
    
    0 讨论(0)
  • 2020-11-28 06:48

    Follow the steps in the latest version of the documentation. Note: GPU and CPU functionality is now combined in a single tensorflow package

    pip install tensorflow
    
    # OLDER VERSIONS pip install tensorflow-gpu
    

    https://www.tensorflow.org/install/gpu

    This is a great guide for installing drivers and CUDA if needed: https://www.quantstart.com/articles/installing-tensorflow-22-on-ubuntu-1804-with-an-nvidia-gpu/

    0 讨论(0)
  • 2020-11-28 06:53

    The 'new' way to install tensorflow GPU if you have Nvidia, is with Anaconda. Works on Windows too. With 1 line.

    conda create --name tf_gpu tensorflow-gpu 
    

    This is a shortcut for 3 commands, which you can execute separately if you want or if you already have a conda environment and do not need to create one.

    1. Create an anaconda environment conda create --name tf_gpu

    2. Activate the environment activate tf_gpu

    3. Install tensorflow-GPU conda install tensorflow-gpu

    You can use the conda environment.

    0 讨论(0)
  • 2020-11-28 06:55

    First you need to install tensorflow-gpu, because this package is responsible for gpu computations. Also remember to run your code with environment variable CUDA_VISIBLE_DEVICES = 0 (or if you have multiple gpus, put their indices with comma). There might be some issues related to using gpu. if your tensorflow does not use gpu anyway, try this

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