Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OperatorNotAllowedInGraphError when running the example Usage code #4

Closed
homerobse opened this issue Jun 7, 2021 · 2 comments
Closed

Comments

@homerobse
Copy link

homerobse commented Jun 7, 2021

Hello!

Thanks for posting this code online. Trying to use it, when I run the code of ESN-usage.ipynb I get the following error:

---------------------------------------------------------------------------
OperatorNotAllowedInGraphError            Traceback (most recent call last)
<ipython-input-51-ec63cda2780d> in <module>
     19 # put all together in a keras sequential model
     20 model = keras.models.Sequential()
---> 21 model.add(recurrent_layer)
     22 model.add(output)

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    515     self._self_setattr_tracking = False  # pylint: disable=protected-access
    516     try:
--> 517       result = method(self, *args, **kwargs)
    518     finally:
    519       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py in add(self, layer)
    206           # and create the node connecting the current layer
    207           # to the input layer we just created.
--> 208           layer(x)
    209           set_inputs = True
    210 

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in __call__(self, inputs, initial_state, constants, **kwargs)
    658 
    659     if initial_state is None and constants is None:
--> 660       return super(RNN, self).__call__(inputs, **kwargs)
    661 
    662     # If any of `initial_state` or `constants` are specified and are Keras

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    949     # >> model = tf.keras.Model(inputs, outputs)
    950     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
--> 951       return self._functional_construction_call(inputs, args, kwargs,
    952                                                 input_list)
    953 

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1088           layer=self, inputs=inputs, build_graph=True, training=training_value):
   1089         # Check input assumptions set after layer building, e.g. input shape.
-> 1090         outputs = self._keras_tensor_symbolic_call(
   1091             inputs, input_masks, args, kwargs)
   1092 

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _keras_tensor_symbolic_call(self, inputs, input_masks, args, kwargs)
    820       return nest.map_structure(keras_tensor.KerasTensor, output_signature)
    821     else:
--> 822       return self._infer_output_signature(inputs, args, kwargs, input_masks)
    823 
    824   def _infer_output_signature(self, inputs, args, kwargs, input_masks):

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in _infer_output_signature(self, inputs, args, kwargs, input_masks)
    861           # TODO(kaftan): do we maybe_build here, or have we already done it?
    862           self._maybe_build(inputs)
--> 863           outputs = call_fn(inputs, *args, **kwargs)
    864 
    865         self._handle_activity_regularization(inputs, outputs)

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in call(self, inputs, mask, training, initial_state, constants)
    792           new_states = [new_states]
    793         return output, new_states
--> 794     last_output, outputs, states = K.rnn(
    795         step,
    796         inputs,

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/backend.py in rnn(step_function, inputs, initial_states, go_backwards, mask, constants, unroll, input_length, time_major, zero_output_for_mask)
   4346     # output_time_zero is used to determine the cell output shape and its dtype.
   4347     # the value is discarded.
-> 4348     output_time_zero, _ = step_function(
   4349         input_time_zero, tuple(initial_states) + tuple(constants))
   4350     output_ta = tuple(

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/layers/recurrent.py in step(inputs, states)
    788       def step(inputs, states):
    789         states = states[0] if len(states) == 1 and is_tf_rnn_cell else states
--> 790         output, new_states = cell_call_fn(inputs, states, **kwargs)
    791         if not nest.is_nested(new_states):
    792           new_states = [new_states]

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
   1010         with autocast_variable.enable_auto_cast_variables(
   1011             self._compute_dtype_object):
-> 1012           outputs = call_fn(inputs, *args, **kwargs)
   1013 
   1014         if self._activity_regularizer:

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    665       try:
    666         with conversion_ctx:
--> 667           return converted_call(f, args, kwargs, options=options)
    668       except Exception as e:  # pylint:disable=broad-except
    669         if hasattr(e, 'ag_error_metadata'):

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
    348   if conversion.is_in_allowlist_cache(f, options):
    349     logging.log(2, 'Allowlisted %s: from cache', f)
--> 350     return _call_unconverted(f, args, kwargs, options, False)
    351 
    352   if ag_ctx.control_status_ctx().status == ag_ctx.Status.DISABLED:

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/autograph/impl/api.py in _call_unconverted(f, args, kwargs, options, update_cache)
    476 
    477   if kwargs is not None:
--> 478     return f(*args, **kwargs)
    479   return f(*args)
    480 

/mnt/d/projects/recirculation/esn/ESN.py in call(self, inputs, states)
    195 
    196         rkernel = self.setAlpha(self.recurrent_kernel_init)
--> 197         if self.alpha != self.alpha_store:
    198             self.clip_variables()
    199             self.echo_ratio.assign(self.echoStateRatio(rkernel))

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in __bool__(self)
    883       `TypeError`.
    884     """
--> 885     self._disallow_bool_casting()
    886 
    887   def __nonzero__(self):

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in _disallow_bool_casting(self)
    490     else:
    491       # Default: V1-style Graph execution.
--> 492       self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
    493 
    494   def _disallow_iteration(self):

~/software/miniconda3/envs/recirc/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in _disallow_in_graph_mode(self, task)
    477 
    478   def _disallow_in_graph_mode(self, task):
--> 479     raise errors.OperatorNotAllowedInGraphError(
    480         "{} is not allowed in Graph execution. Use Eager execution or decorate"
    481         " this function with @tf.function.".format(task))

OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

I tried running tf.executing_eagerly() and it returns True. And I also tried to use @tf.function above call function, but it still did not work.

Should I change the if self.alpha != self.alpha_store: condition to something else using tf.cond? Thank you for your help.

@homerobse
Copy link
Author

homerobse commented Jun 8, 2021

I added dynamic=True (as suggested here for another error) and the code now appears to be working
recurrent_layer = tf.keras.layers.RNN(esn_cell, return_sequences=True, name="rnn", dynamic=True)

@homerobse
Copy link
Author

I closed the issue, but it would be good to update the notebook if that is something essential for all current versions of Tensorflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant