C5W2A2 - Emoji_v3a - cost error

I am using the following formula for calculating cost in the model:
cost += -np.multiply(Y_oh[i], np.log(a[i]))

Is there something wrong here? I get “Incorrect implementation of cost”; my error stack is below.

IndexError Traceback (most recent call last)
in
28 print(“\033[92mAll tests passed!”)
29
—> 30 model_test(model)

in model_test(target)
20
21 np.random.seed(10)
—> 22 pred, W, b = model(X, Y, word_to_vec_map, 0.0025, 110)
23
24 assert W.shape == (2, 2), “W must be of shape 2 x 2”

in model(X, Y, word_to_vec_map, learning_rate, num_iterations)
54 # print(a)
55 # Add the cost using the i’th training label’s one hot representation and “A” (the output of the softmax)
—> 56 cost += -np.multiply(Y_oh[i], np.log(a[i]))
57 ### END CODE HERE ###
58

IndexError: index 2 is out of bounds for axis 0 with size 2

Any help is appreciated - thank you!

Hey @Kavya_Prasad,
Note that Y has a dimensionality of (m, 1), and Y_oh has a dimensionality of (m, n_y). Now, in the for-loop, we are iterating over range(m), i.e., the number of samples, but a has a dimensionality of (n_y,), i.e., the number of classes. That’s why the error.

Moreover, you don’t have to index a. Y_oh[i] is a vector and a is also a vector, and since Y_oh[i] only contains a single one and the rest are zeroes, hence, the rest of the products will be 0 by themselves. Let us know if this helps.

Cheers,
Elemento

Thanks for the tip! Unfortunately I’m still facing errors, and I’m confused because I’ve gone over my code multiple times and cannot see what is wrong with it. Here’s what I’m using:

        {moderator edit: code removed}

This is my error:


AssertionError Traceback (most recent call last)
in
28 print(“\033[92mAll tests passed!”)
29
—> 30 model_test(model)

in model_test(target)
20
21 np.random.seed(10)
—> 22 pred, W, b = model(X, Y, word_to_vec_map, 0.0025, 110)
23
24 assert W.shape == (2, 2), “W must be of shape 2 x 2”

in model(X, Y, word_to_vec_map, learning_rate, num_iterations)
64 b = b - learning_rate * db
65
—> 66 assert type(cost) == np.float64, “Incorrect implementation of cost”
67 assert cost.shape == (), “Incorrect implementation of cost”
68

AssertionError: Incorrect implementation of cost

What can I try changing?

Hey @Kavya_Prasad,
The error still resides in your implementation of cost. Try to print what you are adding in the cost variable, and then you can cross-reference it against what should be added (as given in the markdown). Note that np.multiply() only performs element-wise multiplication. Additionally, note that the formulation contains a negative sign, in order to make the values (which are added in the cost) positive. But there is no negative sign in your code, where do you think that should be put? Let us know if these hints help you out.

Cheers,
Elemento

cost is a scalar.
np.multiply() does not give a scalar result.

Please do not post your code on the forum, that breaks the course Honor Code. I have edited your messages to remove the code.

Thanks very much - fixed the problem!