La solution a été obtenue à partir de Tensorflow set CUDA_VISIBLE_DEVICES within jupyter grâce à Yaroslav.
La plupart des informations ont été obtenues à partir de la documentation de Tensorflow Stackoverflow. Je ne suis pas autorisé à ne pas le poster pas sûr pourquoi.
Insérez ceci au début de votre code.
from tensorflow.python.client import device_lib
# Set the environment variables
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# Double check that you have the correct devices visible to TF
print("{0}\nThe available CPU/GPU devices on your system\n{0}".format('=' * 100))
print(device_lib.list_local_devices())
Different options to start with GPU or CPU. I am using the CPU. Can be changed from the below options
with tf.device('/cpu:0'):
# with tf.device('/gpu:0'):
# with tf.Graph().as_default():
Utilisez les lignes suivantes dans la session:
config = tf.ConfigProto(device_count={'GPU': 1}, log_device_placement=False,
allow_soft_placement=True)
# allocate only as much GPU memory based on runtime allocations
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# Session needs to be closed
sess.close()
La ligne ci-dessous résoudre le problème des ressources verrouillé par python
with tf.Session(config=config) as sess:
Un autre article utile de comprendre l'importance de 'with' Veuillez vérifier le tf.Session() officiel de tensorflow.
explication des paramètres
To find out which devices your operations and tensors are assigned to, create the session with
log_device_placement configuration option set to True.
TensorFlow to automatically choose an existing and supported device to run the operations in case the specified
one doesn't exist, you can set allow_soft_placement=True in the configuration option when creating the session.