This worked for me on TensorFlow 2.1.0 (per: https://www.tensorflow.org/api_docs/python/tf/config/experimental/set_memory_growth)
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
for device in physical_devices:
tf.config.experimental.set_memory_growth(device, True)
It's a simple fix, but it was a nightmare to figure it all out
On Windows I found the Keras install in Anaconda3\Lib\site-packages\keras
sources:
https://www.tensorflow.org/guide/using_gpu
https://github.com/keras-team/keras/blob/master/keras/backend/tensorflow_backend.py
Find the following in your keras/tensorflow_backend.py file you'll add config.gpu_options.allow_growth= True in both places
if _SESSION is None:
if not os.environ.get('OMP_NUM_THREADS'):
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth=True
else:
num_thread = int(os.environ.get('OMP_NUM_THREADS'))
config = tf.ConfigProto(intra_op_parallelism_threads=num_thread,
allow_soft_placement=True)
config.gpu_options.allow_growth=True
_SESSION = tf.Session(config=config)
session = _SESSION
Make sure you have no other processes using the GPU running. Run nvidia-smi to check this.
SOURCE: An issue brought up by @reedwm.
Adding the following lines after imports solved the problem:
configuration = tf.compat.v1.ConfigProto()
configuration.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=configuration)
This Answer is much related to Tensorflow:
Sometimes Tensorflow fails at creation in Windows.
Restarting the notebook using gpu solves it in most cases
If it doesnt then try restarting the notebook after adding these options in your code.
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.9)
tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,allow_soft_placement=True)
I never had such error while using Keras But try restarting your notebook
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With