Multithreading in tensorflow/keras Multithreading in tensorflow/keras multithreading multithreading

Multithreading in tensorflow/keras


I don't know if you fixed your issue but this looks like another question I recently answered.

  • You need to finish the graph creation in the main thread beforestarting the others.
  • In the case of keras, the graph is initialized the first time the fit or predict function is called. You can force the graph creation by calling some of the inner functions of model:

    model._make_predict_function()model._make_test_function()model._make_train_function()

    If that doesn't work, try to warm-up the model by calling on dummy data.

  • Once you finish the graph creation, call finalize() on your main graph so it can be safely shared it with different threads (that will make it read-only).

  • Finalizing the graph will also help you find other places where your graph is being unintentionaly modified.

Hope that helps you.


Tensorflow 2.x

Assuming you got newer tensorflow (question is old), I used to define sessions like this. This was to prevent one application from occupying whole gpu memory :P

import tensorflow as tfconfig = tf.compat.v1.ConfigProto()config.gpu_options.allow_growth = False  # can be true, to increase memory allocationconfig.gpu_options.per_process_gpu_memory_fraction = 0.2  # fraction of memory usedsess = tf.compat.v1.Session(config=config) 

But if you want to do it parallel, you should close session at some point.To do it, you can choose which style suits you more. Context manager seems more reasonable

Normal way:

sess = tf.compat.v1.Session(config=config) # do stuff heresess.close()

or with context manager:

with tf.compat.v1.Session(config=config):    # do stuff here

Keras

This was compatible with one models, I presume it will work with multi session correctly

Session documentaion