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.

learnware.rst 9.9 kB

2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. .. _learnware:
  2. ==========================================
  3. Learnware & Reuser
  4. ==========================================
  5. ``Learnware`` is the most basic concept in the ``learnware paradigm``. This section will introduce the concept and design of ``Learnware`` and its extension for ``Hetero Reuse``. Then, we will introduce the ``Reuse Methods``, which applies one or several ``Learnware``\ s to solve the user's task.
  6. Concepts
  7. ===================
  8. In the learnware paradigm, a learnware is a well-performed trained machine learning model with a specification that enables it to be adequately identified for reuse according to the requirement of future users who know nothing about the learnware in advance. Specifications are introduced in `COMPONENTS: Specification <./spec.html>`_.
  9. In our implementation, the class ``Learnware`` has three important member variables:
  10. - ``id``: The learnware id is generated by ``market``.
  11. - ``model``: The model in the learnware, can be a ``BaseModel`` or a dict including model name and path. When it is a dict, the function ``Learnware.instantiate_model`` is used to transform it to a ``BaseModel``. The function ``Learnware.predict`` uses the model to predict for an input ``X``. See more in `COMPONENTS: Model <./model.html>`_.
  12. - ``specification``: The specification includes the semantic specification and the statistical specification.
  13. Learnware for Hetero Reuse
  14. =======================================================================
  15. In the Hetero Market (refer to `COMPONENTS: Hetero Market <./market.html#hetero-market>`_ for more details),
  16. ``HeteroSearcher`` identifies and recommends valuable learnwares from the entire market, returning learnwares with different feature and prediction spaces compared to the user's task requirements,
  17. known as "heterogeneous learnwares".
  18. ``FeatureAlignLearnware`` and ``HeteroMapLearnware`` facilitate the deployment and reuse of heterogeneous learnwares.
  19. They extend the capabilities of standard ``Learnware`` by aligning the input and output domain of heterogeneous learnwares to match those of the user's task.
  20. These feature-aligned learnwares can then be utilized with either data-free reusers or data-dependent reusers.
  21. ``FeatureAlignLearnware``
  22. ---------------------------
  23. ``FeatureAlignLearnware`` utilizes a neural network to align the feature space of the learnware to the user's task.
  24. It is initialized with a ``Learnware`` and offers the following methods to extend the ability of this ``Learnware``:
  25. - **align**: This method trains a neural network to align ``user_rkme``(the ``RKMETableSpecification`` of the user's data) with the learnware's statistical specification.
  26. - **predict**: Using the trained neural network and the original learnware's model, this method predicts the output for the user's data.
  27. ``HeteroMapAlignLearnware``
  28. -----------------------------
  29. If user data is heterogeneous not only in feature space but also in label space, ``HeteroMapAlignLearnware`` employs
  30. minor labeled data ``(x_train, y_train)`` from the user task to align heterogeneous learnwares with the user task.
  31. ``HeteroMapAlignLearnware`` provides two key interfaces:
  32. - ``HeteroMapAlignLearnware.align(self, user_rkme: RKMETableSpecification, x_train: np.ndarray, y_train: np.ndarray)``
  33. - **Input space alignment**: Aligns the learnware's feature space to the user task's statistical specification ``user_rkme`` using ``FeatureAlignLearnware``.
  34. - **Output space alignment**: Further aligns the label space of the aligned learnware to the user task through a simple model ``FeatureAugmentReuser``, which conduct feature augmentation and is trained on ``(x_train, y_train)``.
  35. - ``HeteroMapAlignLearnware.predict(self, user_data)``
  36. - If input space and output space alignment are performed, it uses ``FeatureAugmentReuser`` to predict the output for ``user_data``.
  37. All Reuse Methods
  38. ===========================
  39. In addition to directly applying ``Learnware``, ``FeatureAlignLearnware`` or ``HeteroMapAlignLearnware`` objects by calling their ``predict`` interface,
  40. the ``learnware`` package also provides a set of baseline ``Reuse Methods`` for users to further customize single or multiple learnwares, with the hope of enabling learnwares to be
  41. helpful beyond their original purposes and reducing the need for users to build models from scratch.
  42. There are two main categories of ``Reuse Methods``: (1) data-free reusers which reuse learnwares directly and (2) data-dependent reusers which reuse learnwares with a small amount of labeled data.
  43. .. note::
  44. Combine ``HeteroMapAlignLearnware`` with the following reuse methods to reuse heterogeneous learnwares conveniently. See `WORKFLOW: Hetero Reuse <../workflows/reuse.html#hetero-reuse>`_ for details.
  45. Data-Free Reusers
  46. ------------------
  47. Two methods for direct reuse of learnwares are provided: ``JobSelectorReuser`` and ``AveragingReuser``.
  48. JobSelectorReuser
  49. ^^^^^^^^^^^^^^^^^^
  50. ``JobSelectorReuser`` trains a classifier ``job selector`` that identifies the most suitable learnware for each data point in user data.
  51. There are three member variables:
  52. - ``learnware_list``: A list of ``Learnware`` objects for the ``JobSelectorReuser`` to choose from.
  53. - ``herding_num``: An optional integer that specifies the number of items to herd, which defaults to 1000 if not provided.
  54. - ``use_herding``: A boolean flag indicating whether to use kernel herding.
  55. The most important methods of ``JobSelectorReuser`` are ``job_selector`` and ``predict``:
  56. - **job_selector**: Train a ``job selector`` based on user's data and the ``learnware_list``. The approaches varies based on the ``use_herding`` setting:
  57. - If ``use_herding`` is False: Statistical specifications of learnwares in ``learnware_list``, along with their respective indices, are used to train the ``job selector``.
  58. - If ``use_herding`` is True:
  59. - The mixture weight is estimated based on user raw data and the statistical specifications of learnwares in ``learnware_list``
  60. - The kernel herding method generates ``herding_num`` auxiliary data points to mimic the user task's distribution using the mixture weight
  61. - The ``job selector`` is then trained on these auxiliary data points
  62. - **predict**: The ``job selector`` is essentially a multi-class classifier :math:`g(\boldsymbol{x}):\mathcal{X}\rightarrow \mathcal{I}` with :math:`\mathcal{I}=\{1,\ldots, C\}`, where :math:`C` is the size of ``learnware_list``. Given a testing sample :math:`\boldsymbol{x}`, the ``JobSelectorReuser`` predicts it by using the :math:`g(\boldsymbol{x})`-th learnware in ``learnware_list``.
  63. AveragingReuser
  64. ^^^^^^^^^^^^^^^^^^
  65. ``AveragingReuser`` uses an ensemble method to make predictions. It is initialized with a list of ``Learnware`` objects and has a member variable ``mode`` which
  66. specifies the ensemble method(default is set to ``mean``).
  67. - **predict**: The member variable ``mode`` provides different options for classification and regression tasks:
  68. - For regression tasks, ``mode`` should be set to ``mean``. The prediction is the average of the learnwares' outputs.
  69. - For classification tasks, ``mode`` has two available options. If ``mode`` is set to ``vote_by_label``, the prediction is the majority vote label based on learnwares' output labels. If ``mode`` is set to ``vote_by_prob``, the prediction is the mean vector of all learnwares' output label probabilities.
  70. Data-Dependent Reusers:
  71. ------------------------
  72. When users have a small amount of labeled data available, the ``learnware`` package provides two methods: ``EnsemblePruningReuser`` and ``FeatureAugmentReuser`` to help reuse learnwares.
  73. They are both initialized with a list of ``Learnware`` objects ``learnware_list`` and have different implementations of ``fit`` and ``predict`` methods.
  74. EnsemblePruningReuser
  75. ^^^^^^^^^^^^^^^^^^^^^^
  76. The ``EnsemblePruningReuser`` class implements a selective ensemble approach inspired by the MDEP algorithm [1]_.
  77. It selects a subset of learnwares from ``learnware_list`` using a multi-objective evolutionary algorithm and uses the ``AveragingReuser`` for average ensemble.
  78. This method effectively balances validation error, margin ratio, and ensemble size, leading to a robust selection of learnwares for specific user tasks.
  79. - **fit**: Effectively prunes the large set of learnwares ``learnware_list`` by evaluating and comparing the learnwares based on their performance on user's labeled validation data ``(val_X, val_y)``. Returns the most suitable subset of learnwares.
  80. - **predict**: The ``mode`` member variable has two available options. Set ``mode`` to ``regression`` for regression tasks and ``classification`` for classification tasks. The prediction is the average of the selected learnwares' outputs.
  81. FeatureAugmentReuser
  82. ^^^^^^^^^^^^^^^^^^^^^^
  83. ``FeatureAugmentReuser`` helps users reuse learnwares by augmenting features. In this method,
  84. outputs of the learnwares from ``learnware_list`` on the user's validation data ``val_X`` are taken as augmented features and are concatenated with original features ``val_X``.
  85. The augmented data (concatenated features combined with validation labels ``val_y``) are then used to train a simple model ``augment_reuser``, which gives the final prediction
  86. on ``user_data``.
  87. - **fit**: Trains the ``augment_reuser`` using augmented user validation data. For classification tasks, ``mode`` should be set to ``classification``, and ``augment_reuser`` is a ``LogisticRegression`` model. For regression tasks, the mode should be set to ``regression``, and ``augment_reuser`` is a ``RidgeCV`` model.
  88. References
  89. -----------
  90. .. [1] Yu-Chang Wu, Yi-Xiao He, Chao Qian, and Zhi-Hua Zhou. Multi-objective evolutionary ensemble pruning guided by margin distribution. In: *Proceedings of the 17th International Conference on Parallel Problem Solving from Nature (PPSN'22)*, 2022, pp.427-441.