Code error: C2_W1_Lab03_CoffeeRoasting_Numpy

Week 1: Using Numpy to simulate TF as in Optional Lab 3
C2_W1_Lab03_CoffeeRoasting_Numpy

STEP 1:
I wrote the following function (almost identical to lab):

#simulate TF’s predict function for an input array X of m observations and n inputs/features, and a 2-layer fixed NN with given weights and biases
def my_predict(X,W1,b1,W2,b2):
** #m is the number of records in the testing dataset**
** m=X.shape[0]**
** #Calculate prediction for each observation, there will be one output for each obs**
** calc_pred = np.zeros((m,1)) **
** #remember the prediction is an array of m values as there is a prediction for each test dataset/observation**
** for i in range(m):**
** calc_pred[i,0]=my_sequential(X,W1,b1,W2,b2) #set 0th, i.e. first column of prediction array with sequential’s output**
** print(f"calc_pred value for {i}th observation is {calc_pred[i,0]}")**
** return(calc_pred)**

STEP 2: I then call the above function with pre-set weights and biases given in the lab:
X_tst = np.array([
** [200,13.9], # postive example**
** [200,17]]) # negative example**

X_tstn = norm_l(X_tst) # remember to normalize
print(f"X_tstn: {X_tstn}")
#predictions = np.zeros((2,1))
p=my_predict(X_tstn, W1_tmp, b1_tmp, W2_tmp, b2_tmp)

I get the following error.
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

ValueError Traceback (most recent call last)
in <cell line: 8>()
6 print(f"X_tstn: {X_tstn}")
7 #predictions = np.zeros((2,1))
----> 8 p=my_predict(X_tstn, W1_tmp, b1_tmp, W2_tmp, b2_tmp)

My weights and biases are as given in the lab.
why does Python think that variable p is a scalar? I am expecting p to be set with the 1D array of predictions returned by my_predict

Thanks for posting the image of your code, that makes it a lot more readable.

Two comments:

  • It looks like your X values have not been normalized.
  • What is in your my_sequential() function?

It would help if you show more of the error message traceback.

thank you for your response. Here are my Dense and my Sequential functions

I have remembered to set the weights and biases given in the optional lab

I believe I am also normalizing appropriately here using TF’s normalize function, as done in the lab

If you’re trying to do this lab in numpy, then why are you using norm_l()?
That’s a tensorflow keras layer. You have to use the .adapt() method to have the normalization learn the correct hyperparameters.

I don’t think so. These are not normalized values.
image

See this line where the error is:

The error says that you can’t write a sequence (whatever g(…) returns) into the a_out[j] location (which wants a scalar).

Note that the course includes a complete version of this lab using Numpy.