Quick Start#

Introduction#

This Quick Start guide aims to illustrate the straightforward process of establishing a full Learnware workflow and utilizing Learnware to handle user tasks.

Installation#

Learnware is currently hosted on PyPI. You can easily intsall the learnware package by following these steps:

pip install learnware

In the learnware package, besides the base classes, many core functionalities such as “learnware specification generation” and “learnware deployment” rely on the torch library. Users have the option to manually install torch, or they can directly use the following command to install the learnware package:

pip install learnware[full]

Note

However, it’s crucial to note that due to the potential complexity of the user’s local environment, installing learnware[full] does not guarantee that torch will successfully invoke CUDA in the user’s local setting.

Prepare Learnware#

In the learnware package, each learnware is encapsulated in a zip package, which should contain at least the following four files:

  • learnware.yaml: learnware configuration file.

  • __init__.py: methods for using the model.

  • stat.json: the statistical specification of the learnware. Its filename can be customized and recorded in learnware.yaml.

  • environment.yaml or requirements.txt: specifies the environment for the model.

To facilitate the construction of a learnware, we provide a Learnware Template that the users can use as a basis for building your own learnware. We’ve also detailed the format of the learnware zip package in Learnware Preparation.

Learnware Package Workflow#

Users can start a Learnware workflow according to the following steps:

Initialize a Learnware Market#

The EasyMarket class provides the core functions of a Learnware Market. You can initialize a basic Learnware Market named “demo” using the code snippet below:

from learnware.market import instantiate_learnware_market

# instantiate a demo market
demo_market = instantiate_learnware_market(market_id="demo", name="easy", rebuild=True)

Upload Leanware#

Before uploading your learnware to the Learnware Market, you’ll need to create a semantic specification, semantic_spec. This involves selecting or inputting values for predefined semantic tags to describe the features of your task and model.

For instance, the following codes illustrates the semantic specification for a Scikit-Learn type model. This model is tailored for education scenarios and performs classification tasks on tabular data:

from learnware.specification import generate_semantic_spec

semantic_spec = generate_semantic_spec(
    name="demo_learnware",
    data_type="Table",
    task_type="Classification",
    library_type="Scikit-learn",
    scenarios="Education",
    license="MIT",
)

After defining the semantic specification, you can upload your learnware using a single line of code:

demo_market.add_learnware(zip_path, semantic_spec)

Here, zip_path is the directory of your learnware zip package.

Reuse Learnwares#

With the list of learnwares, mixture_learnware_list, returned from the previous step, you can readily apply them to make predictions on your own data, bypassing the need to train a model from scratch. We offer provide two methods for reusing a given list of learnwares: JobSelectorReuser and AveragingReuser. Just substitute test_x in the code snippet below with your own testing data, and you’re all set to reuse learnwares:

from learnware.reuse import JobSelectorReuser, AveragingReuser

# using jobselector reuser to reuse the searched learnwares to make prediction
reuse_job_selector = JobSelectorReuser(learnware_list=mixture_item.learnwares)
job_selector_predict_y = reuse_job_selector.predict(user_data=test_x)

# using averaging ensemble reuser to reuse the searched learnwares to make prediction
reuse_ensemble = AveragingReuser(learnware_list=mixture_item.learnwares)
ensemble_predict_y = reuse_ensemble.predict(user_data=test_x)

We also provide two method when the user has labeled data for reusing a given list of learnwares: EnsemblePruningReuser and FeatureAugmentReuser. Just substitute test_x in the code snippet below with your own testing data, and substitute train_X, train_y with your own training labeled data, and you’re all set to reuse learnwares:

from learnware.reuse import EnsemblePruningReuser, FeatureAugmentReuser

# Use ensemble pruning reuser to reuse the searched learnwares to make prediction
reuse_ensemble = EnsemblePruningReuser(learnware_list=mixture_item.learnwares, mode="classification")
reuse_ensemble.fit(train_X, train_y)
ensemble_pruning_predict_y = reuse_ensemble.predict(user_data=data_X)

# Use feature augment reuser to reuse the searched learnwares to make prediction
reuse_feature_augment = FeatureAugmentReuser(learnware_list=mixture_item.learnwares, mode="classification")
reuse_feature_augment.fit(train_X, train_y)
feature_augment_predict_y = reuse_feature_augment.predict(user_data=data_X)

Auto Workflow Example#

The Learnware also offers automated workflow examples. This includes preparing learnwares, uploading and deleting learnwares from the market, and searching for learnwares using both semantic and statistical specifications. To experience the basic workflow of the Learnware Market, please refer to Learnware Examples.