Skip to content

Commit 2242394

Browse files
committed
Article
1 parent 2ff01a9 commit 2242394

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

Ubuntu/13_Keras_and_TensorFlow_how-tos.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,15 @@ import tensorflow as tf
354354
# For TensorFlow 2.x
355355
gpus = tf.config.list_physical_devices('GPU')
356356
if gpus:
357-
try:
358-
# Currently, memory growth needs to be the same across GPUs
359-
for gpu in gpus:
360-
tf.config.experimental.set_memory_growth(gpu, True)
361-
logical_gpus = tf.config.list_logical_devices('GPU')
362-
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
363-
except RuntimeError as e:
364-
# Memory growth must be set before GPUs have been initialized
365-
print(e)
357+
try:
358+
# Currently, memory growth needs to be the same across GPUs
359+
for gpu in gpus:
360+
tf.config.experimental.set_memory_growth(gpu, True)
361+
logical_gpus = tf.config.list_logical_devices('GPU')
362+
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
363+
except RuntimeError as e:
364+
# Memory growth must be set before GPUs have been initialized
365+
print(e)
366366
```
367367

368368
##### <a name="reserve-fraction" />2. Reserve fixed fraction
@@ -377,16 +377,16 @@ import tensorflow as tf
377377
# For TensorFlow 2.x
378378
gpus = tf.config.list_physical_devices('GPU')
379379
if gpus:
380-
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
381-
try:
382-
tf.config.set_logical_device_configuration(
383-
gpus[0],
384-
[tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
385-
logical_gpus = tf.config.list_logical_devices('GPU')
386-
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
387-
except RuntimeError as e:
388-
# Virtual devices must be set before GPUs have been initialized
389-
print(e)
380+
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
381+
try:
382+
tf.config.set_logical_device_configuration(
383+
gpus[0],
384+
[tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
385+
logical_gpus = tf.config.list_logical_devices('GPU')
386+
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
387+
except RuntimeError as e:
388+
# Virtual devices must be set before GPUs have been initialized
389+
print(e)
390390
```
391391
The `per_process_gpu_memory_fraction` acts as a hard upper bound on the amount of GPU memory
392392
that will be used by the process on each GPU on the same machine.

0 commit comments

Comments
 (0)