Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: When do I need to run a tf.Session() when using tf.Keras layers or tf.Estimator API?

Tags:

I am trying to reconcile information from the TF "Graphs and Sessions" Guide and the TF "Keras" Guide and TF Estimators Guides. Now in the former it says that the a tf.Session gives the computation graph access to the physical hardware to execute the graph and train the model. Like the initial tutorials on learning TF require you to use a Session in order to run anything: variables guide, Tensors guide, etc. HOWEVER, in the TF Keras guide, the examples seem to run without any explicit call to the tf.Session or the usual with tf.Session() as sess: The Keras model is not using eager execution either. The same is true in for the Estimators API.

I have a couple of code samples. Some of them use the call to the session while others do not. I was hoping someone could clarify what are the rules for requirements for using tf.Session with Keras layers or Estimators. I mean it seems like you can set up things like run_configs for the keras estimator or standard TF.estimator and set the settings for multi-gpu, etc.

Here is an example from the TF Keras Guide for the Functional API. Note that no call is made to the session:

inputs = tf.keras.Input(shape=(32,))  # Returns a placeholder tensor

# A layer instance is callable on a tensor, and returns a tensor.
x = layers.Dense(64, activation='relu')(inputs)
x = layers.Dense(64, activation='relu')(x)
predictions = layers.Dense(10, activation='softmax')(x)

model = tf.keras.Model(inputs=inputs, outputs=predictions)

# The compile step specifies the training configuration.
model.compile(optimizer=tf.train.RMSPropOptimizer(0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Trains for 5 epochs
model.fit(data, labels, batch_size=32, epochs=100)

Thanks for any info or clarifications.

like image 861
krishnab Avatar asked Dec 20 '18 08:12

krishnab


1 Answers

The call of tf.Session() is not necessary when using only Keras. This is called when tensorflow backend is used, as can be seen here. This is only called when tensorflow backend is used and not with theano or CNTK.

Regarding the use of the Tensorflow interface with tf.Session() calls, this is explained here, which simply uses the tf.Session() as bridge between Keras and pure-TensorFlow tensors and/or functions.

You can see an example of using tf.Session() with Keras, using set_session() backend function:

session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)

from keras import backend as K

sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)
K.set_session(sess)
like image 184
jgorostegui Avatar answered Sep 27 '22 22:09

jgorostegui