-
Notifications
You must be signed in to change notification settings - Fork 7
Advanced Usage
echo edited this page Mar 1, 2025
·
13 revisions
This guide dives deeper into Brain4J, focusing on how to work efficiently with datasets and advanced training techniques.
The DataSet class is designed to manage your training data efficiently. You can split data into batches using partition and partitionWithSize.
Divides the dataset into a fixed number of batches.
DataSet<DataRow> fullData = new DataSet<>();
// Populate dataset with some sample data
for (int i = 0; i < 100; i++) {
Vector input = Vector.random(5); // 5 input features
Vector output = Vector.of(Math.random());
fullData.add(new DataRow(input, output));
}
// Split data into 10 batches
fullData.partition(10);
// Access batches
List<List<DataRow>> batches = fullData.getPartitions();Splits the dataset into batches of a given size.
// Create batches with 16 samples each
fullData.partitionWithSize(16);
// Access batches
List<List<DataRow>> batches = fullData.getPartitions();Before training, it's good practice to shuffle the dataset to improve learning.
fullData.shuffle();SmartTrainer automates training by handling batch updates, stopping conditions, and evaluation.
SmartTrainer trainer = new SmartTrainer(0.95, 5); // Learning rate decay 0.95, evaluate every 5 epochs
trainer.start(model, fullData, 0.01, 0.001); // Train until loss < 0.01 or tolerance exceededtrainer.startFor(model, fullData, 1000); // Train for 1000 epochsYou can add listeners to track the training process in real time.
private static class ExampleListener extends TrainListener {
@Override
public void onEvaluated(DataSet<DataRow> dataSet, int epoch, double loss, long took) {
System.out.print("\rEpoch " + epoch + " loss: " + loss + " took " + (took / 1e6) + " ms");
}
}
trainer.addListener(new ExampleListener());- Use cases: Check out Examples & Use Cases
This wiki is still under construction. If you feel that you can contribute, please do so! Thanks.