LNN
LNN copied to clipboard
model serialisation
Serialize all model rules and facts reflective of model.print
Do we want human-readable serialization like json
or byte-like one? (EDIT: or which one is preferred as first one)
We can start with byte-code, having the ability to dump and load a model is more important than having the dump human-readable right now.
Simple usage example and API proposition:
from lnn import Predicate, Variable, And, Model, Fact
model = Model()
x = Variable("x")
p1 = Predicate(name="p1")
p2 = Predicate(name="p2")
formula = And(p1(x), p2(x))
model.add_formulae(p1, p2, formula)
model.add_facts(
{"p1": {"a": Fact.TRUE, "b": Fact.FALSE}, "p2": {"a": Fact.FALSE, "b": Fact.FALSE}}
)
model.infer()
model.save("my_model")
loaded_model = Model.from_file("my_model")
assert model == loaded_model
@NaweedAghmad Possible problem is that we do not have __eq__
implemented for Model
class, so not sure about the assert
statement.
The Model.from_file
is @classmethod
and they can be understand as alternative constructors. Alternative name can be Model.load
.
Would Pickle Work Like:
def save(self, filename):
with open(filename, 'wb') as outp: # Overwrites any existing file.
pickle.dump(self, outp, pickle.HIGHEST_PROTOCOL)
def from_file(self,filename):
with open(filename, 'rb') as inp:
return pickle.load(inp)
if so i would be happy to contribute @NaweedAghmad