Examples
The repository ships two runnable demos. Here is exactly what they print today.
1. XOR — a multi-layer perceptron
examples/xor_mlp.py trains a small network on the classic XOR problem:
model = gorch.nn.Sequential(
gorch.nn.Linear(2, 8),
gorch.nn.Tanh(),
gorch.nn.Linear(8, 2),
gorch.nn.Softmax(),
)
loss_fn = gorch.nn.CrossEntropyLoss()
opt = gorch.optim.Adam(model.parameters(), lr=0.05)
epoch 0 loss 0.4915 accuracy 50%
epoch 100 loss 0.0015 accuracy 100%
epoch 200 loss 0.0006 accuracy 100%
epoch 300 loss 0.0003 accuracy 100%
epoch 400 loss 0.0002 accuracy 100%
epoch 500 loss 0.0001 accuracy 100%
epoch 600 loss 0.0001 accuracy 100%
epoch 700 loss 0.0001 accuracy 100%
final accuracy: 100%
predictions: [0, 1, 1, 0]
It reaches 100% accuracy in about 100 epochs of vanilla Adam.
2. Neural control
examples/neural_control.py demos three control-flavoured tools.
Kalman filter — state estimation
A double-integrator with noisy position measurements, filtered online:
1) Kalman filter: estimating position/velocity of a noisy double integrator
measurement MSE : 0.0836
filtered MSE : 0.0275 <- filtered is tighter
EKF optimizer — identifying a nonlinear plant
A neural network learns the unknown nonlinear mapping y = tanh(Wx) + 0.2 x₀ x₁
using the extended-Kalman optimizer instead of gradient descent:
2) EKF optimizer: identify a nonlinear plant with a neural network
mean absolute prediction error: 0.0993 (random init ~ 1.2)
RLS — online system identification
Recursive least squares recovers the weights of a linear plant sample by sample:
3) RLS: online least-squares identification of a linear plant
true weights : [0.01709638 0.67987377]
estimated : [0.01710572 0.67987443]
max error : 9.33e-06
Run them yourself
python examples/xor_mlp.py
python examples/neural_control.py