Planar_data_classification_with_one_hidden_layer Shape error

Greetings,
I am getting an error please guide me through this.
This is the code I used -

GRADED FUNCTION: nn_model

def nn_model(X, Y, n_h, num_iterations = 10000, print_cost=False):
“”"
Arguments:
X – dataset of shape (2, number of examples)
Y – labels of shape (1, number of examples)
n_h – size of the hidden layer
num_iterations – Number of iterations in gradient descent loop
print_cost – if True, print the cost every 1000 iterations

Returns:
parameters -- parameters learnt by the model. They can then be used to predict.
"""

np.random.seed(3)
n_x = layer_sizes(X, Y)[0]
n_y = layer_sizes(X, Y)[2]

# Initialize parameters
#(≈ 1 line of code)
# parameters = ...
# YOUR CODE STARTS HERE
parameters = initialize_parameters(n_x, n_h, n_y)


# YOUR CODE ENDS HERE

# Loop (gradient descent)

for i in range(0, num_iterations):
     
    #(≈ 4 lines of code)
    # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
    # A2, cache = ...
    
    # Cost function. Inputs: "A2, Y". Outputs: "cost".
    # cost = ...

    # Backpropagation. Inputs: "parameters, cache, X, Y". Outputs: "grads".
    # grads = ...

    # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters".
    # parameters = ...
    
    # YOUR CODE STARTS HERE

    A2, cache = forward_propagation(X, parameters)
    
    cost = compute_cost(A2, Y)
    
    grads = backward_propagation(parameters, cache, X, Y)
    
    parameters = update_parameters(parameters, grads)
    
    # YOUR CODE ENDS HERE
    
    # Print the cost every 1000 iterations
    if print_cost and i % 1000 == 0:
        print ("Cost after iteration %i: %f" %(i, cost))

return parameters

#second code

t_X, t_Y = nn_model_test_case()
parameters = nn_model(t_X, t_Y, 4, num_iterations=10000, print_cost=True)

print("W1 = " + str(parameters[“W1”]))
print("b1 = " + str(parameters[“b1”]))
print("W2 = " + str(parameters[“W2”]))
print("b2 = " + str(parameters[“b2”]))

nn_model_test(nn_model)

Error

Hi,

I think the shape of W1 is incorrect. Maybe you could trace how you’re calculating it i.e. check its shape prior calling forward_propagation and inside the forward_propagation function itself.

Kind regards,
Alberto

Tried checking it but I am not able to find where I am getting wrong. Please guide me through it

GRADED FUNCTION:forward_propagation

def forward_propagation(X, parameters):
“”"
Argument:
X – input data of size (n_x, m)
parameters – python dictionary containing your parameters (output of initialization function)

Returns:
A2 -- The sigmoid output of the second activation
cache -- a dictionary containing "Z1", "A1", "Z2" and "A2"
"""
# Retrieve each parameter from the dictionary "parameters"
#(≈ 4 lines of code)
# W1 = ...
# b1 = ...
# W2 = ...
# b2 = ...
# YOUR CODE STARTS HERE
W1 = parameters.get('W1')
b1 = parameters.get('b1')
W2 = parameters.get('W2')
b2 = parameters.get('b2')
# YOUR CODE ENDS HERE

# Implement Forward Propagation to calculate A2 (probabilities)
# (≈ 4 lines of code)
# Z1 = ...
# A1 = ...
# Z2 = ...
# A2 = ...
# YOUR CODE STARTS HERE
Z1 = np.dot(W1,X) + b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2,A1) + b2
A2 = sigmoid(Z2)

# YOUR CODE ENDS HERE

assert(A2.shape == (1, X.shape[1]))

cache = {"Z1": Z1,
         "A1": A1,
         "Z2": Z2,
         "A2": A2}
return A2, cache

But what are the shapes? Did you print them out?

You could add a of print out before the line A2, cache = forward_propagation(X, parameters), something like print(X.shape, parameters['W1'].shape). And then inside the forward_propagation function right before calculating Z1 (where you are having the error) you could print again W1 and X shape.