Last active
September 24, 2020 06:15
-
-
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 =<
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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