Load Models DynamicallyΒΆ

The names of models you see on the Models page can be passed to the Model.load() class method to dynamically import the model plugin and load the model.

single.py

import asyncio

from dffml import Model, Features, Feature, train


async def main():
    # Load the model using the entrypoint listed on the model plugins page
    SLRModel = Model.load("slr")

    # Configure the model
    model = SLRModel(
        features=Features(Feature("Years", int, 1)),
        predict=Feature("Salary", int, 1),
        location="slr-model",
    )

    # Train the model
    await train(
        model,
        {"Years": 0, "Expertise": 1, "Trust": 0.1, "Salary": 10},
        {"Years": 1, "Expertise": 3, "Trust": 0.2, "Salary": 20},
    )


if __name__ == "__main__":
    asyncio.run(main())
$ python single.py

You can also load all the models you have installed by not passing any arguments to load. All models have a CONFIG property which is a dataclasses. You can inspect the properties of the dataclass using the dataclasses.fields() function.

all.py

import asyncio
import dataclasses

from dffml import Model


async def main():
    # Load each model class
    for model_cls in [Model.load("slr")]:
        # Print the class
        print(model_cls)
        # Print all the config properties
        for field in dataclasses.fields(model_cls.CONFIG):
            print(f"    {field.name}: {field.metadata['description']}")


if __name__ == "__main__":
    asyncio.run(main())
$ python all.py
<class 'dffml.model.slr.SLRModel'>
    predict: Label or the value to be predicted
    features: Features to train on. For SLR only 1 allowed
    location: Location where state should be saved