Weights on week 2 exercise 8

Once the weights are learned on the training set, I get a dimension error when I try to use the same weights on the test set:

ValueError: shapes (7,28) and (12,1) not aligned: 28 (dim 1) != 12 (dim 0)

How do we deal with the fact that we’ve learned 28 weights for the training set when the test set only has 12 inputs?

Also, my dw appears to be a 7x28 or 28x7 matrix. This does not seem correct, but all of the previous exercises pass testing.

Hi, @tckm0526. Could you insert a snapshot of the “traceback”? That’s the error-log reported to the screen when you try to run the code.

The w and dw values here should be column vectors of the same dimension. Note that it is the number of rows not columns of the training and test input matrices that give you the number of “features”, which is the number of elements in w. The number of columns is the number of samples in each set and there is no need for those to agree between the training and test data.

I added some print statements to my code and here are the dimensions and some of the results that I see when I run the test cell for exercise 8:

type(X_train) <class 'numpy.ndarray'>
X_train.shape (4, 7)
X_test.shape (4, 3)
Y_test.shape (3,)
num_iterations 50 learning_rate 0.01
pred train [[1. 1. 0. 1. 0. 0. 1.]]
pred test [[1. 1. 0.]]
w [[ 0.08639757]
 [-0.08231268]
 [-0.11798927]
 [ 0.12866053]]
b -0.039832360948163205
All tests passed!

You’ll notice that the number 28 does not occur anywhere there so there is some fundamental issue in how you are interpreting what needs to happen here. The number of “features” is 4 for this particular set of input data.

Of course the other important point to make here is that all the code we are writing is intended to be “general” in the sense that it will work with inputs of any size. Meaning that the code needs to derive any dimensions from the actual input data, not by “hard-coding” anything.

Cost after iteration 0: 4.852030

ValueError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
123 y_test = np.array([0, 1, 0])
124
→ 125 d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"

in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
46 # YOUR CODE STARTS HERE
47 [snip]
49
50 # YOUR CODE ENDS HERE

in predict(w, b, X)
21 # A = …
22 # YOUR CODE STARTS HERE
—> 23 [snip]
24
25 # YOUR CODE ENDS HERE

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (7,28) and (12,1) not aligned: 28 (dim 1) != 12 (dim 0)

This helped! I had the wrong argument passed to initialize_with_zeroes.

Indeed. It looks like you must have been multiplying the row and column dimensions in order to get 28 and 12. Glad to hear you were able to sort it out.