My code returned wrong output when I wrote n_x = np.shape(X[0]) but it worked fine with n_x = X.shape[0]. Why is this so ?
The shape of a 2 dimensional array has 2 elements: the first element (index 0) is the number of rows and the second element (index 1) is the number of columns. It just turns out that the way Prof Ng defines the X “sample” matrices here, the dimensions are n_x by m, where n_x is the number of features and m is the number of samples. So each column of X is one input sample vector. And the number of rows gives you the number of features, which is what we need in order to define the number of neurons in the first layer here.
Well, taking the shape of X[0] means you end up with the shape of one row of X, right? X[0] is the first row of X. So that will be a python tuple. That’s not what you want. You want just the n_x value.
Python is an interactive language. You don’t have to just “wonder” what something does: you can try it and see. Watch this:
np.random.seed(42)
A = np.random.randn(3,4)
print(A)
print(A.shape)
Azero = A[0]
print(Azero)
print(np.shape(Azero))
[[ 0.49671415 -0.1382643 0.64768854 1.52302986]
[-0.23415337 -0.23413696 1.57921282 0.76743473]
[-0.46947439 0.54256004 -0.46341769 -0.46572975]]
(3, 4)
[ 0.49671415 -0.1382643 0.64768854 1.52302986]
(4,)
So you can see that it gives you a 1-tuple (4,). There are two problems with that: a) it’s a tuple and b) the value it contains is the number of columns not the number of rows, right?
Yes sir, got it!!
I have another doubt, the no. of hidden layers in this program is 4 , then why is only Z2 is calculated? shouldn’t it be till Z4?
It is not the number of hidden layers, it is the number of output neurons from the one hidden layer, right?
Z2 is the output of layer 2 (before activation). Just because you know how many entries it has, that does not mean you know what the values are, right? That’s the point.
And what is Z4? There are only 2 layers here: layer 1 and layer 2.
Actually what I was trying to ask was, Z1 or A1 corresponds to activation of layer 1 similarly A2 corresponds to activation of layer 2 , but here we have total no. of hidden layers as 4 so where is A3 ( I counted the activation of the last layer as A4)?
The point is that we do not have 4 layers here. 4 is the number of units in the one hidden layer. We only have 2 layers:
Layer 1 has n_x inputs and 4 outputs.
Layer 2 has 4 inputs and n_y outputs.
oh okay . sorry sir, my bad