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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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), ``HeteroSearcher`` identifies and recommends valuable learnwares from the entire market. This includes learnwares with different feature/label spaces compared to the user's task requirements, known as "heterogeneous learnwares".
  16. To enable the reuse of these heterogeneous learnwares, we have developed ``FeatureAlignLearnware`` and ``HeteroMapLearnware``.
  17. These components expand the capabilities of standard ``Learnware`` by aligning the feature and label spaces to match the user's task requirements.
  18. They also provide essential interfaces for effectively applying heterogeneous learnwares to tasks beyond their original purposes.
  19. ``FeatureAlignLearnware``
  20. ---------------------------
  21. ``FeatureAlignLearnware`` employs a neural network to align the feature space of the learnware to the user's task.
  22. It is initialized with a ``Learnware`` and has the following methods to expand the applicable scope of this ``Learnware``:
  23. - **align**: Trains a neural network to align ``user_rkme``, which is the ``RKMETableSpecification`` of the user's data, with the learnware's statistical specification.
  24. - **predict**: Predict the output for user data using the trained neural network and the original learnware's model.
  25. ``HeteroMapAlignLearnware``
  26. -----------------------------
  27. If user data is not only heterogeneous in feature space but also in label space, ``HeteroMapAlignLearnware`` uses the help of
  28. a small amount of labeled data ``(x_train, y_train)`` required from the user task to align heterogeneous learnwares with the user task.
  29. There are two critical interfaces in ``HeteroMapAlignLearnware``:
  30. - ``HeteroMapAlignLearnware.align(self, user_rkme: RKMETableSpecification, x_train: np.ndarray, y_train: np.ndarray)``
  31. - **input space alignment**: Align the feature space of the learnware to the user task's statistical specification ``user_rkme`` using ``FeatureAlignLearnware``.
  32. - **output space alignment**: Further align the label space of the aligned learnware to the user task through supervised learning of ``FeatureAugmentReuser`` using ``(x_train, y_train)``.
  33. - ``HeteroMapAlignLearnware.predict(self, user_data)``
  34. - If input space and output space alignment are performed, use the ``FeatureAugmentReuser`` to predict the output for ``user_data``.
  35. All Reuse Methods
  36. ===========================
  37. In addition to applying ``Learnware``, ``FeatureAlignLearnware`` or ``HeteroMapAlignLearnware`` objects directly by calling their ``predict`` interface,
  38. the ``learnware`` package also provides a set of ``Reuse Methods`` for users to further customize a single or multiple learnwares, with the hope of enabling learnwares to be
  39. helpful beyond their original purposes and eliminating the need for users to build models from scratch.
  40. There are two main categories of ``Reuse Methods``: (1) direct reuse and (2) reuse based on a small amount of labeled data.
  41. .. note::
  42. Combine ``HeteroMapAlignLearnware`` with the following reuse methods to enable the reuse of heterogeneous learnwares. See `WORKFLOW: Hetero Reuse <../workflows/reuse.html#hetero-reuse>`_ for details.
  43. Direct Reuse of Learnware
  44. --------------------------
  45. Two methods for direct reuse of learnwares are provided: ``JobSelectorReuser`` and ``AveragingReuser``.
  46. JobSelectorReuser
  47. ^^^^^^^^^^^^^^^^^^
  48. ``JobSelectorReuser`` trains a classifier ``job selector`` that identifies the optimal learnware for each data point in user data.
  49. There are three member variables:
  50. - ``learnware_list``: A list of ``Learnware`` objects for the ``JobSelectorReuser`` to choose from.
  51. - ``herding_num``: An optional integer that specifies the number of items to herd, which defaults to 1000 if not provided.
  52. - ``use_herding``: A boolean flag indicating whether to use kernel herding.
  53. The most important methods of ``JobSelectorReuser`` are ``job_selector`` and ``predict``:
  54. - **job_selector**: Train a ``job selector`` based on user's data and the ``learnware_list``. Processions are different based on the value of ``use_herding``:
  55. - If ``use_herding`` is False: Statistical specifications of learnwares in ``learnware_list`` combined with the corresponding learnware index are used to train the ``job selector``.
  56. - If ``use_herding`` is True:
  57. - Estimate the mixture weight based on user raw data and the statistical specifications of learnwares in ``learnware_list``
  58. - Use the mixture weight to generate ``herding_num`` auxiliary data points which mimic the user task's distribution through the kernel herding method
  59. - Finally, it learns the ``job selector`` on the auxiliary data points.
  60. - **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``.
  61. AveragingReuser
  62. ^^^^^^^^^^^^^^^^^^
  63. ``AveragingReuser`` uses an ensemble method to make predictions. It is initialized with a list of ``Learnware`` objects and has a member variable ``mode`` which
  64. specifies the ensemble method(default is set to ``mean``).
  65. - **predict**: The member variable ``mode`` provides different options for classification and regression tasks:
  66. - For regression tasks, ``mode`` should be set to ``mean``. The prediction is the average of the learnwares' outputs.
  67. - 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.
  68. Reuse Learnware with Labeled Data
  69. ----------------------------------
  70. When users have a small amount of labeled data available, the ``learnware`` package provides two methods: ``EnsemblePruningReuser`` and ``FeatureAugmentReuser`` to help reuse learnwares.
  71. They are both initialized with a list of ``Learnware`` objects ``learnware_list`` and have different implementations of ``fit`` and ``predict`` methods.
  72. EnsemblePruningReuser
  73. ^^^^^^^^^^^^^^^^^^^^^^
  74. The ``EnsemblePruningReuser`` class implements a selective ensemble approach inspired by the MDEP algorithm [1]_.
  75. It selects a subset of learnwares from ``learnware_list``, utilizing the user's labeled data for effective ensemble integration on user tasks.
  76. This method effectively balances validation error, margin ratio, and ensemble size, leading to a robust and optimized selection of learnwares for task-specific ensemble creation.
  77. - **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.
  78. - **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.
  79. FeatureAugmentReuser
  80. ^^^^^^^^^^^^^^^^^^^^^^
  81. ``FeatureAugmentReuser`` helps users reuse learnwares by augmenting features. In this method,
  82. 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``.
  83. 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
  84. on ``user_data``.
  85. - **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 ``classification``, and ``augment_reuser`` is a ``RidgeCV`` model.
  86. References
  87. -----------
  88. .. [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), Dortmund, Germany, 2022.