Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

Running multiple graphs in different device modes in TensorFlow

I am operating on multiple graphs simultanously. I would for example, like one graph to use CPU and the other to use the GPU. How can I achieve this ?

Current approach and its problems

When I use a tf.Session() with a tf.ConfigProto as follows, it does not work and still uses a GPU.

config = tf.ConfigProto(
            device_count = {'GPU': 0}
        )

I have to use the environment variable CUDA_VISIBLE_DEVICES to disable the use of a GPU. I later on use os.unsetenv() to remove this variable after my work.

These solutions are not useful to me because for one graph I want GPUs to be used and for the other I don't want the GPUs to be used. Setting os.environ() will affect both the graphs.

How can I achieve my purpose ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The config created by tf.ConfigProto() can set the visible devices for the sessions. For exmaple:

config_cpu = tf.ConfigProto()
config_cpu.gpu_options.visible_device_list=''
sess_cpu = tf.Session(config=config_cpu)

config_gpu = tf.ConfigProto()
config_gpu.gpu_options.visible_device_list='0'
sess_gpu = tf.Session(config=config_gpu)

Then the graph in the session, sess_cpu, should run on CPU only and the graph in the session, sess_gpu, should run on GPU 0 only. To prevent tensorflow occupying the whole GPU memory, you can set config_gpu.gpu_options.allow_growth=True. Similar configurations can be adopted for your customized needs. You can take a look at the tf.ConfigProto if you want to use other configurations.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...