Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom and Distributed Training with Tensorflow/Week 4 - Distributed Training/C2_W4_Lab_1_basic-mirrored-strategy.ipynb
Views: 13370
Mirrored Strategy: Basic
In this ungraded lab, you'll go through some of the basics of applying Mirrored Strategy.
Imports
Load the MNIST dataset and split it into training and test chunks.
Next, you define strategy
using the MirroredStrategy()
class. Print to see the number of devices available.
Note:
If you are running this on Coursera, you'll see it gives a warning about no presence of GPU devices.
If you are running this in Colab, make sure you have selected your
Runtime
to beGPU
for it to detect it.In both these cases, you'll see there's only 1 device that is available.
One device is sufficient for helping you understand these distribution strategies.
Next, you create your training and test examples, define your batch size and also define BATCH_SIZE_PER_REPLICA
which is the distribution you are making for each available device.
A mapping function which normalizes your images:
Next, you create your training and evaluation datesets in the batch size you want by shuffling through your buffer size.
For your model to follow the strategy, define your model within the strategy's scope.
Run all the cells below and notice the results.
Afterwards comment out
with strategy.scope():
and run everything again, without the strategy. Then you can compare the results. The important thing to notice and compare is the time taken for each epoch to complete. As mentioned in the lecture, doing a mirrored strategy on a single device (which are lab environment has) might take longer to train because of the overhead in implementing the strategy. With that, the advantages of using this strategy is more evident if you will use it on multiple devices.