Getting an error in C3W1, Deep N grams in batch dataset generator

Issue seen in :Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera

Error:---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[47], line 3
1 # test your function
2 tf.random.set_seed(1)
----> 3 dataset = create_batch_dataset(train_lines[1:100], vocab, seq_length=16, batch_size=2)
5 print("Prints the elements into a single batch. The batch contains 2 elements: ")
7 for input_example, target_example in dataset.take(1):

Cell In[46], line 33, in create_batch_dataset(lines, vocab, seq_length, batch_size)
31 data_generator = ids_dataset.batch(seq_length + 1, drop_remainder=True)
32 # Map each input sample using the split_input_target function
—> 33 dataset_xy = data_generator.map(split_input_target(single_line_data))
35 # Assemble the final dataset with shuffling, batching, and prefetching
36 dataset = (
37 dataset_xy
38 .shuffle(BUFFER_SIZE)
39 .batch(batch_size, drop_remainder=True)
40 .prefetch(tf.data.experimental.AUTOTUNE)
41 )

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/dataset_ops.py:2278, in DatasetV2.map(self, map_func, num_parallel_calls, deterministic, name)
2274 # Loaded lazily due to a circular dependency (dataset_ops → map_op →
2275 # dataset_ops).
2276 # pylint: disable=g-import-not-at-top,protected-access
2277 from tensorflow.python.data.ops import map_op
→ 2278 return map_op._map_v2(
2279 self,
2280 map_func,
2281 num_parallel_calls=num_parallel_calls,
2282 deterministic=deterministic,
2283 name=name)

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/map_op.py:37, in _map_v2(input_dataset, map_func, num_parallel_calls, deterministic, name)
34 if deterministic is not None and not debug_mode.DEBUG_MODE:
35 warnings.warn("The deterministic argument has no effect unless the "
36 “num_parallel_calls argument is specified.”)
—> 37 return _MapDataset(
38 input_dataset, map_func, preserve_cardinality=True, name=name)
39 else:
40 return _ParallelMapDataset(
41 input_dataset,
42 map_func,
(…)
45 preserve_cardinality=True,
46 name=name)

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/map_op.py:107, in _MapDataset.init(self, input_dataset, map_func, use_inter_op_parallelism, preserve_cardinality, use_legacy_function, name)
105 self._use_inter_op_parallelism = use_inter_op_parallelism
106 self._preserve_cardinality = preserve_cardinality
→ 107 self._map_func = structured_function.StructuredFunctionWrapper(
108 map_func,
109 self._transformation_name(),
110 dataset=input_dataset,
111 use_legacy_function=use_legacy_function)
112 self._name = name
113 variant_tensor = gen_dataset_ops.map_dataset(
114 input_dataset._variant_tensor, # pylint: disable=protected-access
115 self._map_func.function.captured_inputs,
(…)
118 preserve_cardinality=self._preserve_cardinality,
119 **self._common_args)

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/data/ops/structured_function.py:154, in StructuredFunctionWrapper.init(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
147 defun_kwargs = {}
149 readable_transformation_name = transformation_name.replace(
150 “.”, “")[:-2] if len(transformation_name) > 2 else “”
152 func_name = "
”.join(
153 [readable_transformation_name,
→ 154 function_utils.get_func_name(func)])
155 # Sanitize function name to remove symbols that interfere with graph
156 # construction.
157 for symbol in [“<”, “>”, “\”, “'”, " "]:

File /usr/local/lib/python3.8/dist-packages/tensorflow/python/util/function_utils.py:97, in get_func_name(func)
95 return str(type(func))
96 else:
—> 97 raise ValueError(
98 ‘Argument func must be a callable. ’
99 f’Received func={func} (of type {type(func)})’)

I know I am making some error with the input passed…Any pointers…

The issue lies here, your error is stating you tried use a function incorrectly with type(function), where as it is only looking for function recall to the data_generator.map

Deepti, Can you pls elaborate? I am not sure I follow this

A more direct answer would be You do not require single_line_data in the recalled code function

Hi Deepti,

As per the suggestion, when I changed the code, it is listing all the information as follows:
Prints the elements into a single batch. The batch contains 2 elements:

Input0 : [b"That season’d woe had pelleted in tears,"
b’And often reading what contents it bears;’
b"As often shrieking undistinguish’d woe,"
b’In clamours of all size, both high and low.’
b"Sometimes her levell’d eyes their carriage ride,"
b’As they did battery to the spheres intend;’
b’Sometime diverted their poor balls are tied’
b’To the orbed earth; sometimes they do extend’
b’Their view right on; anon their gazes lend’
b"To every place at once, and, nowhere fix’d,"
b"The mind and sight distractedly commix’d."
b’Her hair, nor loose nor tied in formal plat,’
b"Proclaim’d in her a careless hand of pride"
b"For some, untuck’d, descended her sheaved hat,"
b’Hanging her pale and pined cheek beside;’
b’Some in her threaden fillet still did bide,']

Target0 : [b’And often reading what contents it bears;’
b"As often shrieking undistinguish’d woe,"
b’In clamours of all size, both high and low.’
b"Sometimes her levell’d eyes their carriage ride,"
b’As they did battery to the spheres intend;’
b’Sometime diverted their poor balls are tied’
b’To the orbed earth; sometimes they do extend’
b’Their view right on; anon their gazes lend’
b"To every place at once, and, nowhere fix’d,"
b"The mind and sight distractedly commix’d."
b’Her hair, nor loose nor tied in formal plat,’
b"Proclaim’d in her a careless hand of pride"
b"For some, untuck’d, descended her sheaved hat,"
b’Hanging her pale and pined cheek beside;’
b’Some in her threaden fillet still did bide,’
b’And true to bondage would not break from thence,']

Input1 : [b’Though slackly braided in loose negligence.’
b’A thousand favours from a maund she drew’
b’Of amber, crystal, and of beaded jet,’
b’Which one by one she in a river threw,’
b’Upon whose weeping margent she was set;’
b’Like usury, applying wet to wet,’
b"Or monarch’s hands that let not bounty fall"
b’Where want cries some, but where excess begs all.’
b’Of folded schedules had she many a one,’
b"Which she perused, sigh’d, tore, and gave the flood;"
b"Crack’d many a ring of posied gold and bone"
b’Bidding them find their sepulchres in mud;’
b"Found yet moe letters sadly penn’d in blood,"
b’With sleided silk feat and affectedly’
b"Enswathed, and seal’d to curious secrecy."
b’These often bathed she in her fluxive eyes,']

Target1 : [b’A thousand favours from a maund she drew’
b’Of amber, crystal, and of beaded jet,’
b’Which one by one she in a river threw,’
b’Upon whose weeping margent she was set;’
b’Like usury, applying wet to wet,’
b"Or monarch’s hands that let not bounty fall"
b’Where want cries some, but where excess begs all.’
b’Of folded schedules had she many a one,’
b"Which she perused, sigh’d, tore, and gave the flood;"
b"Crack’d many a ring of posied gold and bone"
b’Bidding them find their sepulchres in mud;’
b"Found yet moe letters sadly penn’d in blood,"
b’With sleided silk feat and affectedly’
b"Enswathed, and seal’d to curious secrecy."
b’These often bathed she in her fluxive eyes,’
b"And often kiss’d, and often 'gan to tear:"]

I feel there is some error on the subscripts part. Could you pls guide, I am unable to follow the instructions

there could be probably be more issue in your codes.

Also seeing the output it doesn’t seem to be from the code cell you shared issue with. So kindly mention which grade cell got you the output.

Do not post any grade cell codes here.

Regards
DP

Hi @Deepti Prasad, I think I have made a mistake using the split_input_target cell.

  • Make the dataset produce batches of data that will form a single sample each time. This is, make the dataset produce a sequence of seq_length + 1, rather than single numbers at each time. You can do it using the batch function of the already created dataset. You must specify the length of the produced sequences (seq_length + 1). So, the sequence length produced by the dataset will seq_length + 1. It must have that extra element since you will get the input and the output sequences out of the same element. drop_remainder=True will drop the sequences that do not have the required length. This could happen each time that the dataset reaches the end of the input sequence.
    – > I have used the code as is.

  • Use the split_input_target to split each element produced by the dataset into the mentioned input and output sequences.The input will have the first seq_length elements, and the output will have the last seq_length. So, after this step, the dataset generator will produce batches of pairs (input, output) sequences.
    As discussed above the split_input_target is nt used as a function call. So this cell is passing without errors.

Upon execution of cell 40, this is the output that I am getting
Prints the elements into a single batch. The batch contains 2 elements:

Input0 : [b"That season’d woe had pelleted in tears,"
b’And often reading what contents it bears;’
b"As often shrieking undistinguish’d woe,"
b’In clamours of all size, both high and low.’
b"Sometimes her levell’d eyes their carriage ride,"
b’As they did battery to the spheres intend;’
b’Sometime diverted their poor balls are tied’
b’To the orbed earth; sometimes they do extend’
b’Their view right on; anon their gazes lend’
b"To every place at once, and, nowhere fix’d,"
b"The mind and sight distractedly commix’d."
b’Her hair, nor loose nor tied in formal plat,’
b"Proclaim’d in her a careless hand of pride"
b"For some, untuck’d, descended her sheaved hat,"
b’Hanging her pale and pined cheek beside;’
b’Some in her threaden fillet still did bide,']

Target0 : [b’And often reading what contents it bears;’
b"As often shrieking undistinguish’d woe,"
b’In clamours of all size, both high and low.’
b"Sometimes her levell’d eyes their carriage ride,"
b’As they did battery to the spheres intend;’
b’Sometime diverted their poor balls are tied’
b’To the orbed earth; sometimes they do extend’
b’Their view right on; anon their gazes lend’
b"To every place at once, and, nowhere fix’d,"
b"The mind and sight distractedly commix’d."
b’Her hair, nor loose nor tied in formal plat,’
b"Proclaim’d in her a careless hand of pride"
b"For some, untuck’d, descended her sheaved hat,"
b’Hanging her pale and pined cheek beside;’
b’Some in her threaden fillet still did bide,’
b’And true to bondage would not break from thence,']

Input1 : [b’Though slackly braided in loose negligence.’
b’A thousand favours from a maund she drew’
b’Of amber, crystal, and of beaded jet,’
b’Which one by one she in a river threw,’
b’Upon whose weeping margent she was set;’
b’Like usury, applying wet to wet,’
b"Or monarch’s hands that let not bounty fall"
b’Where want cries some, but where excess begs all.’
b’Of folded schedules had she many a one,’
b"Which she perused, sigh’d, tore, and gave the flood;"
b"Crack’d many a ring of posied gold and bone"
b’Bidding them find their sepulchres in mud;’
b"Found yet moe letters sadly penn’d in blood,"
b’With sleided silk feat and affectedly’
b"Enswathed, and seal’d to curious secrecy."
b’These often bathed she in her fluxive eyes,']

Target1 : [b’A thousand favours from a maund she drew’
b’Of amber, crystal, and of beaded jet,’
b’Which one by one she in a river threw,’
b’Upon whose weeping margent she was set;’
b’Like usury, applying wet to wet,’
b"Or monarch’s hands that let not bounty fall"
b’Where want cries some, but where excess begs all.’
b’Of folded schedules had she many a one,’
b"Which she perused, sigh’d, tore, and gave the flood;"
b"Crack’d many a ring of posied gold and bone"
b’Bidding them find their sepulchres in mud;’
b"Found yet moe letters sadly penn’d in blood,"
b’With sleided silk feat and affectedly’
b"Enswathed, and seal’d to curious secrecy."
b’These often bathed she in her fluxive eyes,’
b"And often kiss’d, and often 'gan to tear:"]

The expected output is: Expected output

Prints the elements into a single batch. The batch contains 2 elements: 

Input0  : b'and sight distra'

Target0 : b'nd sight distrac'


Input1  : b'when in his fair'

Target1 : b'hen in his fair '

After this, when cell 41 is executed, this is the assertion error that is getting displayed:

AssertionError Traceback (most recent call last)
Cell In[42], line 2
1 # UNIT TEST
----> 2 w1_unittest.test_create_batch_dataset(create_batch_dataset)

File /tf/w1_unittest.py:55, in test_create_batch_dataset(target)
53 exp_shape = (BATCH_SIZE, SEQ_LENGTH)
54 outputs = dataset.take(1)
—> 55 assert len(outputs) > 0, f"Wrong length. First batch must have 1 element. Got {len(outputs)}"
56 for in_line, out_line in dataset.take(1):
57 assert tf.is_tensor(in_line), “Wrong type. in_line extected to be a Tensor”

AssertionError: Wrong length. First batch must have 1 element. Got 0

hi @nithyau_ramkumar

can you DM me your codes. Please click on my name and then message.

Regards
DP

Incorrect data code usage while converting into tensor caused your error, use the assigned single_line_data for conversion of data into tensor.
regards
DP

Thanks @Deepti_Prasad for the clarification.

There was an oversight w.r.t the tensor creation line on my part. I had given wrong input instead of using the single_line_data as input. Once this issue was fixed, I got the right output.

1 Like