C5W1A2 Wrong Values Error in Exercise 2

Hello,

I am getting an AssertionError: Wrong values. I have checked the calculations and the dimensions and I’m pretty sure that its all fine. The following is my output :

Sampling:
list of sampled indices:
 [23, 16, 26, 26, 19, 25, 22, 16, 7, 11, 17, 26, 23, 23, 23, 18, 10, 6, 12, 2, 14, 24, 14, 17, 7, 18, 0]
list of sampled characters:
 ['w', 'p', 'z', 'z', 's', 'y', 'v', 'p', 'g', 'k', 'q', 'z', 'w', 'w', 'w', 'r', 'j', 'f', 'l', 'b', 'n', 'x', 'n', 'q', 'g', 'r', '\n']
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-179-a13d660e654a> in <module>
     19     print("\033[92mAll tests passed!")
     20 
---> 21 sample_test(sample)

<ipython-input-179-a13d660e654a> in sample_test(target)
     15     assert indices[-1] == char_to_ix['\n'], "All samples must end with \\n"
     16     assert min(indices) >= 0 and max(indices) < len(char_to_ix), f"Sampled indexes must be between 0 and len(char_to_ix)={len(char_to_ix)}"
---> 17     assert np.allclose(indices[0:6], [23, 16, 26, 26, 24, 3]), "Wrong values"
     18 
     19     print("\033[92mAll tests passed!")

AssertionError: Wrong values

Any help is deeply appreciated

Can you post the body of def sample(parameters, char_to_ix, seed) here?

Sure here it is :

# Retrieve parameters and relevant shapes from "parameters" dictionary
    Waa, Wax, Wya, by, b = parameters['Waa'], parameters['Wax'], parameters['Wya'], parameters['by'], parameters['b']
    vocab_size = by.shape[0]
    n_a = Waa.shape[1]
    print()
    
    
    ### START CODE HERE ###
    # Step 1: Create the a zero vector x that can be used as the one-hot vector 
    # Representing the first character (initializing the sequence generation). (≈1 line)
    x = np.zeros((vocab_size,n_a))
    # Step 1': Initialize a_prev as zeros (≈1 line)
    a_prev = np.zeros((n_a, n_a))
    
    # Create an empty list of indices. This is the list which will contain the list of indices of the characters to generate (≈1 line)
    indices = []
    
    # idx is the index of the one-hot vector x that is set to 1
    # All other positions in x are zero.
    # Initialize idx to -1
    idx = -1
    
    # Loop over time-steps t. At each time-step:
    # Sample a character from a probability distribution 
    # And append its index (`idx`) to the list "indices". 
    # You'll stop if you reach 50 characters 
    # (which should be very unlikely with a well-trained model).
    # Setting the maximum number of characters helps with debugging and prevents infinite loops. 
    counter = 0
    newline_character = char_to_ix['\n']
    
    while (idx != newline_character and counter != 50):
        
        # Step 2: Forward propagate x using the equations (1), (2) and (3)
        a = np.tanh(np.dot(Wax, x) + np.dot(Waa, a_prev) + b)
        z = np.dot(Wya, a) + by
        y = softmax(z)
        # For grading purposes
        np.random.seed(counter + seed) 
        
        # Step 3: Sample the index of a character within the vocabulary from the probability distribution y
        # (see additional hints above)
        idx = np.random.choice(range(27), p=y[:,idx].ravel())

        # Append the index to "indices"
        indices.append(idx)
        
        # Step 4: Overwrite the input x with one that corresponds to the sampled index `idx`.
        # (see additional hints above)
        x = np.zeros((vocab_size,n_a))
        x[idx] = y[idx]
        # Update "a_prev" to be "a"
        a_prev = a
        
        # for grading purposes
        seed += 1
        counter +=1

    ### END CODE HERE ###

    if (counter == 50):
        indices.append(char_to_ix['\n'])
    
    return indices

Some thoughts:

You need to use column vectors, not matrices here.

also looks wrong. Why are you using idx here? also don’t hardcode 27.

Sorry for the late reply. Thanks a lot , I’ve been struggling with it for a couple of hours now because of the dimensions of x and a_prev.

1 Like