module 'tensorflow' has no attribute 'GPUOptions'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error AttributeError: module 'tensorflow' has no attribute 'GPUOptions' occurs when code written for TensorFlow 1.x is run on TensorFlow 2.x. In TF1, tf.GPUOptions was used to configure GPU memory allocation (memory growth, per-process memory fraction, visible devices). TF2 reorganized the GPU configuration API, moving these settings to tf.config.experimental and later to tf.config.
The Error
The Fix: TF2 GPU Configuration
Memory Growth (Allocate as Needed)
The most common use case — prevent TensorFlow from grabbing all GPU memory at startup:
Limit GPU Memory
Set a hard memory limit instead of using a fraction:
Restrict Visible GPUs
Or use the environment variable:
Migration Table: TF1 → TF2
| TF1 Code | TF2 Equivalent |
tf.GPUOptions(per_process_gpu_memory_fraction=0.5) | tf.config.set_logical_device_configuration(gpu, [LogicalDeviceConfiguration(memory_limit=MB)]) |
tf.GPUOptions(allow_growth=True) | tf.config.experimental.set_memory_growth(gpu, True) |
tf.GPUOptions(visible_device_list='0') | tf.config.set_visible_devices(gpus[0], 'GPU') |
tf.ConfigProto(gpu_options=...) | Not needed — use tf.config directly |
tf.Session(config=config) | Not needed — TF2 uses eager execution |
Full TF1 → TF2 Migration Example
TF1 (Old)
TF2 (New)
Using tf.compat.v1 (Temporary Fix)
If you cannot migrate immediately, use the compatibility module:
This is a temporary bridge — plan to migrate to native TF2 APIs.
Checking GPU Availability
Common Pitfalls
- Must configure before any TF operation: GPU configuration (
set_memory_growth,set_visible_devices) must be called before any TensorFlow operation. Once TF initializes the GPU runtime, configuration changes raise aRuntimeError. set_memory_growthvsmemory_limit: You cannot use both on the same GPU. Choose one approach: either let TF grow memory as needed (set_memory_growth) or set a hard cap (LogicalDeviceConfiguration).- Environment variable precedence:
CUDA_VISIBLE_DEVICESis applied before TF sees any GPUs. If you setCUDA_VISIBLE_DEVICES=1, thentf.config.list_physical_devices('GPU')only shows one GPU at index 0 (which is actually physical GPU 1). - Mixed precision: TF2 mixed precision (
tf.keras.mixed_precision) can reduce GPU memory usage significantly. Consider this before hard-limiting memory. - Docker containers: In Docker, you need
--gpusflag:docker run --gpus all. Without it,list_physical_devices('GPU')returns an empty list regardless of configuration.
Summary
tf.GPUOptionswas removed in TF2 — usetf.configAPIs instead- Use
tf.config.experimental.set_memory_growth(gpu, True)to enable dynamic memory allocation - Use
tf.config.set_logical_device_configurationto set a hard memory limit - Use
tf.config.set_visible_devicesto restrict which GPUs are used - For temporary compatibility, use
tf.compat.v1.GPUOptions - Configure GPU settings before any TensorFlow operation

