mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.14k stars 2.53k forks source link

Incorrect behaviour of plot_decision_boundary() in 02_neural_network_classification_with_tensorflow.ipynb #320

Closed donalhill closed 2 years ago

donalhill commented 2 years ago

Hello,

I am working through video 76 on the "TensorFlow Developer Certificate in 2022: Zero to Mastery" course. The function plot_decision_boundary() has the following logic:

if len(y_pred[0]) > 1:
    print("doing multiclass classification...")
    # We have to reshape our predictions to get them ready for plotting
    y_pred = np.argmax(y_pred, axis=1).reshape(xx.shape)
  else:
    print("doing binary classifcation...")
    y_pred = np.round(y_pred).reshape(xx.shape) #rounding to give us a 0 or 1

When I check len(y_pred[0]) which comes from y_pred = model.predict(x_in), I get a length of 2. So the current function then follows the logic for multi-class classification, although the problem being worked on is binary. Can this be fixed?

Cheers! Donal

mrdbourke commented 2 years ago

Hey @donalrinho, looks like this is closed, did you solve it?

You could solve it by checking the dimensions of y_pred[0].

It depends on the shape of your output predictions.

If you're getting something like:

y_pred = [[0, 1, 0],
                 [1, 0, 0]...

The above code should work.

If your y_pred looks different, have a play around with the dimension checking logic to see if you can adjust it for your own use case.

For example, you could check the shape or ndim of the output tensor too.

donalhill commented 2 years ago

Hey @mrdbourke,

Thanks for getting back to me! The issue was transient it seems, and went away when I reran a bunch of cells. Perhaps it was something silly I did when fiddling with one of the inputs.

Really enjoying your material so far, thank you for providing this resource :)

Cheers, Donal