W4_A2_Two_layer_Neural Network_Wrong output

FOR:

4 - Two-layer Neural Network

Exercise 1 - two_layer_model

def two_layer_model(. . .)
. . .

The assignment seems relatively straight forward:

  • We are using “data” and “functions” provided by the exercise,
  • I checked, re-checked, and just can’t see what I am doing wrong but…
  • keep getting:

Cost after iteration 1: 0.6564026188409187
Cost after first iteration: 0.6950464961800915
Cost after iteration 1: 0.7239781229671559
Cost after iteration 1: 0.7239781229671559
Cost after iteration 1: 0.7239781229671559
Error: Wrong output for variable W1.
Error: Wrong output for variable b1.
Error: Wrong output for variable W2.
Error: Wrong output for variable b2.
Error: Wrong output for variable 0.
2 Tests passed
1 Tests failed
. . .
. . .
AssertionError: Not all tests were passed for two_layer_model. Check your equations and avoid using global variables inside the function.

Seems very similar to:

But, replacing layers_dim with the three dimensions doesn’t help…

I think I need help…

Hi @dvnicolasdh,
Would it be possible for you to give more data without revealing your solution too much? Currently, it is very hard to understand why you get these errors, at least from the outputs your provided

Hello @yanivh ,

Without revealing my solution… the closest thing would be to say that I almost literally copied the sample lines of “The functions and their inputs are:” from the cell “Use the helper functions”


4 - Two-layer Neural Network

Exercise 1 - two_layer_model

Use the helper functions you have implemented in the previous assignment to build a 2-layer neural network with the following structure: LINEAR → RELU → LINEAR → SIGMOID. The functions and their inputs are:

def initialize_parameters(n_x, n_h, n_y):
    ...
    return parameters 
def linear_activation_forward(A_prev, W, b, activation):
    ...
    return A, cache
def compute_cost(AL, Y):
    ...
    return cost
def linear_activation_backward(dA, cache, activation):
    ...
    return dA_prev, dW, db
def update_parameters(parameters, grads, learning_rate):
    ...
    return parameters

but obviously replacing the AL with the appropriate variable, and assigning the proper “activation” according to the corresponding layers…

Note that the provided “initialize_parameters” function doesn’t accept the three dimensions as individual parameters, but seems to expect an array(list)…hence I used layers_dim

That said, training my implementation of this “two_layer_model” on the next cell:

4.1 - Train the model

parameters, costs = two_layer_model(. . . )

gave:

Cost after iteration 0: 0.6950464961800915
Cost after iteration 100: 0.5892596054583805
Cost after iteration 200: 0.5232609173622991
. . .
. . .
Cost after iteration 2300: 0.028387859212946117
Cost after iteration 2400: 0.026615212372776077
Cost after iteration 2499: 0.024821292218353375

Not the “expected” results, but similar ones…

I’ve repeatedly re-checked these lines:

   # Initialize parameters dictionary
   # parameters = ...
   # YOUR CODE STARTS HERE
   . . .
   # YOUR CODE ENDS HERE
        # Forward propagation:
        # A1, cache1 = ...
        # A2, cache2 = ...
        # YOUR CODE STARTS HERE
        . . .
        # YOUR CODE ENDS HERE
        # Compute cost
        # cost = ...
        # YOUR CODE STARTS HERE
        . . .
        # YOUR CODE ENDS HERE
        # Backward propagation.
        # dA1, dW2, db2 = ...
        # dA0, dW1, db1 = ...
        # YOUR CODE STARTS HERE
        . . .
        # YOUR CODE ENDS HERE
   # Update parameters.
   # parameters = ...
   # YOUR CODE STARTS HERE
   . . .
   # YOUR CODE ENDS HERE

and don’t see my error(s)…

Note that the problem happens upon executing “two_layer_model_test” evaluation cell:

parameters, costs = two_layer_model(train_x, train_y, layers_dims = (n_x, n_h, n_y), num_iterations = 2, print_cost=False)

print("Cost after first iteration: " + str(costs[0]))

two_layer_model_test(two_layer_model)

I get “exactly” the same “error” messages as image|690x437
presented by @tfu on post " Week 4 exercise 4 not passing both tests" but @tfu solution

made it work by replacing layers_dim with the three dimensions

doesn’t work for me…

I feel that any further than this would be revealing my implemented solution…

Hello

Found my problem…
Sorry @yanivh, it was quasi impossible for you to see my error, without me showing my attemtped solutions…

The exact same issue was identified by “Parsa Chadermazi” in the “Coursera Discussion” title “Two layer model”: https://www.coursera.org/learn/neural-networks-deep-learning/discussions/all/threads/4JXms7f2Eeuw1Q42CpJ9yQ

I believe that after too many scrolling up and down, I ended-up following the “initialization” instructions for the L_layer_model instead of the two_layer_model.
Mistakenly using the initialize_parameters_deep initialization function for the two_layer_model caused the problem. Using initialize_parameters solved the issue, giving the correct output for the two_layer_model_test.

1 Like

Help, I have this exact same error, and I’ve tried everything…
I’m using initialize_parameters(n_x , , ) etc…
I don’t see where I’m going wrong

Okay, i had the “activations” wrong…
sheesh.

Hi! I was getting a similar error related to wrong outputs and tracing the issue I found the next problem with the cost computation where I would appreciate some help in finding a possible cause.
I have included some ‘print’ statement and the A2 and Y inputs seem in good shape for the first cost computation but I am getting a ‘nan’ return instead (and the following iterations). I tried to generate a separate code just for calculating the cost, even generating a new compute_cost(AL, Y) function from my previous assignment to discard any problem with the given function, but I still getting a nan output. Thank you in advance
image

solved! I was not passing the 'relu' or 'sigmoid' activation so I was getting wrong values for A2 that through me nan. Once I fixed the linear_activation_backward() call everything was fixed. I discovered by observing strange values in the A2 matrix and checking if having ‘nan’ in np.log(A2)) and np.log(1-A2)). I solved first by calling the L_model_forward() which add internally the activation function but need to admit that I was just fumbling around a bit. Thanks

Thank you. You saved my life!!!