Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:
    ...
like image 430
rd11 Avatar asked May 12 '26 16:05

rd11


1 Answers

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])
like image 182
Yaroslav Bulatov Avatar answered May 14 '26 06:05

Yaroslav Bulatov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!