Where is the tensorflow session in Keras

LinTIna picture LinTIna · Dec 6, 2018 · Viewed 11.5k times · Source

I'm new to keras and tensorflow. When I write programs with tensorflow, I must bulid a session to run the graph. However, when I use keras, although the backend is obviously tensorflow, I don't see session in the keras code. It seems all thing is done after the model.compile and model.fit.

So, how does Keras work? where is the tensorflow session? and instead of session, can I use eager execution with keras?

Thanks in advance and sorry for my English

Answer

GPhilo picture GPhilo · Dec 6, 2018

Keras doesn't directly have a session because it supports multiple backends. Assuming you use TF as backend, you can get the global session as:

from keras import backend as K
sess = K.get_session()

If, on the other hand, yo already have an open Session and want to set it as the session Keras should use, you can do so via:

K.set_session(sess)