Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does keras use gpu automatically?

It seems like it uses gpu automatically, but I do not know why.

First, I declared as below

tf_config = tf.ConfigProto( allow_soft_placement=True )
tf_config.gpu_options.allow_growth = True
sess = tf.Session(config=tf_config) 
keras.backend.set_session(sess)

Then I defined some model as below

with K.tf.device('/gpu:0'):
    some keras model

This is obvious that it will use the gpu and I checked it uses the first gpu(with index 0) as I expected.

But then, I removed the line

with K.tf.device('/gpu:0'):

and re-indented all the keras model. I ran the code, it still seems like using first gpu(with index 0).

On my ubuntu I used nvidia-smi command to check the gpu memory usage, and I looked on the process manager on my windows.

Both of them take the gpu memory and its usages.

As far as I remember, tensorflow does not use gpu if I do not spare them to its model. But with Keras it seems like it uses gpu automatically ... is it because I ran the code

tf_config = tf.ConfigProto( allow_soft_placement=True )
tf_config.gpu_options.allow_growth = True
sess = tf.Session(config=tf_config) 
keras.backend.set_session(sess)

or is there some other reason I am missing?

like image 310
Isaac Sim Avatar asked Jan 27 '23 23:01

Isaac Sim


1 Answers

According to the documentation TensorFlow will use GPU by default if it exist:

If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be given priority when the operation is assigned to a device. For example, matmul has both CPU and GPU kernels. On a system with devices cpu:0 and gpu:0, gpu:0 will be selected to run

like image 59
pouyan Avatar answered Jan 31 '23 18:01

pouyan