W2_A1_Ex-1_NameError: name ‘train_set_x_orig’ is not defined

Please i need more detailed explanation on Exercise 1 of my First programming Test. I find it difficult to understand. Thanks

You have posted in the “General Discussions” forum area. That’s not specific to any particular course.

We don’t know what course you are attending.

Please move your thread to the correct course forum. You can do this using the “pencil” icon next to the thread title.

Thanks for moving this to the DLS Course 1 forum.

Which week and assignment are you working on? “First programming Test” isn’t sufficient information.

Please give the exact name, or post a URL.

Week 2, First Programming Assignment

Hopefully one of the mentors for Course 1 will be able to help.
I don’t have access to that course.

Hello @Chukwu_Nnaemeka_Paul! Can you be more specific, please?

For Week 2, the first programming assignment is Python Basics with Numpy (optional assignment) and it’s exercise 1 is:

Exercise 1

Set test to "Hello World" in the cell below to print “Hello World” and run the two cells below.

And if you are talking about the first (required) programming assignment, Logistic_Regression_with_a_Neural_Network_mindset, its exercise 1 is:

Exercise 1

Find the values for: - m_train (number of training examples) - m_test (number of test examples) - num_px (= height = width of a training image) Remember that train_set_x_orig is a numpy-array of shape (m_train, num_px, num_px, 3). For instance, you can access m_train by writing train_set_x_orig.shape[0].

Which explanation you found difficult to understand? And, are you familiar with Python?

Best,
Saif.

NameError: name ‘train_set_x_orig’ is not defined

This is what I’m getting after running the cell of Exercise 1. I dont understand what that means. Hence, I need more explanations please.
I am not really use to Python.
Thanks

Also, in Exercise 2, I got

NameError: name ‘train_set_x_orig’ is not defined

after running the cell. What am i not doing well please?

Please run all the above cells.

This is what i got after submitting my First Assignment,

[ValidateApp | INFO] Validating ‘/home/jovyan/work/submitted/courseraLearner/W2A2/Logistic_Regression_with_a_Neural_Network_mindset.ipynb’
[ValidateApp | INFO] Executing notebook with kernel: python3
Tests failed on 5 cell(s)! These tests could be hidden. Please check your submission.

The following cell failed:

dim = 2
w, b = initialize_with_zeros(dim)

assert type(b) == float
print ("w = " + str(w))
print ("b = " + str(b))

initialize_with_zeros_test_1(initialize_with_zeros)
initialize_with_zeros_test_2(initialize_with_zeros)

The error was:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-11-5a383810e519> in <module>
      2 w, b = initialize_with_zeros(dim)
      3 
----> 4 assert type(b) == float
      5 print ("w = " + str(w))
      6 print ("b = " + str(b))

AssertionError: 

==========================================================================================
The following cell failed:

w =  np.array([[1.], [2]])
b = 1.5
X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
Y = np.array([[1, 1, 0]])
grads, cost = propagate(w, b, X, Y)

assert type(grads["dw"]) == np.ndarray
assert grads["dw"].shape == (2, 1)
assert type(grads["db"]) == np.float64


print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print ("cost = " + str(cost))

propagate_test(propagate)

The error was:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-13-7cd89fb9dcd2> in <module>
      3 X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
      4 Y = np.array([[1, 1, 0]])
----> 5 grads, cost = propagate(w, b, X, Y)
      6 
      7 assert type(grads["dw"]) == np.ndarray

NameError: name 'propagate' is not defined

==========================================================================================
The following cell failed:

params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009...

print ("w = " + str(params["w"]))
print ("b = " + str(params["b"]))
print ("dw = " + str(grads["dw"]))
print ("db = " + str(grads["db"]))
print("Costs = " + str(costs))

optimize_test(optimize)

The error was:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-3483159b4470> in <module>
----> 1 params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_ra...
      2 
      3 print ("w = " + str(params["w"]))
      4 print ("b = " + str(params["b"]))
      5 print ("dw = " + str(grads["dw"]))

<ipython-input-14-bd93eeef1d6a> in optimize(w, b, X, Y, num_iterations, learning_ra...
     35         # grads, cost = ...
     36         # YOUR CODE STARTS HERE
---> 37         grads, cost = propagate(w, b, X, Y)
     38 
     39         # YOUR CODE ENDS HERE

NameError: name 'propagate' is not defined

==========================================================================================
The following cell failed:

w = np.array([[0.1124579], [0.23106775]])
b = -0.3
X = np.array([[1., -1.1, -3.2],[1.2, 2., 0.1]])
print ("predictions = " + str(predict(w, b, X)))

predict_test(predict)

The error was:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-2cb6511f8fa3> in <module>
      2 b = -0.3
      3 X = np.array([[1., -1.1, -3.2],[1.2, 2., 0.1]])
----> 4 print ("predictions = " + str(predict(w, b, X)))
      5 
      6 predict_test(predict)

NameError: name 'predict' is not defined

==========================================================================================
The following cell failed:

from public_tests import *

model_test(model)

The error was:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-19-9408a3dffbf6> in <module>
      1 from public_tests import *
      2 
----> 3 model_test(model)

~/work/submitted/courseraLearner/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['...

<ipython-input-18-9de3f2cd5b26> in model(X_train, Y_train, X_test, Y_test, num_iter...
     34 
     35     # YOUR CODE STARTS HERE
---> 36     w = np.zeros((train_x.shape[0], 1))
     37     b = 0
     38 

NameError: name 'train_x' is not defined

What did i do wrong please?

The ones flagged as wrong were not the particular ones i answered. There is something wrong somewhere. Please i need more explanation

Hi @Chukwu_Nnaemeka_Paul , we will just look at the first one now.

This means that the b returned by your work of the initialize_with_zeros_test_1 function is not float. I suggest you to check up the type of your b by adding a print line in the following place of your code:

    # (≈ 2 lines of code)
    # w = ...
    # b = ...
    print('The type of my variable b is', type(b)) #please remove this line after debugging, otherwise it can interfere with the autograder at submission and fail your work

Check out the type, google for the difference between what you get and what you are required to get (a float), and then figure it out yourself on how to make the correction.

Note that you need to remove the print line after debugging.

Cheers,
Raymond

This is what i got when i ran the code:

NameError Traceback (most recent call last)
in
1 dim = 2
----> 2 w, b = initialize_with_zeros(dim)
3
4 assert type(b) == float
5 print ("w = " + str(w))

in initialize_with_zeros(dim)
17 # b = …
18 # YOUR CODE STARTS HERE
—> 19 w = np.zeros((dim, 1))
20 b = 0
21 print(‘The type of my variable b is’, type(b))

NameError: name ‘np’ is not defined

I dont still understand it.

Hello @Chukwu_Nnaemeka_Paul,

In the following cases, a similar NameError will occur. Can you tell me, in each of the following cases, (1) what causes the error, or (2) if we were the Python interpreter, what would we print out?

Case 1:

variable_a = 'Hello'
print(variable_B)

Case 2:

def this_is_a_function():
    variable_that_is_defined_in_the_function = 'World'

print(variable_that_is_defined_in_the_function )

Case 3:

another_variable = np.ones(3)
print(another_variable)

Raymond

Most likely that means you have not run all the previous cells in the notebook. Anytime you close and reopen the notebook, you need to rerun everything to recreate the runtime state of the notebook. Try “Cell → Run All Above” and then run your test again.

To explain this a bit more, the question is where is “np” defined? It is in the very first “import” cell in the notebook, right? Please go find that cell and read it to understand what it does. Then run it. Then try your test again.

If you are new to DLS, it’s worth taking a look at the DLS FAQ Thread. There are many topics there, including one about “name not defined”. Please have a look and it explains the same thing that I explained above.

BTW all this was also explained in the lecture in Week 2 called “A Quick Introduction to the Jupyter Notebooks”. If you are struggling with the mechanics of the Jupyter notebooks, that might be worth another look (or a first look as the case may be). :nerd_face:

Once you fix that problem, you will then have an assertion failure which is telling you that the type of your b value is incorrect: it should be “float”, but it isn’t. So what type is it? You can find out by adding a print statement in that cell:

print(type(b))

I predict it will show the type as integer. The point is that in python 0 is not the same thing as 0. right? That decimal point there makes a big difference.

Case 1:
The error is caused by printing variable_B (which was not defined) instead of variable_a

Case 2:
The Python interpreter would print ‘World’

Case 3:
The Python interpreter would print np.ones (3)

Paul

Hello @Chukwu_Nnaemeka_Paul,

All three cases will result in NameError that something is not defined. In case 1, as you said, variable_B wasn’t defined.

In case 2, variable_that_is_defined_in_the_function was also NOT defined in the global context. It was only defined in the context of the function, but not in the global context.

In case 3, np was the guy that was not defined. You know what np should refer to, but Python doesn’t. You will need to explicitly tell Python what np is by, for example, adding one more line:

import numpy as np #without this line, Python doesn't know what `np` is.

another_variable = np.ones(3)
print(another_variable)

These are the common cases that generate the NameError. The reason why I asked you these questions is that:-

  1. NameError appeared 7 times in your replies in this thread, so you need to know how it can occur, so that you can think about how to diagnose this problem and how to fix this problem

  2. I hope you will run my three cases and see the errors for yourself. You should have seen errors in all three cases.


Now after knowing the causes of NameError, @paulinpaloalto’s reply actually shared useful action items and explanation that I would follow if I were you. My final piece of suggestion is: if the NameError occurs, make sure the variable has been defined, and make sure the cell that defines the variable has been run and run successfully.

Good luck for the rest of your debugging.

Cheers,
Raymond

Thanks a lot @paulinpaloalto. I will go back and check it out again. Hopefully, i would do better.

Thank you so much @rmwkwok. I will try it out again.