From efc3bd4a90deb407af59a951e44a83f56993f6ed Mon Sep 17 00:00:00 2001 From: Fahad Khalid <f.khalid@fz-juelich.de> Date: Tue, 4 May 2021 08:32:43 +0200 Subject: [PATCH] Updated the course material so that the examples comply with TF2. --- .../examples/mnist_epoch_distributed.py | 17 ++++++++--------- course_material/examples/mnist_single_gpu.py | 13 ++++++------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/course_material/examples/mnist_epoch_distributed.py b/course_material/examples/mnist_epoch_distributed.py index 7c9080e..504b2a8 100644 --- a/course_material/examples/mnist_epoch_distributed.py +++ b/course_material/examples/mnist_epoch_distributed.py @@ -4,8 +4,6 @@ # Version 2.0 (see the NOTICE file for details). """ - This program is an adaptation of the following code sample: - https://github.com/horovod/horovod/blob/master/examples/keras_mnist.py. The program creates and trains a shallow ANN for handwritten digit classification using the MNIST dataset. @@ -13,14 +11,14 @@ example epochs are distributed across the Horovod ranks, not data. To run this sample use the following command on your - workstation/laptop equipped with a GPU: + workstation/laptop: - mpirun -np 1 python -u mnist_epoch_distributed.py + mpirun -np 1 python -u mnist_epoch_distributed.py If you have more than one GPU on your system, you can increase the number of ranks accordingly. - The code has been tested with Python 3.7.5, tensorflow-gpu 1.13.1, and + The code has been tested with Python 3.8.7, tensorflow 2.3.1, and horovod 0.16.2. Note: This code will NOT work on the supercomputers. @@ -30,16 +28,17 @@ import math import tensorflow as tf import horovod.tensorflow.keras as hvd -from tensorflow.python.keras import backend as K # Horovod: initialize Horovod. hvd.init() # Horovod: pin GPU to be used to process local rank (one GPU per process) -config = tf.ConfigProto() -config.gpu_options.visible_device_list = str(hvd.local_rank()) -K.set_session(tf.Session(config=config)) +gpus = tf.config.experimental.list_physical_devices('GPU') +if gpus: + tf.config.experimental.set_visible_devices(gpus[hvd.local_rank()], 'GPU') + for gpu in gpus: + tf.config.experimental.set_memory_growth(gpu, True) # Reference to the MNIST dataset mnist = tf.keras.datasets.mnist diff --git a/course_material/examples/mnist_single_gpu.py b/course_material/examples/mnist_single_gpu.py index 794150f..2918cd0 100644 --- a/course_material/examples/mnist_single_gpu.py +++ b/course_material/examples/mnist_single_gpu.py @@ -4,17 +4,16 @@ # Version 2.0 (see the NOTICE file for details). """ - This program is an adaptation of the code sample available at - https://www.tensorflow.org/tutorials/. The program creates - and trains a shallow ANN for handwritten digit classification - using the MNIST dataset. + This program is an adaptation of a previously available code sample + at https://www.tensorflow.org/tutorials/. The program creates and trains a + shallow ANN for handwritten digit classification using the MNIST dataset. To run this sample use the following command on your - workstation/laptop equipped with a GPU: + workstation/laptop: - python -u mnist.py + python -u mnist.py - The code has been tested with Python 3.7.5 and tensorflow-gpu 1.13.1. + The code has been tested with Python 3.8.7 and tensorflow 2.3.1 Note: This code will NOT work on the supercomputers. -- GitLab