NLP C3W2 Assignment - Error in unit tests for data_generator function

Having obtained the “expected output” for this function, I get the following error in the unit tests:

data_generator error

It seems to me the error is in the unit test code, where it is apparently requesting the shape of a list object and not of an array.

You are supposed to return a tuple of 3 numpy arrays.

See this comment in the function data_generator
# convert the batch (data type list) to a numpy array

…which is what I’m doing, no?

image

Please click my name and message your notebook as an attachment.

I will do that. Many thanks.

Not sure how to attach my notebook to the message…

Looked at the notebook. There was an extra -1 term when computing the number of number of elements for pad inside the function data_generator

@Community-Team @Mubsi @paulinpaloalto
There are bugs in w2_unittest.py inside test_data_generator.
failed_cases is a list. So, instead of invoking it like a function, use failed_tests.append

Got it.

Many thanks for your help!

Thanks @balaji.ambresh, noted.

@Mubsi @balaji.ambresh I am also having problems in the unit_tests: I get 24/6 tests passed. Initially I get:

(DeviceArray([[49, 50, 51, 52, 53, 54, 55, 56, 57, 1],
[50, 51, 52, 53, 54, 55, 56, 57, 48, 1]], dtype=int32),
DeviceArray([[49, 50, 51, 52, 53, 54, 55, 56, 57, 1],
[50, 51, 52, 53, 54, 55, 56, 57, 48, 1]], dtype=int32),
DeviceArray([1, 1], dtype=int32))

Expected output

(DeviceArray([[49, 50, 51, 52, 53, 54, 55, 56, 57, 1],
[50, 51, 52, 53, 54, 55, 56, 57, 48, 1]], dtype=int32),
DeviceArray([[49, 50, 51, 52, 53, 54, 55, 56, 57, 1],
[50, 51, 52, 53, 54, 55, 56, 57, 48, 1]], dtype=int32),
DeviceArray([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32))

Element with index 2 in the output tuple has incorrect shape. It should be (batch_size, max_length).
Expected (2, 10).
Got (2,).

I am assuming element with index 2 is the mask_np_arr?
I use np.where() to create the example_mask

My lab id is phpssgbp

Thanks,
Drew

@drew_Frances Please click my name and message your notebook as an attachment.

@drew_Frances Computation of example_mask is incorrect.
Please be aware of the parameter type when using np.where. Your implementation would be correct if the type of tensor_pad was a numpy array. Unfortunately, it’s a list. As a result, instead of vectoring the operation, it’s just a simple comparison. Since the list is not equal to 0, you’ll get 1 as the result.

1 Like

Hi Balaji.ambresh:

Thanks for the answer. That fixed it. This explains why my test code works: I was using np.array.

I seem to be making this mistake a lot. Is there a way that I would run the code locally and have some tool pick up these type errors? Does numpy use type hints?

Again, thanks for the help!

Cheers,
Drew

Try these steps to run a notebook locally:

  1. Download the assignment by clicking Lab Files and then downloading all files.
  2. Install the correct version of libraries. Run !pip list > libs.txt on the coursera jupyter environment to get this information.

See numpy.typing for numpy type hints.

@balaji.ambresh:

Thanks for the answer!

Cheers,
Drew