Tensorflow

From UMIACS
Revision as of 19:03, 25 October 2023 by Mbaney (talk | contribs)
Jump to navigation Jump to search

Tensorflow is a Python deep learning package from Google. The easiest way to use install it is to build a Python virtualenv with it in it.

First load GPU modules to allow access to accelerated GPGPU training.

module add cuda/9.0.176 cudnn/v7.0.5

Next you will want to create a virtualenv and source into it.

$ virtualenv env
New python executable in env/bin/python
Installing Setuptools..............................................................................................................................................................................................................................done.
Installing Pip.....................................................................................................................................................................................................................................................................................................................................done.
$ source env/bin/activate
(env) $

The next step is to ensure you have a recent copy of pip in your virtualenv.

(env) $ pip install --upgrade pip
Downloading/unpacking pip from https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
  Downloading pip-9.0.1.tar.gz (1.2MB): 1.2MB downloaded
  Running setup.py egg_info for package pip
  ....

Then you can now install the Tensorflow wheel through pip.

(env) $ pip install --upgrade tensorflow-gpu
Collecting tensorflow-gpu
  Downloading tensorflow_gpu-1.1.0-cp27-cp27mu-manylinux1_x86_64.whl (84.1MB)
    100% |████████████████████████████████| 84.1MB 15kB/s
Collecting wheel (from tensorflow-gpu)
  Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
    100% |████████████████████████████████| 71kB 2.0MB/s
...

Finally start up a python shell (or install ipython through pip) and import Tensorflow.

(env)[derek@ramawks76:/scratch0/derek ] $ python
Python 2.7.5 (default, Aug  2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> tf.__version__
'1.1.0'

You can then try a more rigourous test by running the following example.

import tensorflow as tf
mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)

To use this install after you close the shell you did this install in, you will need to both add the correct Cuda/cuDNN modules and activate the virtualenv by the source command. This includes any time you are submitting to Slurm or other resource managers.