Week2_lab2_error

In exercise 2, where we have to build the layers of the models, I get this error when I run
the cell that calls model summery. The error is:

ValueError Traceback (most recent call last)
in
----> 1 model.summary()

/opt/conda/lib/python3.7/site-packages/keras/engine/training.py in summary(self, line_length, positions, print_fn, expand_nested, show_trainable)
2774 if not self.built:
2775 raise ValueError(
→ 2776 'This model has not yet been built. ’
2777 'Build the model first by calling build() or by calling ’
2778 ‘the model on a batch of data.’)

ValueError: This model has not yet been built. Build the model first by calling build() or by calling the model on a batch of data.

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

I had the same problem but it eventually worked after running this cell for several times.

This is possibly happening because the model isn’t yet aware of the shape of X. Adding the layers, but not specifying the shape of the input vector, would cause this type of error; the first layer would be (???, 25) and the summary is impossible to compute yet.

(This is technically mentioned in point 4.4 of the practice lab, “Tensorflow Model Implementation”, although I also missed it the first time and got the same error).

You have two options to finalize the model implementation:

  • the first is to actually specify the shape of the input vector, which would be done with a line identical to the one shown in 4.4;
  • the second would be to compile and fit the model; by fitting it to X and y, tensorflow will adapt the model to the shape of the input data.

I would say that the practice lab expects you to implement the first option, but technically, both work. If you do the second, you’d just add some more epochs to the overall model fitting.

1 Like