ValueError: Unsupported value type BatchEncoding returned by IteratorSpec._serialize
Author: user06039
Pull request: no

I am trying to prediction on a data-point, but keep getting an error,

```
from transformers import TFDistilBertForSequenceClassification, DistilBertTokenizerFast

# initialize longformer tokenizer and model
tokenizer = DistilBertTokenizerFast.from_pretrained("distilbert-base-uncased", do_lower_case=True)
model = TFDistilBertForSequenceClassification.from_pretrained("MODEL")

data = tokenizer.encode_plus(
        sentence,
        padding="max_length",
        add_special_tokens=True,
        max_length=512,
        truncation=True,
    )
data['input_ids'] = tf.convert_to_tensor(np.reshape(data['input_ids'], (1, -1)))
data['attention_mask'] = tf.convert_to_tensor(np.reshape(data['attention_mask'], (1, -1)))

model.predict(data)
```

```
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-95-c8644be457c1> in <module>
----> 1 model.predict(data)

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1692           for step in data_handler.steps():
   1693             callbacks.on_predict_batch_begin(step)
-> 1694             tmp_batch_outputs = self.predict_function(iterator)
   1695             if data_handler.should_sync:
   1696               context.async_wait()

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    865     tracing_count = self.experimental_get_tracing_count()
    866     with trace.Trace(self._name) as tm:
--> 867       result = self._call(*args, **kwds)
    868       compiler = "xla" if self._jit_compile else "nonXla"
    869       new_tracing_count = self.experimental_get_tracing_count()

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    900       # In this case we have not created variables on the first call. So we can
    901       # run the first trace but we should fail if variables are created.
--> 902       results = self._stateful_fn(*args, **kwds)
    903       if self._created_variables:
    904         raise ValueError("Creating variables on a non-first call to a function"

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
   3015     with self._lock:
   3016       (graph_function,
-> 3017        filtered_flat_args) = self._maybe_define_function(args, kwargs)
   3018     return graph_function._call_flat(
   3019         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _maybe_define_function(self, args, kwargs)
   3395 
   3396     cache_key_context = self._cache_key_context()
-> 3397     cache_key = self._cache_key(args, kwargs, cache_key_context)
   3398 
   3399     try:

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _cache_key(self, args, kwargs, cache_key_context, include_tensor_ranks_only)
   3176       input_signature = pywrap_tfe.TFE_Py_EncodeArg(inputs,
   3177                                                     include_tensor_ranks_only)
-> 3178       hashable_input_signature = _make_input_signature_hashable(input_signature)
   3179     else:
   3180       del args, kwargs

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/eager/function.py in _make_input_signature_hashable(elem)
    112   """
    113   try:
--> 114     hash(elem)
    115   except TypeError:
    116     # TODO(slebedev): consider using nest.

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in __hash__(self)
    311 
    312   def __hash__(self):
--> 313     return hash(self.__get_cmp_key())
    314 
    315   def __reduce__(self):

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in __get_cmp_key(self)
    349     """Returns a hashable eq-comparable key for `self`."""
    350     # TODO(b/133606651): Decide whether to cache this value.
--> 351     return (type(self), self.__make_cmp_key(self._serialize()))
    352 
    353   def __make_cmp_key(self, value):

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in __make_cmp_key(self, value)
    367       ])
    368     if isinstance(value, tuple):
--> 369       return tuple([self.__make_cmp_key(v) for v in value])
    370     if isinstance(value, list):
    371       return (list, tuple([self.__make_cmp_key(v) for v in value]))

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in <listcomp>(.0)
    367       ])
    368     if isinstance(value, tuple):
--> 369       return tuple([self.__make_cmp_key(v) for v in value])
    370     if isinstance(value, list):
    371       return (list, tuple([self.__make_cmp_key(v) for v in value]))

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in __make_cmp_key(self, value)
    367       ])
    368     if isinstance(value, tuple):
--> 369       return tuple([self.__make_cmp_key(v) for v in value])
    370     if isinstance(value, list):
    371       return (list, tuple([self.__make_cmp_key(v) for v in value]))

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in <listcomp>(.0)
    367       ])
    368     if isinstance(value, tuple):
--> 369       return tuple([self.__make_cmp_key(v) for v in value])
    370     if isinstance(value, list):
    371       return (list, tuple([self.__make_cmp_key(v) for v in value]))

~/Desktop/Work/apps/env/lib/python3.8/site-packages/tensorflow/python/framework/type_spec.py in __make_cmp_key(self, value)
    379       return (np.ndarray, value.shape,
    380               TypeSpec.__nested_list_to_tuple(value.tolist()))
--> 381     raise ValueError("Unsupported value type %s returned by "
    382                      "%s._serialize" %
    383                      (type(value).__name__, type(self).__name__))

ValueError: Unsupported value type BatchEncoding returned by IteratorSpec._serialize
```


This is the input, I don't see any mistake in the input as well,

`Tensorflow :- 2.5.0-dev20210311`

```
{'input_ids': <tf.Tensor: shape=(1, 512), dtype=int64, numpy=
array([[  101, 10373, 25933, 20974, 13006,  2980, 21397,  4012,  4813,
         2086,  4341,  6823,  2086, 20519,  4852, 10198,  3325,  6605,
         3375,  3934,  2312,  2235,  4094,  2565,  2622,  2968,  2203,
         2203,  3001,  2458,  2166, 23490,  2968,  2974,  7300,  6959,
         2974,  3136,  6032,  3325,  2551, 10317,  5338,  4044,  2832,
         8048,  3003,  2844,  4813,  6581,  4105, 11647,  2583,  6464,
         2958,  6578, 22859,  8754, 21466,  3893, 10266,  3798,  2136,
         2372,  5198,  3237,  3247, 11153,  3237,  2968,  2195,  3454,
         2764,  3266,  4800, 10521,  6895, 28296,  5649,  8185,  2098,
         5500,  2780,  9605,  2136,  2372,  2164,  6327, 17088,  6605,
         3674, 18402,  3934,  2780,  7453,  3144,  2650,  2501, 12771,
         3934,  9531,  2051,  5166,  3737,  4118, 20792,  6605,  6502,
         9871,  3325,  2086,  2551,  2976,  2231,  6401,  2780, 12746,
        16134,  6310,  2544, 16134,  6605,  3934,  5994, 28585,  2974,
         4526,  9084,  2449,  6194,  7142,  7620,  2306,  6605,  7781,
         2279,  4245,  4684,  5097, 11924, 16380,  5281, 19905, 25870,
         2500,  6605,  4736,  4114,  6581, 17826,  6970, 28823,  4807,
         4813,  3001,  2933,  3266, 11103,  2892,  8360,  3934,  2434,
         4145,  2345,  7375,  4208,  2968,  4073, 12725,  2536,  3450,
        14206,  4187,  4130,  3450,  4719,  3934,  5147,  2949,  2051,
         2306,  5166, 25276,  2152,  3737,  4781,  3113,  8013, 14206,
         3450,  8518,  2426,  2622,  2136,  2372,  4722,  7640,  6327,
         4411,  2734,  3113,  2622,  3289,  5676,  2622,  6503,  6134,
         6413,  4807,  4722,  6327, 22859,  4953, 14670,  9531,  3570,
         5166,  2622,  8503,  3450,  2164,  5337,  3550, 24162,  2622,
         9920,  5935,  8220,  4342,  2949, 12653,  2192,  2622,  8116,
         3085,  2015, 23946, 20271,  3530,  2588,  3314, 13248,  2967,
         2949,  2695,  7375,  3319,  6709,  2752,  7620, 10779,  2136,
         2372,  8013,  8048,  3921, 10908,  2836,  4806,  7411,  4781,
         2836,  9312,  2873,  5704,  2443,  7748,  2780, 12706,  5300,
         6481,  5326,  7142,  7620,  6078, 10471,  5906,  2109,  2622,
         2968,  3120,  3642,  2544,  2491, 16473, 10813,  3451,  2689,
         8678,  3488,  5461, 29494, 10787,  3029,  8651,  2366,  2195,
         3934,  5704,  2443, 25505,  2724,  2164,  3772,  3040, 10912,
         6605,  2051, 27554, 11376, 19795,  3145,  4391,  6567, 12725,
         3463,  4041,  2680,  7396, 10908,  2635,  2599,  3788,  4722,
        22859,  7846,  2622, 20283,  4187,  5246,  9990,  4415,  2396,
         2594, 10924,  4751, 11157, 10035,  8635,  2147,  7528, 29445,
         6194,  3266,  4684, 16380,  7375,  2081,  3154,  3266,  2048,
         2590,  2951, 25095,  3934,  2951,  2697,  4600,  2109,  3934,
        11506,  2367,  4127,  2951,  2066,  2865,  4684,  2951,  2449,
         3563,  2592,  2136,  2565,  2622, 10489,  3375,  3795,  3454,
         5884, 10843, 16316,  3454, 12139, 11100,  5225,  3144, 23259,
         6503,  3454,  6162,  2449,  3289,  2164,  8720,  5813, 10831,
         3314,  4254,  2565,  6959,  2195,  3454,  3934,  6401,  2976,
         4034,  2306,  1044,  7898,  2565,  3208,  2877,  3947,  7396,
         3674,  4411,  3266,  2832,  7620,  2967,  3271, 10938,  3151,
        29003, 16134,  3024, 11433,  6143,  3257,  8182,  2449,  5918,
         2864,  6578,  4106,  2203,  2203,  6653,  2346,  2863,  2361,
        24977,  4208,  2968,  4073, 12725,  2536,  3450, 14206,  4187,
         4130,  3450,  4719,  3934,  5147,  2949,  2051,  2306,  5166,
        25276,  2152,  3737,  4781,  3113,  8013, 14206,  3450,  8518,
         2426,  2622,  2136,  2372,  4722,  7640,  6327,  4411,  2734,
         3113,  2622,  3289,  5676,  2622,  6503,  6134,  2780,  3454,
         2458,  2190,  6078,  2109,  2622,  2968,  3120,  3642,  2544,
         2491, 16473, 10813,  3266,  9123,  3433,  3934,  4083, 19875,
         6364, 17953,  2361,  2109, 22969,  2376,  8013,   102]])>, 'attention_mask': <tf.Tensor: shape=(1, 512), dtype=int64, numpy=
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        1, 1, 1, 1, 1, 1]])>}
```

Comments: