Programming Assignment: Practice Lab: Neural Networks for Binary Classification

When creating a post, please add:

  • Week # must be added in the tags option of the post.
  • Link to the classroom item you are referring to:
  • Description (include relevant info but please do not post solution code or your entire notebook):
    Hi, I need help completing the Final Practice Lab in Week 1. In the notebook, my code passes all the tests however when I submit the Lab I fail with 0/100 grade. The error I receive is this :

Cell #32. Can’t compile the student’s code. Error: AttributeError(“\n ‘EagerTensor’ object has no attribute ‘astype’.\n If you are looking for numpy-related methods, please run the following:\n from tensorflow.python.ops.numpy_ops import np_config\n np_config.enable_numpy_behavior()”)
Traceback (most recent call last):
File “/home/www/app/grading/exceptions.py”, line 112, in handle_solution_errors
yield {}
File “/home/www/app/grading/abstract.py”, line 393, in _grade
context = compiled_code.run(cell_index=cell.index)
File “/home/www/app/grading/submission/compiled_code.py”, line 195, in run
return list(self._code_items.values())[cell_num - 1].run()
File “/home/www/app/grading/submission/compiled_code.py”, line 54, in run
return import_module(self.import_statement, items)
File “/usr/local/lib/python3.7/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1006, in _gcd_import
File “”, line 983, in _find_and_load
File “”, line 967, in _find_and_load_unlocked
File “”, line 677, in _load_unlocked
File “”, line 728, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/tmp/student_solution_cells/cell_32.py”, line 34, in
Yhat = (Prediction >= 0.5).astype(int)
File “/usr/local/lib/python3.7/site-packages/tensorflow/python/framework/ops.py”, line 512, in getattr
np_config.enable_numpy_behavior()“”".format(type(self).name, name))
AttributeError:
‘EagerTensor’ object has no attribute ‘astype’.
If you are looking for numpy-related methods, please run the following:
from tensorflow.python.ops.numpy_ops import np_config
np_config.enable_numpy_behavior()

Please help me!

The way you formatted the exception trace leaves out some valuable information. In general it’s better to use the {} formatting tool to avoid having your “copy/paste” text get interpreted as “markdown”, which messes up the formatting.

But as best I can tell, it is the above line that is “throwing”. What the error is telling you is that Prediction is a TF EagerTensor and you can’t use the “astype()” method with a tensor. That only works with a numpy array. So there are several choices:

  1. It’s easy to convert a Tensor to a numpy array by using the “numpy()” method.
  2. TF has its own way to cast a tensor.
  3. Do you really care whether the result is an integer type? Usually the type coercion rules will take care of interpreting a Boolean as a number if that is required.

Also are you sure you want >= there? Usually in binary classifiers I’ve seen, the criterion for True is > 0.5 not >=. But I don’t know the NLP Course 4 material, so maybe that is correct in your context. Or maybe it doesn’t really matter in any case, since we’re just doing floating point approximations here. :nerd_face:

Thank you for replying! Yes I see the line of code that causing the issue. The problem is the cell was already written in the Lab assignment and I can t find a way to edit the cell ( editing seems to be blocked)

Yhat = (Prediction >= 0.5).astype(int)
print("predict a zero: ",Yhat[0], "predict a one: ", Yhat[500])

Heres the code the course has already in the assignment

The problem then is that your code that is used to set the Prediction variable has a defect.

The traceback is that Prediction comes from my_sequential_v(), which uses my_dense_v().

Since my_sequental_v() is provided for you, then I’d guess the defect is in your my_dense_v() function.

1 Like

Sorry, I am not familiar with the details of that assignment, so I didn’t realize the code you were showing was test code that was given to you. If the test fails, the solution is not to change the test. There’s a reason that usually make the test cells not editable. :nerd_face: So as Tom says, that means that the defect in your code is that Prediction ends up as a TF EagerTensor, when it should be a numpy array. So now you have to figure out why that happened.

Hmm my model seems pretty straight forward. Do you notice anything out of the ordinary?

# UNQ_C1
# GRADED CELL: Sequential model

model = Sequential(
    [               
        tf.keras.Input(shape=(400,)),    #specify input size
        ### START CODE HERE ### 
        tf.keras.layers.Dense(25, activation="sigmoid"),
        tf.keras.layers.Dense(15, activation="sigmoid"),
        tf.keras.layers.Dense(1, activation="sigmoid")
        ### END CODE HERE ### 
    ], name = "my_model" 
)

That’s not the function I’m referring to.

  • That’s your TensorFlow model (Exercise 1).
  • I’m discussing the my_dense_v() function that is in the Numpy implementation (Exercise 2).

The “astype(int)” method that is throwing your error is only used in Exercise 2.