| @@ -167,13 +167,13 @@ def test_stat_search(): | |||
| user_spec = specification.rkme.RKMEStatSpecification() | |||
| user_spec.load(os.path.join(unzip_dir, "svm.json")) | |||
| user_info = BaseUserInfo( | |||
| id="user_0", semantic_spec={"desc": "test_user_number_0"}, stat_info={"RKME": user_spec} | |||
| id="user_0", semantic_spec={"desc": "test_user_number_0"}, stat_info={"RKMEStatSpecification": user_spec} | |||
| ) | |||
| sorted_dist_list, single_learnware_list, mixture_learnware_list = easy_market.search_learnware(user_info) | |||
| print(f"search result of user{idx}:") | |||
| for dist, learnware in zip(sorted_dist_list, single_learnware_list): | |||
| print(f"dist: {dist}, learnware_id: {learnware.id}, learnware_name: {learnware.name}") | |||
| print(f"dist: {dist}, learnware_id: {learnware.id}") | |||
| mixture_id = " ".join([learnware.id for learnware in mixture_learnware_list]) | |||
| print(f"mixture_learnware: {mixture_id}\n") | |||
| @@ -182,7 +182,7 @@ def test_stat_search(): | |||
| if __name__ == "__main__": | |||
| learnware_num = 5 | |||
| prepare_learnware(learnware_num) | |||
| # prepare_learnware(learnware_num) | |||
| # test_market() | |||
| # test_stat_search() | |||
| @@ -1,19 +0,0 @@ | |||
| import os | |||
| import joblib | |||
| import numpy as np | |||
| from learnware.model import BaseModel | |||
| class SVM(BaseModel): | |||
| def __init__(self): | |||
| dir_path = os.path.dirname(os.path.abspath(__file__)) | |||
| self.model = joblib.load(os.path.join(dir_path, "svm.pkl")) | |||
| def fit(self, X: np.ndarray, y: np.ndarray): | |||
| pass | |||
| def predict(self, X: np.ndarray) -> np.ndarray: | |||
| return self.model.predict(X) | |||
| def fintune(self, X: np.ndarray, y: np.ndarray): | |||
| pass | |||
| @@ -1,8 +0,0 @@ | |||
| model: | |||
| class_name: SVM | |||
| kwargs: {} | |||
| stat_specifications: | |||
| - module_path: learnware.specification | |||
| class_name: RKMEStatSpecification | |||
| file_name: svm.json | |||
| kwargs: {} | |||
| @@ -51,7 +51,7 @@ def get_learnware_from_dirpath(id: str, semantic_spec: dict, learnware_dirpath: | |||
| learnware_config["name"] = yaml_config["name"] | |||
| if "model" in yaml_config: | |||
| learnware_config["model"].update(yaml_config["model"]) | |||
| if "stats_specifications" in yaml_config: | |||
| if "stat_specifications" in yaml_config: | |||
| learnware_config["stat_specifications"] = yaml_config["stat_specifications"].copy() | |||
| if "module_path" not in learnware_config["model"]: | |||
| @@ -60,6 +60,7 @@ def get_learnware_from_dirpath(id: str, semantic_spec: dict, learnware_dirpath: | |||
| try: | |||
| learnware_spec = Specification() | |||
| for _stat_spec in learnware_config["stat_specifications"]: | |||
| _stat_spec["file_name"] = os.path.join(learnware_dirpath, _stat_spec["file_name"]) | |||
| stat_spac_name, stat_spec_inst = get_stat_spec_from_config(_stat_spec) | |||
| learnware_spec.update_stat_spec(**{stat_spac_name: stat_spec_inst}) | |||
| @@ -44,7 +44,7 @@ def get_model_from_config(model: Union[BaseModel, dict]) -> BaseModel: | |||
| def get_stat_spec_from_config(stat_spec: dict) -> BaseStatSpecification: | |||
| stat_spec_module = get_module_by_module_path(stat_spec["module_path"]) | |||
| stat_spec_inst = getattr(stat_spec_module, stat_spec["class_name"])(**stat_spec["kwargs"]) | |||
| if not isinstance(stat_spec_inst, BaseStatSpecification): | |||
| raise TypeError( | |||
| f"Statistic specification must be type of BaseStatSpecification, not {BaseStatSpecification.__class__.__name__}" | |||
| @@ -44,7 +44,7 @@ class EasyMarket(BaseMarket): | |||
| A flag indicating whether the learnware can be accepted. | |||
| """ | |||
| try: | |||
| spec_data = learnware.specification.stat_spec["RKME"].get_z() | |||
| spec_data = learnware.specification.stat_spec["RKMEStatSpecification"].get_z() | |||
| pred_spec = learnware.predict(spec_data) | |||
| except Exception: | |||
| logger.warning(f"The learnware [{learnware.id}-{learnware.name}] is not avaliable!") | |||
| @@ -88,7 +88,7 @@ class EasyMarket(BaseMarket): | |||
| """ | |||
| rkme_stat_spec = RKMEStatSpecification() | |||
| rkme_stat_spec.load(stat_spec_path) | |||
| stat_spec = {"RKME": rkme_stat_spec} | |||
| stat_spec = {"RKMEStatSpecification": rkme_stat_spec} | |||
| specification = Specification(semantic_spec=semantic_spec, stat_spec=stat_spec) | |||
| """ | |||
| @@ -154,7 +154,7 @@ class EasyMarket(BaseMarket): | |||
| The second is the mmd dist between the mixture of learnware rkmes and the user's rkme | |||
| """ | |||
| learnware_num = len(learnware_list) | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKME") for learnware in learnware_list] | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKMEStatSpecification") for learnware in learnware_list] | |||
| if type(intermediate_K) == np.ndarray: | |||
| K = intermediate_K | |||
| @@ -213,7 +213,7 @@ class EasyMarket(BaseMarket): | |||
| The second is the intermediate value of C | |||
| """ | |||
| num = intermediate_K.shape[0] - 1 | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKME") for learnware in learnware_list] | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKMEStatSpecification") for learnware in learnware_list] | |||
| for i in range(intermediate_K.shape[0]): | |||
| intermediate_K[num, i] = RKME_list[-1].inner_prod(RKME_list[i]) | |||
| intermediate_C[num, 0] = user_rkme.inner_prod(RKME_list[-1]) | |||
| @@ -297,7 +297,7 @@ class EasyMarket(BaseMarket): | |||
| the second is the list of Learnware | |||
| both lists are sorted by mmd dist | |||
| """ | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKME") for learnware in learnware_list] | |||
| RKME_list = [learnware.specification.get_stat_spec_by_name("RKMEStatSpecification") for learnware in learnware_list] | |||
| mmd_dist_list = [] | |||
| for RKME in RKME_list: | |||
| mmd_dist = RKME.dist(user_rkme) | |||
| @@ -369,10 +369,10 @@ class EasyMarket(BaseMarket): | |||
| learnware_list_description = self._search_by_semantic_description(learnware_list, user_info) | |||
| learnware_list = list(set(learnware_list_tags + learnware_list_description)) | |||
| if "RKME" not in user_info.stat_info: | |||
| if "RKMEStatSpecification" not in user_info.stat_info: | |||
| return None, learnware_list, None | |||
| else: | |||
| user_rkme = user_info.stat_info["RKME"] | |||
| user_rkme = user_info.stat_info["RKMEStatSpecification"] | |||
| sorted_dist_list, single_learnware_list = self._search_by_rkme_spec_single(learnware_list, user_rkme) | |||
| weight_list, mixture_learnware_list = self._search_by_rkme_spec_mixture( | |||
| learnware_list, user_rkme, search_num | |||