BinClass (BinClass v0.1.2)

Copy Markdown View Source

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

compile_predictor(classifier, opts \\ [])

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 to EXLA.
  • :batch_size - The batch size to compile for. Defaults to 1 (lowest latency).

deserialize(binary, opts \\ [])

Deserializes a saved model from a binary and returns an Nx.Serving struct.

deserialize_classifier(binary)

Deserializes a saved model from a binary and returns a BinClass.Classifier struct.

load(path, opts \\ [])

Loads a saved model from a file and returns an Nx.Serving struct.

load_classifier(path)

Loads a saved model from a file and returns a BinClass.Classifier struct.

save(classifier, path)

Saves the classifier to a file.

serialize(classifier)

Serializes the classifier to a binary.