From 5416ca5594fce13c5190c92b567c8e30eb8a6356 Mon Sep 17 00:00:00 2001 From: Daniel Bourke Date: Fri, 11 Mar 2022 11:47:41 +1000 Subject: [PATCH] add plot_decision_boundary --- helper_functions.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/helper_functions.py b/helper_functions.py index 344090c..bec8994 100644 --- a/helper_functions.py +++ b/helper_functions.py @@ -5,8 +5,46 @@ If a function gets defined once and could be used over and over, it'll go in her """ import torch import matplotlib.pyplot as plt +import numpy as np + from torch import nn +def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Tensor): + """Plots decision boundaries of model predicting on X in comparison to y. + + Source - https://madewithml.com/courses/foundations/neural-networks/ (with modifications) + """ + # Put everything to CPU (works better with NumPy + Matplotlib) + model.to("cpu") + X, y = X.to("cpu"), y.to("cpu") + + # Setup prediction boundaries and grid + x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1 + y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1 + xx, yy = np.meshgrid(np.linspace(x_min, x_max, 101), + np.linspace(y_min, y_max, 101)) + + # Make features + X_to_pred_on = torch.from_numpy(np.column_stack((xx.ravel(), yy.ravel()))).float() + + # Make predictions + model.eval() + with torch.inference_mode(): + y_logits = model(X_to_pred_on) + + # Test for multi-class or binary and adjust logits to prediction labels + if len(torch.unique(y)) > 2: + y_pred = torch.softmax(y_logits, dim=1).argmax(dim=1) # mutli-class + else: + y_pred = torch.round(torch.sigmoid(y_logits)) # binary + + # Reshape preds and plot + y_pred = y_pred.reshape(xx.shape).detach().numpy() + plt.contourf(xx, yy, y_pred, cmap=plt.cm.RdYlBu, alpha=0.7) + plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.RdYlBu) + plt.xlim(xx.min(), xx.max()) + plt.ylim(yy.min(), yy.max()) + # Plot linear data or training and test and predictions (optional) def plot_predictions(train_data, train_labels,