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()
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:
It’s easy to convert a Tensor to a numpy array by using the “numpy()” method.
TF has its own way to cast a tensor.
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.
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)
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. 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.