Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a MirroredStrategy for multi-GPU training.
TensorFlow
strategy = tf.distribute.[1]()Attempts:
3 left
💡 Hint
Common Mistakes
Using OneDeviceStrategy which only uses one device.
Confusing TPUStrategy with GPU strategies.
✗ Incorrect
MirroredStrategy is used to distribute training across multiple GPUs on one machine.
2fill in blank
mediumComplete the code to build and compile the model inside the strategy scope.
TensorFlow
with strategy.[1](): model = tf.keras.Sequential([...]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' which is for executing functions, not defining scope.
Using 'strategy' which is the object, not a method.
✗ Incorrect
The 'scope' method is used to specify the context for building and compiling the model under the distribution strategy.
3fill in blank
hardFix the error in the code to distribute the dataset for multi-GPU training.
TensorFlow
dist_dataset = strategy.[1](dataset)Attempts:
3 left
💡 Hint
Common Mistakes
Using 'distribute_dataset' which does not exist.
Using 'distribute' which is not a method.
✗ Incorrect
The correct method to distribute a dataset is 'experimental_distribute_dataset' in TensorFlow 2.x.
4fill in blank
hardFill both blanks to correctly reduce the loss across GPUs and update the optimizer.
TensorFlow
per_replica_losses = strategy.run(train_step, args=(dist_inputs,)) loss = strategy.[1](tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None) optimizer.[2](loss)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reduce_sum' which does not exist.
Using 'apply_gradients' directly without computing gradients.
✗ Incorrect
We use 'reduce' to combine losses from all replicas (with ReduceOp.MEAN or SUM) and 'minimize' to update the optimizer with the loss.
5fill in blank
hardFill all three blanks to create a distributed dataset, iterate over it, and run the training step.
TensorFlow
dist_dataset = strategy.[1](dataset) iterator = iter(dist_dataset) for _ in range(steps_per_epoch): dist_inputs = next(iterator) strategy.[2](train_step, args=([3],))
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'distribute_dataset' which is not a valid method.
Confusing 'dist_inputs' with 'dataset'.
✗ Incorrect
We use 'experimental_distribute_dataset' to distribute the dataset, 'run' to execute the training step on each replica, and 'dist_inputs' as the input argument.
