Getting started
Quickstart
Train a small network on XOR and read its predictions.
XOR is the classic problem a single neuron cannot solve, and a small network can.
1. Describe the network
python
from pymlkit import Network, Activations, Losses
model = Network(2) # two inputs
model.add_dense(8, Activations.ReLU) # hidden layer
model.add_dense(1, Activations.Sigmoid) # output layer, last
model.set_loss(Losses.HalfMSE)The output layer is whichever one you add last. There is no separate output declaration.
2. Prepare flat data
Samples are concatenated into one flat list. Four samples with two inputs each become eight numbers.
python
x = [0, 0, 0, 1, 1, 0, 1, 1]
y = [0, 1, 1, 0]3. Fit
python
model.fit(x * 50, y * 50, lr=0.5, epochs=300, batch=4, log_step=100)Repeating the four samples gives the optimizer more steps per epoch. Training prints progress and a loss value every log_step epochs.
4. Predict
python
for i in range(4):
sample = x[i * 2 : i * 2 + 2]
print(sample, "->", round(model.forward(sample)[0], 2))
# [0, 0] -> 0.02
# [0, 1] -> 0.99
# [1, 0] -> 0.99
# [1, 1] -> 0.01Exact numbers vary between runs because weights are initialised randomly.
