Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

PyTorch (continued)

GPU is available, using GPU Tesla T4

XOR Dataset Classification

<Figure size 960x720 with 1 Axes>
(1024, 2) (1024,)
Train: 820, Val: 204, Test: 64

Training script (for classification with an arbitrary model)

Model creation and training

100%|██████████| 100/100 [00:03<00:00, 28.19it/s, epoch=100 / 100, train_acc=0.987, train_loss=0.036, val_acc=0.990, val_loss=0.025]
<Axes: >
<Figure size 750x375 with 2 Axes>
<Figure size 960x720 with 1 Axes>

Customizing model behavior

NoisyModel( (hidden_layer1): NoisyLinear() (hidden_layer2): Linear(in_features=16, out_features=16, bias=True) (hidden_layer3): Linear(in_features=16, out_features=2, bias=True) )
100%|██████████| 200/200 [00:06<00:00, 29.55it/s, epoch=200 / 200, train_acc=0.962, train_loss=0.097, val_acc=1.000, val_loss=0.039]
<Axes: >
<Figure size 750x375 with 2 Axes>
<Figure size 960x720 with 1 Axes>

MNIST Classification

Sequential( (0): Flatten(start_dim=1, end_dim=-1) (1): Linear(in_features=784, out_features=32, bias=True) (2): ReLU() (3): Linear(in_features=32, out_features=32, bias=True) (4): ReLU() (5): Linear(in_features=32, out_features=10, bias=True) )

Adam optimizer

The Adam, or adaptive momentum optimizer, makes use of two minimal extensions to stochastic gradient descent:

  • adaptive learning rate

  • momentum

100%|██████████| 20/20 [01:26<00:00,  4.31s/it, epoch=020 / 020, train_acc=0.971, train_loss=0.099, val_acc=0.960, val_loss=0.138]
<Figure size 750x375 with 2 Axes>
                                                  
Test accuracy: 0.960

Learning rate schedulers

100%|██████████| 10/10 [00:43<00:00,  4.31s/it, epoch=010 / 010, lr=0.0417, train_acc=0.978, train_loss=0.069, val_acc=0.962, val_loss=0.135]
<Figure size 750x375 with 2 Axes>
                                                  
Test accuracy: 0.963