The output is correct but has an error

InvalidArgumentError Traceback (most recent call last)
Cell In[33], line 2
1 # Test your function
----> 2 w1_unittest.test_line_to_tensor(line_to_tensor)

File /tf/, in test_line_to_tensor(target)
18 assert tf.math.reduce_all(tf.equal(ids, [3, 2, 3, 2, 3, 3, 3, 2])), f"Unit test 1 failed. "
20 line = “123”
—> 21 ids = target(line, vocab)
22 assert tf.is_tensor(ids), f"Wrong type, your function must return a Tensor"
23 assert len(ids) == len(line), f"Wrong length. Expected: {len(line)} but got {len(ids)}"

Cell In[31], line 22, in line_to_tensor(line, vocab)
19 chars = list(line)
21 # Map characters to their respective integer values using the lookup table
—> 22 ids = vocab_table(chars)
24 return ids

File /usr/local/lib/python3.8/dist-packages/keras/src/utils/, in filter_traceback..error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.traceback)
68 # To get the full stack trace, call:
69 # tf.debugging.disable_traceback_filtering()
—> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/ops/, in Assert(condition, data, summarize, name)
100 xs = ops.convert_n_to_tensor(data)
101 data_str = [_summarize_eager(x, summarize) for x in xs]
→ 102 raise errors.InvalidArgumentError(
103 node_def=None,
104 op=None,
105 message="Expected ‘%s’ to be true. Summarized data: s"
106 (condition, “\n”.join(data_str)))
107 return
109 with ops.name_scope(name, “Assert”, [condition, data]) as name:

InvalidArgumentError: Exception encountered when calling layer ‘string_lookup_22’ (type StringLookup).

Expected ‘tf.Tensor(False, shape=(), dtype=bool)’ to be true. Summarized data: b’When num_oov_indices=0 all inputs should be in vocabulary, found OOV values [“2” “3”], consider setting num_oov_indices=1.’

Call arguments received by layer ‘string_lookup_22’ (type StringLookup):
• inputs=[“‘1’”, “‘2’”, “‘3’”]

Hi @Masdarul_Rizqi

First, as you probably can see from the error:
“22 assert tf.is_tensor(ids), f"Wrong type, your function must return a Tensor”"

Hint, this exercise is pretty simple - just the same lines as in the cells in “1.3 - Convert a Line to Tensor” section. Or in particular ids = vocab_table(chars) is wrong.


Thanks for reporting the error @Masdarul_Rizqi .
Perhaps you have changed the argument num_oov_indices to 0 in your call to tf.keras.layers.StringLookup, something like this? :

ids = tf.keras.layers.StringLookup(vocabulary=list(vocab), mask_token=None, num_oov_indices=0)(chars)

To give you context to why setting num_oov_indices=0 is incorrect, I am quoting the following from documentation:

The number of out-of-vocabulary tokens to use. If this value is more than 1, OOV inputs are hashed to determine their OOV value. If this value is 0, OOV inputs will cause an error when calling the layer. Defaults to 1 .

Hope that helps.