Closed anayurg closed 1 year ago
Please also see Issue #46 which is related to this.
Dear @anayurg,
May I suggest just loading the whole TM directly?
saving:
import pickle
from tmu.models.classification.vanilla_classifier import TMClassifier
from tmu.data.tmu_datasource import TMUDatasetSource
data = TMUDatasetSource().get_dataset(
"XOR_biased",
cache=True,
cache_max_age=1,
features=["X", "Y"],
labels=["xor"],
shuffle=True,
train_ratio=1000,
test_ratio=1000,
return_type=dict
)
tm = TMClassifier(20,10,2)
tm.fit(data["x_train"], data["y_train"])
with open('tm.pkl', 'wb') as f:
pickle.dump(tm, f)
loading:
from tmu.models.classification.vanilla_classifier import TMClassifier
from tmu.data.tmu_datasource import TMUDatasetSource
import os
import pickle
data = TMUDatasetSource().get_dataset(
"XOR_biased",
cache=True,
cache_max_age=1,
features=["X", "Y"],
labels=["xor"],
shuffle=True,
train_ratio=1000,
test_ratio=1000,
return_type=dict
)
with open('tm.pkl', 'rb') as f:
tm_new = pickle.load(f)
y_pred_new = tm_new.predict(data["x_test"])
print((y_pred_new == data["y_test"]).mean())
I've also corrected
AttributeError: 'ClauseBank' object has no attribute 'lcm_p'. Did you mean: 'lcc_p'?
Thanks for finding this :)
Reopen if this does not satisfy your requirements.
Hey there,
I'm playing around with the Tsetlin classifier and I'm having some trouble figuring out how to save a trained classifier so that it can be loaded later. Do you have a go-to method that you can recommend? I couldn't do it with pickle and joblib. So I tried pickling the clause banks and weight banks separately but that seems to work only if I pickle them before the classifier is used for any prediction which in my case is not an option.
I'm attaching a minimal example just in case but I imagine there must be another way to do it that I haven't figured out.
saving.py
loading.py
The error that I get when I first predict something and then save the clause and weight banks is the following:
Thanks for the help!