How to use Green Tsetlin
Basic Usage
With green_tsetlin built in dependencies, few packages is required other than green_tsetlin.
import green_tsetlin as gt
Get a dataset that has binary features. For this simple example, we use the xor dataset:
train_x, train_y, eval_x, eval_y = gt.dataset_generator.xor_dataset()
The Tsetlin Machine is the core of Green Tsetlin. Here a dense (regular) TM:
tm = gt.TsetlinMachine(n_literals=train_x.shape[1],
n_clauses=5,
n_classes=2,
s=3.0,
threshold=42,
literal_budget=4)
And here is the sparse TM.
tm = gt.SparseTsetlinMachine(n_literals=train_x.shape[1],
n_clauses=5,
n_classes=2,
s=3.0,
threshold=42,
literal_budget=4)
With the green_tsetlin Trainer we can wrap the TM and train it.
trainer = gt.Trainer(tm, seed=42, n_jobs=1)
trainer.set_train_data(train_x, train_y)
trainer.set_eval_data(eval_x, eval_y)
results = trainer.train() # can also be accessed by trainer.results
Exporting and importing models
After training, the Tsetlin Machine can easily be exported:
tm.save_state("tsetlin_state.npz")
And imported to continue training or use for inference:
tm_trained = gt.TsetlinMachine.load_state("tsetlin_state.npz")
predictor = tm_trained.get_predictor()
predictor.predict([0, 1, 1, 1])
inference
Using the trained TM for inference lets us predict and explain the prediction. This means, given a set of features, we can see which features was important for that specific prediction.
Literal explanations
First we have to get the predictor class. We can get explanations on literals, features or both. If a feature is divided into more than one literal, it would be insightful to instead use feature explanation. For this example, one feature is one literal, so we use literals.
tm_trained = gt.TsetlinMachine.load_state("tsetlin_state.npz")
predictor = tm_trained.get_predictor(explanation="literals", exclude_negative_clauses=False)
Then, we want to test on a simple example:
example = [0, 1, 1, 1]
y_pred, expl = predictor.predict_and_explain(example)
Showing the explanation gives on insight in what features were important.
for i, (f, w) in enumerate(zip(example, expl)):
print(f"feature {i}:{f} - {w}")
feature 0:0 - 124
feature 1:1 - 192
feature 2:1 - 0
feature 3:1 - 0
Feature explanation
Head to the Iris tutorial to see how feature explanation is used.
Exporting predictor as c program
With a trained TM we can export the predictor as c program:
predictor = tm.get_predictor(explanation="literals", exclude_negative_clauses=False)
predictor.export_as_program("xor_tm_dense.h")