Skip to content

Trainer

Trainer is an orchestrator class used to streamline, train, and manage the training process.

Parameters:

Name Type Description Default
model Module

Model to train.

required
optimizer Optimizer

Optimizer used for parameter updates.

required
loss

Loss function used during training.

required

Example:

trainer = ntk.Trainer(
    model=my_model,
    optimizer=ntk.Adam(parameters=my_model.parameters()),
    loss=ntk.MeanSquaredError()
)

fit

Train the model on the provided data.

Training data may be provided as raw tensors, a Dataset, or a DataLoader. When using raw input tensors, labels must be provided through the y argument.

Parameters:

Name Type Description Default
data

Training data.

required
y

Training labels when using raw input tensors.

None
epochs int

Number of training epochs.

1
batch_size int

Number of samples per batch.

None
shuffle bool

Whether to shuffle the training data.

None
drop_remainder_batch bool

Whether to drop incomplete batches.

None
validation_split float

Fraction of training data reserved for validation.

None
validation_data

Separate validation dataset or dataloader.

None
verbose bool

Whether to track progess in the terminal. Defaults to True

None

Returns:

Type Description
History

After training has completed, a History object is returned that can be used to plot training metrics overtime.

Example

history = trainer.fit(dataloader, epochs=100)

history.plot("loss", "val_loss)

Implementatino

trainer = ntk.Trainer(
    model=my_model,
    optimizer=ntk.Adam(parameters=my_model.parameters()),
    loss=ntk.BinaryCrossEntropy()
)

history = trainer.fit(dataloader, epochs=300)
history.plot("loss")

History Plot Output

Plot Output

History Metrics

The History object tracks two training metrics (with more to come), loss and validation loss, which can be plotted like so:

history.plot("loss", "val_loss")