Assignment: Single Perceptron NN

Hi, having trouble with exercises 2 and 6.

Here’s my challenge in 2 …

GRADED FUNCTION: layer_sizes

def layer_sizes(X, Y):
“”"
Arguments:
X – input dataset of shape (input size, number of examples)
Y – labels of shape (output size, number of examples)

Returns:
n_x -- the size of the input layer
n_y -- the size of the output layer
"""
### START CODE HERE ### (~ 2 lines of code)
# Size of input layer.
n_x = np.size(X[0][0])
# Size of output layer.
n_y = np.size(Y[0][0])
### END CODE HERE ###
return (n_x, n_y)

Here’s my graded result:
Wrong size of the input layer n_x for the test case, where array X has a shape (5, 100).
Expected: 5.
Got: 1.
Wrong size of the output layer n_y for the test case, where array Y has a shape (3, 100).
Expected: 3.
Got: 1.
2 Tests passed
2 Tests failed

Here’s my challenge in 6 …

I’e input this code as required:

START CODE HERE ### (~ 1 line of code)

parameters_multi = nn_model(X_multi_norm, Y_multi_norm, num_iterations=100, print_cost=True)

END CODE HERE

print("W = " + str(parameters_multi[“W”]))
print("b = " + str(parameters_multi[“b”]))

W_multi = parameters_multi[“W”]
b_multi = parameters_multi[“b”]

But this is the error I;m getting:

ValueError Traceback (most recent call last)
in
1 ### START CODE HERE ### (~ 1 line of code)
----> 2 parameters_multi = nn_model(X_multi_norm, Y_multi_norm, num_iterations=100, print_cost=True)
3 ### END CODE HERE ###
4
5 print("W = " + str(parameters_multi[“W”]))

in nn_model(X, Y, num_iterations, print_cost)
27 ### START CODE HERE ### (~ 2 lines of code)
28 # Forward propagation. Inputs: “X, parameters”. Outputs: “Y_hat”.
—> 29 Y_hat = forward_propagation(X, parameters)
30
31 # Cost function. Inputs: “Y_hat, Y”. Outputs: “cost”.

in forward_propagation(X, parameters)
18 # Implement Forward Propagation to calculate Z.
19 ### START CODE HERE ### (~ 2 lines of code)
—> 20 Z = W @ X + b
21 Y_hat = Z
22 ### END CODE HERE ###

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 1)

Any help on how I can get around this problem??

1 Like

Solved!!

### START CODE HERE ### (~ 2 lines of code)
# Size of input layer.
n_x = np.shape(X)[0]
# Size of output layer.
n_y = np.shape(Y)[0]
### END CODE HERE ###
return (n_x, n_y)
1 Like

Hi @ultron_code ,

You can use np.size() too. Here is how to do it:

np.size(X,0)

The integer 0 means getting the number of elements from the row dimension, if it is 1, it means from the column dimension.

1 Like

Thank you. Appreciate it.

How about the part on the nn_model() ?? Can you help??

1 Like

np.size(X,0) doesn’t seem to work. np.shape(X)[0] works. Aim was to find the input and output layer sizes :slightly_smiling_face:

1 Like

Hi @ultron_code ,

In the nn_model(), there is a call to initialize_parameters() to generate the weights matrix and the bias vector. So you need to make sure the correct arguments are passed to this function so that the weights matrix and bias vector are created according to the network structure.

1 Like

Hi @ultron_code ,

The assertionError is raised because the W matrix generated in your function is in a wrong shape, it should be (n_y, n_x).

1 Like

Meaning that the following is correct?

W = np.random.randn(n_y, n_x) * 0.01
b = np.zeros((n_y, n_x))

1 Like

Hi @Piltanquito36 ,

The bias is a vector, so it should look like this b = np.zeros((n_y, 1)). Here is the comments within the code cell specified what the return argument looks like.
“”"
Returns:
params – python dictionary containing your parameters:
W – weight matrix of shape (n_y, n_x)
b – bias value set as a vector of shape (n_y, 1)
“”"

1 Like