How to seleect GPU device automatically

Hi, all
I have 4 GPUs, and I want to use one GPU per one network setting.
so I use os.environ['CUDA_VISIBLE_DEVICES']= '0'
Every time when I change the network settings, I should change the this number.
Is there any way to automatically select free-gpu device?

You can find some approaches to get the next “free” GPU in this thread.