How to use Keras layers in eager execution?

3,460 views
Skip to first unread message

mohd....@uconn.edu

unread,
Feb 16, 2018, 12:29:12 AM2/16/18
to Discuss
I am using TensorFlow 1.5 and would like to run this code in eager execution:

import tensorflow as tf
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras.backend import learning_phase
sess = tf.Session()
x = tf.random_uniform((3, 10, 10, 2))
bn = BatchNormalization(axis=-1)
op1 = bn(x)
init = tf.global_variables_initializer()
sess.run(init)
a = sess.run(op1, feed_dict={learning_phase():1})

This is what I tried:

import tensorflow as tf
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras.backend import set_learning_phase
import  tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
x = tf.random_uniform((3, 10, 10, 2))
bn = BatchNormalization(axis=-1)
set_learning_phase(True)
op1 = bn(x)

But it gives the following error:

AttributeError: 'EagerTensor' object has no attribute '_uses_learning_phase'

Is running all Keras layers in eager execution supported?

Francois Chollet

unread,
Feb 19, 2018, 7:39:34 PM2/19/18
to Discuss
Eager execution has no concept of learning phase, that's a symbolic concept. The "learning phase" is the global symbolic state (boolean scalar) used to switch the default value of the `training` arguments in all layer calls, at once.

Here is the correct flow (which also works in symbolic mode):

import tensorflow as tf
from tensorflow import keras
import tensorflow.contrib.eager as tfe

tfe
.enable_eager_execution()

x
= tf.random_uniform((3, 10, 10, 2))

bn
= keras.layers.BatchNormalization(axis=-1)
y_training
= bn(x, training=True)
y_inference
= bn(x, training=False)

mohd....@uconn.edu

unread,
Mar 1, 2018, 12:43:48 PM3/1/18
to Discuss
What version of TensorFlow are you using?

I tried to run your snippet but this is what I get:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-5f7a0f1d2a10> in <module>()
      8
      9 bn = keras.layers.BatchNormalization(axis=-1)
---> 10 y_training = bn(x, training=True)
     11 y_inference = bn(x, training=False)

~\Anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\topology.py in __call__(self, inputs, **kwargs)
    256     """
    257     # Actually call the layer (optionally building it).
--> 258     output = super(Layer, self).__call__(inputs, **kwargs)
    259     if context.in_eager_mode():
    260       return output

~\Anaconda3\lib\site-packages\tensorflow\python\layers\base.py in __call__(self, inputs, *args, **kwargs)
    650
    651         if not in_deferred_mode:
--> 652           outputs = self.call(inputs, *args, **kwargs)
    653           if outputs is None:
    654             raise ValueError('A layer\'s `call` method should return a Tensor '

~\Anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\layers\normalization.py in call(self, inputs, training)
    109       training = K.learning_phase()
    110     output = super(BatchNormalization, self).call(inputs, training=training)
--> 111     if training is K.learning_phase():
    112       output._uses_learning_phase = True  # pylint: disable=protected-access
    113     return output

~\Anaconda3\lib\site-packages\tensorflow\python\keras\_impl\keras\backend.py in learning_phase()
    329   graph = ops.get_default_graph()
    330   if graph not in _GRAPH_LEARNING_PHASES:
--> 331     phase = array_ops.placeholder(dtype='bool', name='keras_learning_phase')
    332     _GRAPH_LEARNING_PHASES[graph] = phase
    333   return _GRAPH_LEARNING_PHASES[graph]

~\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py in placeholder(dtype, shape, name)
   1675   """
   1676   if context.in_eager_mode():
-> 1677     raise RuntimeError("tf.placeholder() is not compatible with "
   1678                        "eager execution.")
   1679

RuntimeError: tf.placeholder() is not compatible with eager execution.

Matt Potma

unread,
Mar 2, 2018, 8:28:28 PM3/2/18
to Discuss
His example worked for me, using Keras 2.1.4 and TF 1.6.0

mohd....@uconn.edu

unread,
Mar 2, 2018, 9:00:52 PM3/2/18
to Discuss
Thanks for checking. I was using TF 1.5.0.

Upgrading to TF 1.6.0 fixes the issue.
Reply all
Reply to author
Forward
0 new messages