Tensorflow¶
- pagelastupdated
2022-01-09
Tensorflow is a commonly used Python package for deep learning.
Basic usage¶
First, check the tutorials up to and including GPU computing.
Installing via conda¶
Have a look here for details on how to install conda environments.
Creating an environment with GPU enabled Tensorflow¶
To create an environment with GPU enabled Tensorflow you can use an
environment file like this
tensorflow-env.yml
:
name: tensorflow-env
channels:
- conda-forge
dependencies:
- tensorflow=*=*cuda*
Here we install the latest tensorflow from conda-forge
-channel with an additional
requirement that the build version of the tensorflow
-package must contain
a reference to a CUDA toolkit. For a specific version replace the =*=*cuda*
with e.g. =2.8.1=*cuda*
for version 2.8.1
.
Hint
During installation conda will try to verify what is the maximum version of CUDA installed graphics cards can support and it will install non-CUDA enabled versions by default if none are found (as is the case on the login node, where environments are normally built). This can be usually overcome by setting explicitly that the packages should be the CUDA-enabled ones. It might however happen, that the environment creation process aborts with a message similar to:
nothing provides __cuda needed by tensorflow-2.9.1-cuda112py310he87a039_0
In this instance it might be necessary to override the CUDA settings used by
conda/mamba.
To do this, prefix your environment creation command with CONDA_OVERRIDE_CUDA=CUDAVERSION
,
where CUDAVERSION is the CUDA toolkit version you intend to use as in:
CONDA_OVERRIDE_CUDA="11.2" mamba env create -f cuda-env.yml
This will allow conda to assume that the respective CUDA libraries will be present at a later point and so it will skip those requirements during installation.
For more information, see this helpful post in Conda-Forge’s documentation.
Examples:¶
Simple Tensorflow/Keras model¶
Let’s run the MNIST example from Tensorflow’s tutorials:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
The full code for the example is in
tensorflow_mnist.py
.
One can run this example with srun
:
wget https://raw.githubusercontent.com/AaltoSciComp/scicomp-docs/master/triton/examples/tensorflow/tensorflow_mnist.py
module load anaconda
srun --time=00:15:00 --gres=gpu:1 python tensorflow_mnist.py
or with sbatch
by submitting
tensorflow_mnist.sh
:
#!/bin/bash
#SBATCH --gres=gpu:1
#SBATCH --time=00:15:00
module load anaconda
python tensorflow_mnist.py
Do note that by default Keras downloads datasets to $HOME/.keras/datasets
.
Running simple Tensorflow/Keras model with NVIDIA’s containers¶
Let’s run the MNIST example from Tensorflow’s tutorials:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
The full code for the example is in
tensorflow_mnist.py
.
One can run this example with srun
:
wget https://raw.githubusercontent.com/AaltoSciComp/scicomp-docs/master/triton/examples/tensorflow/tensorflow_mnist.py
module load nvidia-tensorflow/20.02-tf1-py3
srun --time=00:15:00 --gres=gpu:1 singularity_wrapper exec python tensorflow_mnist.py
or with sbatch
by submitting
tensorflow_singularity_mnist.sh
:
#!/bin/bash
#SBATCH --gres=gpu:1
#SBATCH --time=00:15:00
module load nvidia-tensorflow/20.02-tf1-py3
singularity_wrapper exec python tensorflow_mnist.py
Do note that by default Keras downloads datasets to $HOME/.keras/datasets
.