Issue when using custom Likelihood class with Tensorflow
I have a custom likelihood class that uses a Tensorflow model in the log-likelihood calculation. I intend to sample this likelihood class with the samplers built in to bilby but this currently does not work because of how Tensorflow behaves with multiprocessing and how bilby handles the likelihood. In particular, the script hangs when using multiple threads with Dynesty and CPnest (I haven't tested the other samplers) and does not return any errors.
Tensorflow/Keras are known to have problems with multiprocessing (https://github.com/keras-team/keras/issues/9964) and an easy work around is to avoid importing Tensorflow in the main process and instead import it in each of the sub-processes started by multiprocessing (for example). I've therefore written my likelihood class to import Tensorflow the first time the log-likelihood is evaluated which would ideally be when the log-likelihood is first evaluated in each thread.
This however does not resolve the issue because the log-likelihood is evaluated when the Sampler class is initialised and the likelihood is timed and the ratio verified: https://git.ligo.org/lscsoft/bilby/blob/master/bilby/core/sampler/base_sampler.py#L112. So when the sampling starts on the separate threads the model fails to load correctly since Tensorflow has already been imported and the script hangs without returning an error.
I realise that the core of this issue lies with how Tensorflow and multiprocessing interact and how the different samplers handle multiple threads rather than with bilby but wanted to highlight this issue since Tensorflow is a popular python package that sees a lot of use.