Practice Lab: Neural Networks for Handwritten Digit Recognition, Binary

When creating a post, please add:

/content/public_tests.py in test_c1(target)
      8     assert len(target.layers) == 3, \
      9         f"Wrong number of layers. Expected 3 but got {len(target.layers)}"
---> 10     assert target.input.shape.as_list() == [None, 400], \
     11         f"Wrong input shape. Expected [None,  400] but got {target.input.shape.as_list()}"
     12     i = 0


/usr/local/lib/python3.11/dist-packages/keras/src/ops/operation.py in input(self)
    274             Input tensor or list of input tensors.
    275         """
--> 276         return self._get_node_attribute_at_index(0, "input_tensors", "input")
    277 
    278     @property


/usr/local/lib/python3.11/dist-packages/keras/src/ops/operation.py in _get_node_attribute_at_index(self, node_index, attr, attr_name)
    305         """
    306         if not self._inbound_nodes:
--> 307             raise AttributeError(
    308                 f"The layer {self.name} has never been called "
    309                 f"and thus has no defined {attr_name}."


AttributeError: The layer my_model has never been called and thus has no defined input.

Hi,

I’m working on the Week 1 Assignment for Course 2 (“Neural Networks for Binary Classification”) and I’m having trouble passing the first unit test:

python

CopiarEditar

from public_tests import * test_c1(model)

I believe the issue is with how I’m building the Keras model. The test seems to expect:

  • Input shape: (400,) (flattened 20x20 image)

  • 3 Dense layers, all with sigmoid activation:

    • Layer 1: 25 units

    • Layer 2: 15 units

    • Layer 3: 1 unit (output)

  • No extra layers (Flatten, Dropout, etc.)

Here’s my current model code:

python

CopiarEditar

{mentor edit: code removed}

Question:
Even with this structure, test_c1(model) still fails. Could someone please clarify the exact requirements for the model to pass this test, or point out what I might be missing?

Thanks in advance!

German Ardila

Please do not share your code on the forum. That is not allowed by the Code of Conduct.

The requirements for the model() function are given in the notebook Markdown instructions.

Tip: Be sure that you are only modifying the marked portions of the function code (between START CODE HERE and END CODE HERE). Do not add any other code that does things like importing libraries, etc.

The necessary code for that is already provided for you.

So, do not do this within your code:

And there are “Hint” sections in the notebook that give you all of the clues you need.

Every time you open a notebook, you must run all of the cells starting from the top of the file.

1 Like

@TMosh, Thanks for the advice I really appreciate it. I’ll make sure to keep the Code of Conduct in mind moving forward.

German