Skip to content

Installing TensorFlow 1.x with virtualenv

Installation

To install TensorFlow (the latest stable release) in a python virtual environment, follow the steps below.

Install TF 1.13.1

1
2
3
4
5
6
7
8
module purge
module load GCC/6.4.0-2.28  OpenMPI/2.1.2
module load CUDA/10.0.130 cuDNN/7.5.0.56-CUDA-10.0.130 
module load Python/3.6.4

virtualenv -p python3 tf-1.13.1-env
source ~/tf-1.13.1-env/bin/activate
pip install tensorflow-gpu

In the above pip install command, specifying tensorflow-gpu as the package name will install the latest stable release with GPU support. As of May 2019, the latest version is 1.13.1.

Notes:

  1. Prior to version 1.13, installation of TF requires CUDA 9.
  2. For installation of version 2.0 alpha, refer to another wiki in the TensorFlow menu.
  3. In October 2019, TF 2.0 was released. If you still want to install an older version 1.x, please specify the version explicitly when running pip install, such as pip instal tensorflow-gpu==1.14. Directly writing "tensorflow-gpu" will install the latest TF 2.0 which has fundamental updates/differences as compared with 1.x, and can produce errors.

Testing example 1

We will test TF 1.13.1 with the following commands (using this cifar10 estimator tutorial). 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
module purge
module load GCC/6.4.0-2.28  OpenMPI/2.1.2
module load CUDA/10.0.130 cuDNN/7.5.0.56-CUDA-10.0.130 
module load Python/3.6.4
source ~/tf-1.13.1-env/bin/activate
export TF_CPP_MIN_LOG_LEVEL=2 # disables the warning, doesn't enable AVX/FMA.

cd <your working dir>
git clone https://github.com/tensorflow/models.git
cd models/tutorials/image/cifar10_estimator
python generate_cifar10_tfrecords.py --data-dir=${PWD}/cifar-10-data
python cifar10_main.py --data-dir=${PWD}/cifar-10-data  --job-dir=${PWD}/TMP_cifar10  --num-gpus=4  --train-steps=1000

If you want to submit above commands through a SLURM job script, you will need add a directive line of

#SBATCH --gres=gpu:4

in your script to request 4 GPUs. Additionally, you may want to make "echo $CUDA_VISIBLE_DEVICES" your first command before running the main part. See tutorial page "Submitting a TensorFlow job" for a job script example.

Testing example 2 (tf.keras)

We will run the following the python code test_keras.py (adopted from https://www.tensorflow.org/guide/keras):

test_keras.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import tensorflow as tf
from tensorflow.keras import layers
import numpy as np

print(tf.VERSION)
print(tf.keras.__version__)

model = tf.keras.Sequential([
# Adds a densely-connected layer with 64 units to the model:
layers.Dense(64, activation='relu', input_shape=(32,)),
# Add another:
layers.Dense(64, activation='relu'),
# Add a softmax layer with 10 output units:
layers.Dense(10, activation='softmax')])

model.compile(optimizer=tf.train.AdamOptimizer(0.001), loss='categorical_crossentropy', metrics=['accuracy'])

data = np.random.random((1000, 32))
labels = np.random.random((1000, 10))

val_data = np.random.random((100, 32))
val_labels = np.random.random((100, 10))

model.fit(data, labels, epochs=10, batch_size=32, validation_data=(val_data, val_labels))

To run this code:

1
2
3
4
5
6
module purge
module load GCC/6.4.0-2.28  OpenMPI/2.1.2
module load CUDA/10.0.130 cuDNN/7.5.0.56-CUDA-10.0.130
module load Python/3.6.4
source ~/tf-1.13.1-env/bin/activate
python test_keras.py