Quickstart
Install
git clone https://github.com/Armangb1/pygorch.git
cd pygorch
pip install -e . # runtime (NumPy)
pip install -e ".[dev]" # + pytest, pytest-cov, ruff for development
Tensors and gradients
import numpy as np
import gorch
x = gorch.Tensor(np.array([[0.0], [1.0]]), requires_grad=True)
W = gorch.Tensor(np.random.randn(1, 1), requires_grad=True)
y = (x @ W).tanh()
y.sum().backward()
print(W.grad.value) # shape matches W.value
Scalar outputs can call backward() with no arguments. For non-scalar outputs,
pass an explicit gradient seed:
y.backward(gorch.Tensor(np.ones_like(y.value)))
Train a small network
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)
x = gorch.Tensor(X)
target = gorch.Tensor(Y)
for _ in range(800):
opt.zero_grad()
pred = model(x)
loss = loss_fn(pred, target)
loss.backward()
opt.step()
Save and load
state = model.state_dict() # dict of parameter names -> numpy arrays
model.load_state_dict(state) # restore weights in place
model.save("model.pkl") # pickles the state_dict
model.load("model.pkl") # and back
Run the examples
python examples/xor_mlp.py # MLP learning the XOR function
python examples/neural_control.py # Kalman filtering + EKF/RLS system identification
Run the tests
pip install -e ".[dev]"
pytest