title: Converting Tensor to Ragged Tensor in Graph mode using Keras
body: <p>I want to convert a Tensor to a Ragged Tensor in my graph using Keras. 
  However, the function <code>RaggedTensor.from_row_lengths</code> fails in my graph. </p>
  
  <p>Tensorflow version: <code>tf-nightly 2.1.0.dev20191203</code></p>
  
  <p>Here's a code sample:  </p>
  
  <pre class="lang-py prettyprint-override"><code>import tensorflow as tf
  import numpy as np
  
  
  input_sequence = np.reshape(
      np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=np.int32),
      (2, 4))
  
  labels = np.reshape(
      np.array([1.0, 0.0, ], dtype=np.float32),
      (2, 1))
  
  dataset = tf.data.Dataset.from_tensor_slices((input_sequence, labels)).batch(1)
  
  sequence_in = tf.keras.layers.Input(shape=(None,), dtype=tf.int32)
  
  # Failing line, the rest works without the line below
  ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [2, 2])
  
  embedded_tensor = tf.keras.layers.Embedding(9, 4)(sequence_in)
  
  flat_tensor = tf.reshape(embedded_tensor, [-1, 16])
  prediction = tf.keras.layers.Dense(2)(flat_tensor)
  
  model = tf.keras.Model(inputs=sequence_in, outputs=prediction)
  model.compile(
      tf.keras.optimizers.Adam(),
      loss=tf.keras.losses.CategoricalCrossentropy(),
      metrics=['acc'])
  
  model.fit(dataset, steps_per_epoch=1)
  </code></pre>
  
  <p>The error seem to be related to the verification applied to check the Tensor shapes:</p>
  
  <pre><code>Traceback (most recent call last):
    File "myscript.py", line 18, in &lt;module&gt;
      ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [4, 1])
    File "python3.6/site-packages/tensorflow_core/python/ops/ragged/ragged_tensor.py", line 510, in from_row_lengths
      check_ops.assert_equal(nvals1, nvals2, message=msg)
    File "python3.6/site-packages/tensorflow_core/python/ops/check_ops.py", line 506, in assert_equal
      if not condition:
    File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 765, in __bool__
      self._disallow_bool_casting()
    File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 534, in _disallow_bool_casting
      self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
    File "python3.6/site-packages/tensorflow_core/python/framework/ops.py", line 523, in _disallow_in_graph_mode
      " this function with @tf.function.".format(task))
  tensorflow.python.framework.errors_impl.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
  </code></pre>
  
  <p>I can ignore the error by using <code>validate=False</code> but then it fails at the next Layer:</p>
  
  <pre class="lang-py prettyprint-override"><code>ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in, [2, 2], validate=False)
  embedded_ragged = tf.keras.layers.Embedding(9, 4)(ragged_in)
  </code></pre>
  
  <p>I wonder if it has to do with the fact that the batch size, and 'sequence_in' Tensor are not fixed. 
  So I also tried converting only the first observation to a Ragged Tensor, but the same error persists.</p>
  
  <pre class="lang-py prettyprint-override"><code>ragged_in = tf.RaggedTensor.from_row_lengths(sequence_in[0], [2, 2])
  </code></pre>