A reusable library for building, training, and using binary classifiers using Axon.
Usage
Training
data = [
%{text: "This is a positive text", label: 1},
%{text: "This is a negative text", label: 0},
# ... more data
]
# Use a map for explicit label mapping
classifier = BinClass.Trainer.train(data,
epochs: 5,
labels: %{0 => :negative, 1 => :positive}
)
# Save the model
BinClass.save(classifier, "model.bin")Prediction
# Load the model as a serving (supports custom compiler/defn_options)
serving = BinClass.load("model.bin", compiler: EXLA)
result = Nx.Serving.run(serving, "Some text to classify")
# result is %{label: :positive, confidence: 0.99, ...}Ultra-Low Latency Inference
# Load raw classifier and compile a predictor function
classifier = BinClass.load_classifier("model.bin")
predict = BinClass.compile_predictor(classifier)
result = predict.("Instant prediction")
Summary
Functions
Compiles the classifier into a highly optimized, in-process prediction function.
Deserializes a saved model from a binary and returns an Nx.Serving struct.
Deserializes a saved model from a binary and returns a BinClass.Classifier struct.
Loads a saved model from a file and returns an Nx.Serving struct.
Loads a saved model from a file and returns a BinClass.Classifier struct.
Saves the classifier to a file.
Serializes the classifier to a binary.
Functions
Compiles the classifier into a highly optimized, in-process prediction function.
This is intended for scenarios where lowest possible latency is required and batching (provided by Nx.Serving) is not necessary (e.g. CLI tools, single-user scripts, or very low-concurrency high-speed inference).
Returns an anonymous function that takes a text (string) or list of texts and returns the classification results.
Options
:compiler- The compiler to use. Defaults toEXLA.:batch_size- The batch size to compile for. Defaults to 1 (lowest latency).
Deserializes a saved model from a binary and returns an Nx.Serving struct.
Deserializes a saved model from a binary and returns a BinClass.Classifier struct.
Loads a saved model from a file and returns an Nx.Serving struct.
Loads a saved model from a file and returns a BinClass.Classifier struct.
Saves the classifier to a file.
Serializes the classifier to a binary.