carbono is a lightweight neural network library for JavaScript that supports both classification and regression tasks with multiple activation functions, optimizers, and loss functions. This api adheres to the wip uai web specification.
// Create a new neural network instance
const nn = new carbono(debug = true);
debug
: Enable/disable console logging (default: true)
Returns: this
nn.layer(inputSize, outputSize, activation)
inputSize
: Number of input neuronsoutputSize
: Number of output neuronsactivation
: Activation function ("tanh", "sigmoid", "relu", "selu", "softmax")
Returns: this
await nn.train(trainSet, options)
Parameters:
trainSet
: Array of{input: [], output: []}
objectsoptions
: Training configuration object:
{
epochs: 200, // Training iterations
learningRate: 0.212, // Learning rate
printEveryEpochs: 100, // Logging frequency
earlyStopThreshold: 1e-6, // Early stopping condition
testSet: null, // Validation data
callback: null, // Epoch callback function
optimizer: "sgd", // "sgd" or "adam"
lossFunction: "mse" // "mse" or "cross-entropy"
}
Returns: Training summary object:
{
parameters: number, // Total model parameters
training: {
loss: number, // Final training loss
testloss: number, // Final test loss
time: number, // Training duration (ms)
epochs: number, // Total epochs
learningRate: number // Learning rate used
}
}
nn.predict(input, tags = true)
input
: Input arraytags
: Return labeled predictions (default: true)
Returns:
- Classification with
tags=true
: Array of{tag: string, probability: number}
- Otherwise: Raw output array
// Save model
await nn.save(name = "model")
// Load model
await nn.load(callback)
nn.info({
name: "My Model",
author: "John Doe",
license: "MIT",
note: "Description",
date: "2023-01-01"
})
tanh
: Hyperbolic tangentsigmoid
: Sigmoid functionrelu
: Rectified Linear Unitselu
: Scaled Exponential Linear Unitsoftmax
: Softmax (for classification)
mse
: Mean Squared Errorcross-entropy
: Cross Entropy Loss
sgd
: Stochastic Gradient Descentadam
: Adam Optimizer
// Create network
const nn = new carbono();
// Add layers
nn.layer(2, 4, "tanh");
nn.layer(4, 1, "sigmoid");
// Training data
const data = [
{input: [0,0], output: [0]},
{input: [0,1], output: [1]},
{input: [1,0], output: [1]},
{input: [1,1], output: [0]}
];
// Train
await nn.train(data, {
epochs: 1000,
learningRate: 0.1
});
// Predict
const prediction = nn.predict([0,1]);
// Save model
await nn.save("xor_model");
- Automatic label encoding for classification
- Early stopping to prevent overfitting
- Xavier weight initialization
- Gradient clipping
- Model saving/loading (.uai format)
- Asynchronous training with callbacks
- Comprehensive training metrics
- Support for both regression and classification
This documentation reflects the actual implementation while maintaining clarity and usability for developers.