Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. def set_keras_device(device="GPU0", gpu_fraction=0.3):
  2. """
  3. Function to configure devices for Keras with the tensorflow backend
  4. """
  5. # get type of device: CPU or GPU
  6. device_type = device.rstrip("0123456789")
  7. import tensorflow as tf
  8. os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
  9. import keras.backend as K
  10.  
  11. if device_type == "GPU" or device_type == "gpu":
  12. # if no gpu is specified will use 0
  13. if device_type is not device:
  14. device_number = device[len(device_type)]
  15. else:
  16. device_number = "0"
  17. # set available gpu
  18. os.environ["CUDA_VISIBLE_DEVICES"] = device_number
  19. print("Setting up Keras to use GPU with miminal memory on {}".format(device_number))
  20. config = tf.ConfigProto()
  21. config.gpu_options.allow_growth = True
  22. config.gpu_options.per_process_gpu_memory_fraction = gpu_fraction
  23. elif device_type == "CPU" or device_type == "cpu":
  24. print("Setting up Keras to use CPU")
  25. config = tf.ConfigProto(device_count = {"GPU": 0})
  26.  
  27. # set up session
  28. K.tensorflow_backend.set_session(tf.Session(config=config))
  29. sess = tf.Session(config=config)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement