|
| 1 | +""" |
| 2 | +Train an SVM on a 2D classification problem, then plot a gradient vector field |
| 3 | +for the predicted class probabilty. |
| 4 | +""" |
| 5 | + |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import numpy as np |
| 8 | +import sk2torch |
| 9 | +import torch |
| 10 | +from sklearn.svm import SVC |
| 11 | + |
| 12 | +# Create a dataset of two Gaussians. There will be some overlap |
| 13 | +# between the two classes, which adds some uncertainty to the model. |
| 14 | +xs = np.concatenate( |
| 15 | + [ |
| 16 | + np.random.random(size=(256, 2)) + [1, 0], |
| 17 | + np.random.random(size=(256, 2)) + [-1, 0], |
| 18 | + ], |
| 19 | + axis=0, |
| 20 | +) |
| 21 | +ys = np.array([False] * 256 + [True] * 256) |
| 22 | + |
| 23 | +# Train an SVM on the data and wrap it in PyTorch. |
| 24 | +sk_model = SVC(probability=True) |
| 25 | +sk_model.fit(xs, ys) |
| 26 | +model = sk2torch.wrap(sk_model) |
| 27 | + |
| 28 | +# Create a coordinate grid to compute a vector field on. |
| 29 | +spaced = np.linspace(-2, 2, num=25) |
| 30 | +grid_xs = torch.tensor([[x, y] for x in spaced for y in spaced], requires_grad=True) |
| 31 | + |
| 32 | +# Compute the gradients of the SVM output. |
| 33 | +outputs = model.predict_proba(grid_xs)[:, 1] |
| 34 | +(input_grads,) = torch.autograd.grad(outputs.sum(), (grid_xs,)) |
| 35 | + |
| 36 | +# Create a quiver plot of the vector field. |
| 37 | +plt.quiver( |
| 38 | + grid_xs[:, 0].detach().numpy(), |
| 39 | + grid_xs[:, 1].detach().numpy(), |
| 40 | + input_grads[:, 0].detach().numpy(), |
| 41 | + input_grads[:, 1].detach().numpy(), |
| 42 | +) |
| 43 | +plt.savefig("svm_vector_field.png") |
0 commit comments