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_4_one-device-strategy.ipynb
Views: 13370
One Device Strategy
In this ungraded lab, you'll learn how to set up a One Device Strategy. This is typically used to deliberately test your code on a single device. This can be used before switching to a different strategy that distributes across multiple devices. Please click on the Open in Colab badge above so you can download the datasets and use a GPU-enabled lab environment.
Imports
Define the Distribution Strategy
You can list available devices in your machine and specify a device type. This allows you to verify the device name to pass in tf.distribute.OneDeviceStrategy()
.
Parameters
We'll define a few global variables for setting up the model and dataset.
Download and Prepare the Dataset
We will use the Cats vs Dogs dataset and we will fetch it via TFDS.
Define and Configure the Model
As with other strategies, setting up the model requires minimal code changes. Let's first define a utility function to build and compile the model.
You can now call the function under the strategy scope. This places variables and computations on the device you specified earlier.
model.fit()
can be run as usual.
Once everything is working correctly, you can switch to a different device or a different strategy that distributes to multiple devices.