TensorFlow: To GPU or Not to GPU?
TensorFlow: To GPU or Not to GPU?
In this article, I'm going to share how I chose a version of TensorFlow to install — which is *not* quite as easy as it appears at first
Join the DZone community and get the full member experience.Join For Free
As I've been wanting to maximize my TensorFlow compute power, I decided to install and optimize it on all of my home computers. On my desk, I actually run one of each of the major desktop platforms: an HP running Windows 10, a 2011 model iMac, and an alienware machine that I moved to Ubuntu Linux (which I'm writing this on!).
In this post, I'm going to share how I chose a version of TensorFlow to install. It's not quite as easy as it appears at first, as you need to understand your system capabilities when it comes to GPU, as well as the version of Python that you're using. Then, when it comes to installing, you can choose a "release" version or you can choose a nightly build. The latter sounds scary and brittle — and you probably think you need to be able to compile the code yourself. But it's not that bad, and in this article, I'll show you how I easily got the nightly build to run on my machines. (This article focuses on Ubuntu Linux; I'll follow up with Windows/MacOS versions soon.)
When installing TensorFlow, there are four main options:
- Python 2.x
- Python 2.x + GPU
- Python 3.x
- Python 3.x + GPU
Having seen how much a GPU helps with my rendering work (example here), I charged straight into the GPU version, only to hit issues with CUDA drivers. At the time of writing this, the release version of TensorFlow used version 8 as a dependency, whereas my Linux system had version 9. I then spent hours twiddling with settings on Linux to try to go back to version 8, only to succeed and find that it still failed. I hadn't done the most obvious thing — and that was to check that my system had an NVidia card that supports CUDA. I'll be honest. I had no idea what CUDA actually was before digging into all of this; I just thought I needed a system with a GPU to use the GPU. Not the case.
So, before choosing whether you're going to use the GPU version, you need to know if it's going to work. This boils down to seeing if you have a machine that supports CUDA and making sure all the right goodies for CUDA are installed.
Open a terminal and issue this prompt:
lspci | grep -i nvidia
Take a look at what is then returned — here's what my system gave:
See the model name — in this case, GeForce GTX 860M. That's what you need to check for compatibility with CUDA on NVidia's site.
In my case, my GPU is listed (yay!), so I know I can install TensorFlow with GPU support. Per the TensorFlow site, I see that there's a dependency library I need to install called
libcupti. Install that next:
sudo apt-get install libcupti-dev
In the next step, you'll see how to install TensorFlow itself. I'm going with the nightly build option, which isn't as scary as it sounds. If you want to install the release version of TensorFlow, you can follow the instructions here.
Install TensorFlow From Nightly Builds
Earlier, we saw how to determine if you have a supported GPU. If you don't, then simply install the non-GPU version of TensorFlow. Another dependency, of course, is the version of Python you're running, and its associated
pip tool. If you don't have either, you should install them now.
If you don't yet have Python, I'd recommend you stop reading now, and go get it with the instructions for your system here.
With my flavor of Ubuntu Linux, I have Python 2.7, and I installed
pip with this command:
sudo apt-get install python-pip python-dev
Note that if you're using Python 3.x, you should use the packages
Note also that you should have at least version 8.1 of
pip. If you haven't updated for a while, you should do so now. (Check your version with
Once you have done all this, you can install TensorFlow from a nightly build as easily as this:
sudo pip install tf-nightly-gpu
Or, if your system doesn't support CUDA (see the above section), then use:
sudo pip install tf-nightly
You can now check if it worked by using the following steps.
First, open a Python interpreter:
Then, import the TensorFlow libraries:
import tensorflow as tf
Note that if you didn't install TensorFlow properly, or if you installed the GPU version on an unsupported system, you'll get an error here. CUDA errors are very common at this point. I find this to be a great litmus test to see if you can use TensorFlow before going any further. If you have any errors, let me know in the comments below. If it works, then print out the TensorFlow version:
(Note that there are two underscores before and after
When you're done, you should see the TensorFlow version printed out like this:
You can see that because I installed from nightly builds, I got version 1.4 — the latest at the time of writing this post!
I'll follow up with the Mac and Windows experience soon. Let me know if you have any questions!
Published at DZone with permission of Laurence Moroney , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.