Skip to content

Instantly share code, notes, and snippets.

@AmericanEnglish
Last active September 24, 2020 06:15
Show Gist options
  • Save AmericanEnglish/f2a58a31cb6baed83599c3ca5d61735d to your computer and use it in GitHub Desktop.
Save AmericanEnglish/f2a58a31cb6baed83599c3ca5d61735d to your computer and use it in GitHub Desktop.
How to get Keras to use multiple GPUs with Tensorflow 2.0.0 =<
from mymodel import create_model
from data import x, y
if __name__ == "__main__":
from tensorflow import distribute as D
from tensorflow import config
# Get a list of physical devices, specifically GPUs
devices = [device for device in config.list_physical_devices() if "GPU" == device.device_type]
devices = ["/gpu:{}".format(i) for i, device in enumerate(devices)]
# Setup a single machine multi-gpu strategy that does not use NCCL
strat = D.MirroredStrategy(devices=devices, cross_device_ops=D.HierarchicalCopyAllReduce())
with strat.scope():
model = create_model(**kwargs)
# Keras / TensorFlow is now using a mirrored learning strategy on all GPUs available
model.fit(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment