I have some part of python script where I do not want tensorflow to use GPU.
import os
import tensorflow as tf
from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPool2D
from tensorflow.keras.models import Sequential
os.environ['CUDA_VISIBLE_DEVICES'] = ""
model = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1)),
MaxPool2D(),
Flatten(),
Dense(320, activation="relu"),
Dense(10, activation="softmax"),
])
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
os.environ['CUDA_VISIBLE_DEVICES'] = "0,1" # 2 GPUs
for gpu in physical_devices:
try:
tf.config.set_logical_device_configuration(
gpu,
[tf.config.LogicalDeviceConfiguration(memory_limit=2000)]
)
except RuntimeError as e:
raise e
# dataset setup, model training...
Still physical_devices
is just empty list. Main question is how do I execute some part of code without initializing GPU and at the end I enable tensorflow to execute with GPU.