当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Change default configuration for TensorFlow Session?

问题描述:

Is it possible to change the default Session configuration, either within Python or by setting environment variables, etc.?

Specifically I'd like

with tf.Session() as sess:

...

to use up much less memory when I'm running small side tests in parallel to other jobs. So I'd the above to behave the same as

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1)

config = tf.ConfigProto(gpu_options=gpu_options)

with tf.Session(config=config) as sess:

...

网友答案:

Don't think there's a way to set process-wide default, but here's the pattern I use.

def create_session():
  config = tf.ConfigProto(log_device_placement=True)
  config.gpu_options.per_process_gpu_memory_fraction=0.3 # don't hog all vRAM
  sess = tf.InteractiveSession("", config=config)
  return sess

sess=create_session()
a=tf.constant(1)
b=tf.constant(2)
sess.run([a+b])
分享给朋友:
您可能感兴趣的文章:
随机阅读: