question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiprocessing: Failed to get device properties

See original GitHub issue

I would like to use a keras model in a multiprocessing setup. The model is used in a generator, which produces data to train another model.

As long as I don’t use multiprocessing, everything works fine. But with multiprocessing, I get the following error:

E tensorflow/core/grappler/clusters/utils.cc:81] Failed to get device properties, error code: 3

I searched how to use Keras in a multithreaded context and found this: https://github.com/keras-team/keras/issues/5640

Apparently, I need to call _make_predict_function and get the tensorflow graph. I added this to my code:

before any training:

q_approximator = create_model()
q_approximator_fixed = create_model()

q_approximator._make_predict_function()
q_approximator_fixed._make_predict_function()

# only this one will be trained
q_approximator.compile(RMSprop(LEARNING_RATE, rho=RHO, epsilon=EPSILON), loss=huber_loss)

graph = tf.get_default_graph()
#graph = K.get_session().graph         # this way also doesn't work

inside the generator:

with graph.as_default():
               q_values = q_approximator_fixed.predict([state.reshape(1, *INPUT_SHAPE),
                                                        np.ones((1, NUM_ACTIONS))])

and finally, the training setup:

q_approximator.fit_generator(interaction_generator(q_approximator_fixed,
                                                           replay_memory,
                                                           exploration,
                                                           interaction_counter,
                                                           interaction_lock),
                                     epochs=10, steps_per_epoch=BATCH_SIZE * 1000,
                                     use_multiprocessing=True,
                                     workers=1)

With just 1 worker and no multiprocessing it works fine. Multiple workers and no multiprocessing, also fine. But a single worker and multiprocessing makes the program crash with the above error message.

How can I use a keras model in a multiprocessing context ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15

github_iconTop GitHub Comments

50reactions
wt-huangcommented, Nov 2, 2018

Closing as this is resolved

1reaction
VikasOjha666commented, Apr 13, 2019

Try reinstalling your graphics driver

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use keras in multiprocessing - python - Stack Overflow
To use a keras model in multiple processes, you have to do the following: ... Failed to get device properties, error code: 3....
Read more >
Tips and Tricks for GPU and Multiprocessing in TensorFlow
In this post, I'll share some tips and tricks when using GPU and multiprocessing in machine learning projects in Keras and TensorFlow.
Read more >
torch.multiprocessing — PyTorch 1.13 documentation
torch.multiprocessing is a wrapper around the native multiprocessing module. ... Python's multiprocessing sometimes fails to clean up its children.
Read more >
Python Multiprocessing Pool: The Complete Guide
The Python Multiprocessing Pool class allows you to create and manage process pools in Python. Although the Multiprocessing Pool has been ...
Read more >
multiprocessing — Process-based parallelism — Python 3.11 ...
The multiprocessing module also introduces APIs which do not have analogs in the ... It is an error to attempt to join a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found