You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

quick.rst 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. .. _quick:
  2. ============================================================
  3. Quick Start
  4. ============================================================
  5. Introduction
  6. ====================
  7. This ``Quick Start`` guide aims to illustrate the straightforward process of establishing a full ``Learnware`` workflow
  8. and utilizing ``Learnware`` to handle user tasks.
  9. Installation
  10. ====================
  11. Learnware is currently hosted on `PyPI <https://pypi.org/project/learnware/>`_. You can easily intsall the ``learnware`` package by following these steps:
  12. .. code-block:: bash
  13. pip install learnware
  14. 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:
  15. .. code-block:: bash
  16. pip install learnware[full]
  17. .. note::
  18. 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.
  19. Prepare Learnware
  20. ====================
  21. In the ``learnware`` package, each learnware is encapsulated in a ``zip`` package, which should contain at least the following four files:
  22. - ``learnware.yaml``: learnware configuration file.
  23. - ``__init__.py``: methods for using the model.
  24. - ``stat.json``: the statistical specification of the learnware. Its filename can be customized and recorded in learnware.yaml.
  25. - ``environment.yaml`` or ``requirements.txt``: specifies the environment for the model.
  26. To facilitate the construction of a learnware, we provide a `Learnware Template <https://www.bmwu.cloud/static/learnware-template.zip>`_ 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 <../workflows/upload:prepare-learnware>`_.
  27. Learnware Package Workflow
  28. ============================
  29. Users can start a ``Learnware`` workflow according to the following steps:
  30. Initialize a Learnware Market
  31. -------------------------------
  32. The ``EasyMarket`` class provides the core functions of a ``Learnware Market``.
  33. You can initialize a basic ``Learnware Market`` named "demo" using the code snippet below:
  34. .. code-block:: python
  35. from learnware.market import instantiate_learnware_market
  36. # instantiate a demo market
  37. demo_market = instantiate_learnware_market(market_id="demo", name="easy", rebuild=True)
  38. Upload Leanware
  39. -------------------------------
  40. Before uploading your learnware to the ``Learnware Market``,
  41. you'll need to create a semantic specification, ``semantic_spec``. This involves selecting or inputting values for predefined semantic tags
  42. to describe the features of your task and model.
  43. For instance, the following codes illustrates the semantic specification for a Scikit-Learn type model.
  44. This model is tailored for education scenarios and performs classification tasks on tabular data:
  45. .. code-block:: python
  46. from learnware.specification import generate_semantic_spec
  47. semantic_spec = generate_semantic_spec(
  48. name="demo_learnware",
  49. data_type="Table",
  50. task_type="Classification",
  51. library_type="Scikit-learn",
  52. scenarios="Education",
  53. license="MIT",
  54. )
  55. After defining the semantic specification,
  56. you can upload your learnware using a single line of code:
  57. .. code-block:: python
  58. demo_market.add_learnware(zip_path, semantic_spec)
  59. Here, ``zip_path`` is the directory of your learnware ``zip`` package.
  60. Semantic Specification Search
  61. -------------------------------
  62. To find learnwares that align with your task's purpose, you'll need to provide a semantic specification, ``user_semantic``, that outlines your task's characteristics.
  63. The ``Learnware Market`` will then perform an initial search using ``user_semantic``, identifying potentially useful learnwares with models that solve tasks similar to your requirements.
  64. .. code-block:: python
  65. # construct user_info which includes a semantic specification
  66. user_info = BaseUserInfo(id="user", semantic_spec=semantic_spec)
  67. # search_learnware: performs semantic specification search when user_info doesn't include a statistical specification
  68. search_result = easy_market.search_learnware(user_info)
  69. single_result = search_results.get_single_results()
  70. # single_result: the List of Tuple[Score, Learnware] returned by semantic specification search
  71. print(single_result)
  72. Statistical Specification Search
  73. ---------------------------------
  74. If you decide in favor of porviding your own statistical specification file, ``stat.json``,
  75. the ``Learnware Market`` can further refine the selection of learnwares from the previous step.
  76. This second-stage search leverages statistical information to identify one or more learnwares that are most likely to be beneficial for your task.
  77. For example, the code below executes learnware search when using Reduced Kernel Mean Embedding (RKME) as the statistical specification:
  78. .. code-block:: python
  79. import learnware.specification as specification
  80. user_spec = specification.RKMETableSpecification()
  81. # unzip_path: directory for unzipped learnware zipfile
  82. user_spec.load(os.path.join(unzip_path, "rkme.json"))
  83. user_info = BaseUserInfo(
  84. semantic_spec=user_semantic, stat_info={"RKMETableSpecification": user_spec}
  85. )
  86. search_result = easy_market.search_learnware(user_info)
  87. single_result = search_results.get_single_results()
  88. multiple_result = search_results.get_multiple_results()
  89. # search_item.score: based on MMD distances, sorted in descending order
  90. # search_item.learnware.id: id of learnwares, sorted by scores in descending order
  91. for search_item in single_result:
  92. print(f"score: {search_item.score}, learnware_id: {search_item.learnware.id}")
  93. # mixture_item.learnwares: collection of learnwares whose combined use is beneficial
  94. # mixture_item.score: score assigned to the combined set of learnwares in `mixture_item.learnwares`
  95. for mixture_item in multiple_result:
  96. print(f"mixture_score: {mixture_item.score}\n")
  97. mixture_id = " ".join([learnware.id for learnware in mixture_item.learnwares])
  98. print(f"mixture_learnware: {mixture_id}\n")
  99. Reuse Learnwares
  100. -------------------------------
  101. 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.
  102. We offer provide two methods for reusing a given list of learnwares: ``JobSelectorReuser`` and ``AveragingReuser``.
  103. Just substitute ``test_x`` in the code snippet below with your own testing data, and you're all set to reuse learnwares:
  104. .. code-block:: python
  105. from learnware.reuse import JobSelectorReuser, AveragingReuser
  106. # using jobselector reuser to reuse the searched learnwares to make prediction
  107. reuse_job_selector = JobSelectorReuser(learnware_list=mixture_item.learnwares)
  108. job_selector_predict_y = reuse_job_selector.predict(user_data=test_x)
  109. # using averaging ensemble reuser to reuse the searched learnwares to make prediction
  110. reuse_ensemble = AveragingReuser(learnware_list=mixture_item.learnwares)
  111. ensemble_predict_y = reuse_ensemble.predict(user_data=test_x)
  112. We also provide two method when the user has labeled data for reusing a given list of learnwares: ``EnsemblePruningReuser`` and ``FeatureAugmentReuser``.
  113. 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:
  114. .. code-block:: python
  115. from learnware.reuse import EnsemblePruningReuser, FeatureAugmentReuser
  116. # Use ensemble pruning reuser to reuse the searched learnwares to make prediction
  117. reuse_ensemble = EnsemblePruningReuser(learnware_list=mixture_item.learnwares, mode="classification")
  118. reuse_ensemble.fit(train_X, train_y)
  119. ensemble_pruning_predict_y = reuse_ensemble.predict(user_data=data_X)
  120. # Use feature augment reuser to reuse the searched learnwares to make prediction
  121. reuse_feature_augment = FeatureAugmentReuser(learnware_list=mixture_item.learnwares, mode="classification")
  122. reuse_feature_augment.fit(train_X, train_y)
  123. feature_augment_predict_y = reuse_feature_augment.predict(user_data=data_X)
  124. Auto Workflow Example
  125. ============================
  126. The ``Learnware`` also offers automated workflow examples.
  127. This includes preparing learnwares, uploading and deleting learnwares from the market, and searching for learnwares using both semantic and statistical specifications.
  128. To experience the basic workflow of the Learnware Market, please refer to `Learnware Examples <https://github.com/Learnware-LAMDA/Learnware/tree/main/examples>`_.