-
Notifications
You must be signed in to change notification settings - Fork 7
Advanced Usage
This guide dives deeper into Brain4J, focusing on how to work efficiently with datasets, advanced training techniques, and utilizing GPU acceleration.
SmartTrainer automates training by handling batch updates, stopping conditions, and evaluation.
// Learning rate decay 0.95, evaluate every 5 epochs
SmartTrainer trainer = new SmartTrainer(0.95, 5);
// Train until loss < 0.01
trainer.start(model, dataSource, 0.01);trainer.startFor(model, dataSource, 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(ListDataSource dataSource, EvaluationResult evaluation, int epoch, long took) {
System.out.print("\rEpoch " + epoch + " loss: " + evaluation.loss() + " took " + (took / 1e6) + " ms");
}
}
trainer.addListener(new ExampleListener());Brain4J now supports hardware-accelerated neural network operations through its tensor system. Tensors replace traditional matrices and vectors, providing multidimensional data structures optimized for neural network operations.
Tensors are N-dimensional arrays that can represent scalars (0D), vectors (1D), matrices (2D), and higher-dimensional data. They form the foundation of modern neural networks.
// Create a 2D tensor (matrix)
Tensor matrix = Tensors.matrix(3, 4); // 3x4 matrix
// Create a 3D tensor
Tensor tensor3D = Tensors.create(2, 3, 4); // shape [2,3,4]
// Create tensors with initial values
Tensor ones = Tensors.ones(2, 2); // 2x2 matrix filled with 1.0
Tensor zeros = Tensors.zeros(3, 3); // 3x3 matrix filled with 0.0
Tensor random = Tensors.random(2, 3); // 2x3 matrix with random values
// Create from existing data
float[] data = {1.0f, 2.0f, 3.0f, 4.0f};
Tensor fromData = Tensors.of(new int[]{2, 2}, data); // Creates a 2x2 tensorBrain4J can automatically use GPU acceleration for tensor operations when available, providing significant speedups for large models.
// Check if GPU is available
boolean gpuAvailable = TensorGPU.isGpuAvailable();
// Check if GPU is currently being used
boolean usingGPU = Tensors.isUsingGPU();
// Enable GPU if available
Brain4J.useGPUIfAvailable();
// Force CPU usage (even if GPU is available)
Brain4J.forceCPU();
// Remember to release GPU resources when done
TensorGPU.releaseGPUResources();The tensor system provides optimized operations for neural networks:
// Matrix multiplication
Tensor result = tensorA.matmul(tensorB);
// Element-wise operations
Tensor sum = tensorA.add(tensorB);
Tensor difference = tensorA.sub(tensorB);
Tensor product = tensorA.mul(tensorB);
Tensor quotient = tensorA.div(tensorB);
// Apply function to all elements
Tensor activated = tensor.map(x -> Math.max(0, x)); // ReLU activation
// Reshape tensor
Tensor reshaped = tensor.reshape(3, 4);
// Transpose dimensions
Tensor transposed = matrix.transpose();- Use cases: Check out Examples & Use Cases
This wiki is still under construction. If you feel that you can contribute, please do so! Thanks.