C1W3 assignment - dimensions of n_x, n_h, n_y not matching

Hi,

When creating the n_x, n_h, and n_y sizes, it is wrong vs what its expected. But i dont see where is wrong, Can you help?

My code:
n_x = shape_X[0]
n_h = 4
n_y = shape_Y[0]

expected: 5,4,2
result: 2,4,1

Thanks,
J

Hi @chichulito ,

What you have done here is that your code referenced to a global variable shape_X and shape_Y that you might have defined earlier in the code rather then using the input parameters. The correct way is:

 X.shape[0] 

The comments on arguments explained what X, and Y are. Here X is array object. To get the dimensions of X, we can use X.shape. The ‘dot’ is how to call the method ‘shape’. By specifying which element we are interested in, we can write X.shape[0] for accessing the first element of the dimensions of X returned by the shape method. This is very different from shape_X which is a variable name.

Don’t forget to rerun the code from start when you have made changes to your code.

2 Likes

@chichulito, @Kic is correct.

Another way to think about this more generally is within the subfunctions what arguments are being passed in ?

In this case:

def layer_sizes(X, Y):

It is only X and Y, so if you need to pull information (weights, sizes, etc) assume you must do so only via the variables the function is giving you access to in the definition.

Second, in a number of cases, if you read the instructions for the particular exercise carefully you will see they have provided bits of code (in the areas that you don’t need to edit) that will sort of already ‘break down’ this subproblem for you.

For example, in exercise 5 here:

def compute_cost(A2, Y):
    
    Computes the cross-entropy cost given in equation (13)
    
    Arguments:
    A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
    Y -- "true" labels vector of shape (1, number of examples)

    Returns:
    cost -- cross-entropy cost given equation (13)
    
    m = Y.shape[1] # number of examples

They are breaking the problem down for you by ‘giving’ you ‘m’, rather than making you hunt for it. Yet it is worthy to note where they are getting it from to understand when writing your own programs.

2 Likes

Thanks! Works now!