Week2-Assigment2

Hi Guys,

I am stuck in the model call. I understand that it is against policy to paste code, so I will paste the error response instead:

j = 0 idx = 0
single_example = turiasaurus
single_example_chars [‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
X = [[20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]]
Y = [[20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19], 0]


TypeError Traceback (most recent call last)
in
----> 1 parameters, last_name = model(data.split(“\n”), ix_to_char, char_to_ix, 22001, verbose = True)
2
3 assert last_name == ‘Trodonosaurus\n’, “Wrong expected output”
4 print(“\033[92mAll tests passed!”)

in model(data_x, ix_to_char, char_to_ix, num_iterations, n_a, dino_names, vocab_size, verbose)
85 if j % 2000 == 0:
86
—> 87 print('Iteration: %d, Loss: f' (j, loss) + ‘\n’)
88
89 # The number of dinosaur names to print

TypeError: only size-1 arrays can be converted to Python scalars

Appreciated the help.

thanks
Nazih

loss should be a scalar i.e. a floating point number.
j should be an int since it’s a loop counter

Please use type(loss) to check if loss is numpy.float64

Thanks @balaji.ambresh for the quick reply.

I made some changes to the code to fix X type.
The output now looks as follows and seems to be stuck at the first iteration:
It seems it is taking a long time to execute the rest.

j = 0 idx = 0
single_example = turiasaurus
single_example_chars [‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
X = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
Y = [21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 20, 0]

Iteration: 0, Loss: 23.084041

Nkzxwtdmfqoeyhsqwasjkjvu
Kneb
Kzxwtdmfqoeyhsqwasjkjvu
Neb
Zxwtdmfqoeyhsqwasjkjvu
Eb
Xwtdmfqoeyhsqwasjkjvu

Here are the steps taken:

Set the index idx

    idx = this is remainder of division of j over len(examples)

From the output you can tell that the parsing is correct.

This is how I am setting Y to be:
Y = X[skip the first entry] + X[index the first entry] + add the new line

1 Like

Hi @nfattal ,

To create the list of input characters X, None value should be prepend to X because RNN uses None as flag to set the input vector as zero vector.

To construct the Y vector, just skipping the first char in X, which is None value, and copy across all the elements in X and add a newline at the end.

It looks like your code didn’t prepend the None value to X, so in effort it has removed the real first char in X.

Please refer to the implementation instruction and check your code’s logic.

Thanks @Kic

I think I initially misunderstood what was meant by None.
I made the modifications; however, still not there yet.
The auto grader is complaining about the model function.

I get the following output now:
j = 0 idx = 0
single_example = turiasaurus
single_example_chars [‘t’, ‘u’, ‘r’, ‘i’, ‘a’, ‘s’, ‘a’, ‘u’, ‘r’, ‘u’, ‘s’]
single_example_ix [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
X = [None, 20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19]
Y = [20, 21, 18, 9, 1, 19, 1, 21, 18, 21, 19, 0]

Iteration: 0, Loss: 23.087336

Nkzxwtdmfqoeyhsqwasjkjvu
Kneb
Kzxwtdmfqoeyhsqwasjkjvu
Neb
Zxwtdmfqoeyhsqwasjkjvu
Eb
Xwtdmfqoeyhsqwasjkjvu

Hi @nfattal ,

The output posted here is good. What errors are generated by the grader?

It’s complaining about the model function:

Code Cell UNQ_C1: Function ‘clip’ is correct.
Code Cell UNQ_C2: Function ‘sample’ is correct.
Code Cell UNQ_C3: Function ‘optimize’ is correct.

**Code Cell UNQ_C4: Function ‘model’ is incorrect. Check implementation. **
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

1 Like

Hi @nfattal ,

Download your lab in ipynb format and attach to a direct message to me. I will have a look for you.

Hi @Kic

Thank you for your help with this.
It’s appreciated.

Regards
Nazih

Hi @nfattal ,

First of all, please edit your post and remove the assignment file as publicly posting answer/code in the forum is against the honour code policy.

In regards to the code, there are two places need attention:

  1. The construction of input chars for the X vector - there is no need to append X at the end, because we are constructing the string of chars for one example at a time.
  2. No learning rate is used in your code. Please see the comment lines above, it clearly stated that learning rate of 0.01 should be used when calling the optimize() function.

Hi @Kic,

First, please excuse the public posting of the notebook. I removed it.
For point 1, I removed the trailing X. Clearly a mistake.

For point 2, noted. The parameter learning_rate is optional to be sent as the function definition hard codes it in the declaration:

def optimize(X, Y, a_prev, parameters, learning_rate = 0.01):

Removing the trailing X solved the issue.
The code runs correctly now.

Thank you for your support.
Appreciated.

Regards
Nazih

Hi @nfattal ,

Great to hear the code is now working.

In regards to the learning_rate set in the definition, that is the default option meaning when calling the function, you can pass a different value as required. In this case, the both the default and the passing in value are the same.

Noted and thanks for your help.