LNN icon indicating copy to clipboard operation
LNN copied to clipboard

model serialisation

Open NaweedAghmad opened this issue 2 years ago • 4 comments

Serialize all model rules and facts reflective of model.print

NaweedAghmad avatar Apr 12 '22 08:04 NaweedAghmad

Do we want human-readable serialization like json or byte-like one? (EDIT: or which one is preferred as first one)

mikulatomas avatar Apr 13 '22 06:04 mikulatomas

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.

NaweedAghmad avatar Apr 13 '22 11:04 NaweedAghmad

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.

mikulatomas avatar Apr 16 '22 08:04 mikulatomas

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

anomius avatar Aug 13 '22 20:08 anomius