Browse Source

[MNT] rename RKMETextSpecification

tags/v0.3.2
nju-xy 2 years ago
parent
commit
64f59c92b1
11 changed files with 34 additions and 34 deletions
  1. +2
    -2
      learnware/market/easy2/checker.py
  2. +4
    -4
      learnware/market/easy2/searcher.py
  3. +6
    -6
      learnware/reuse/job_selector.py
  4. +1
    -1
      learnware/specification/__init__.py
  5. +1
    -1
      learnware/specification/regular/__init__.py
  6. +1
    -1
      learnware/specification/regular/text/__init__.py
  7. +2
    -2
      learnware/specification/regular/text/rkme.py
  8. +6
    -6
      learnware/specification/utils.py
  9. +4
    -4
      tests/test_specification/test_rkme.py
  10. +1
    -1
      tests/test_text_workflow/example_files/example_yaml.yaml
  11. +6
    -6
      tests/test_text_workflow/main.py

+ 2
- 2
learnware/market/easy2/checker.py View File

@@ -82,9 +82,9 @@ class EasyStatisticalChecker(BaseChecker):
input_shape = learnware_model.input_shape

# Check rkme dimension
is_text = "RKMETextStatSpecification" in learnware.get_specification().stat_spec
is_text = "RKMETextSpecification" in learnware.get_specification().stat_spec
if is_text:
stat_spec = learnware.get_specification().get_stat_spec_by_name("RKMETextStatSpecification")
stat_spec = learnware.get_specification().get_stat_spec_by_name("RKMETextSpecification")
else:
stat_spec = learnware.get_specification().get_stat_spec_by_name("RKMETableSpecification")
if stat_spec is not None and not is_text:


+ 4
- 4
learnware/market/easy2/searcher.py View File

@@ -438,7 +438,7 @@ class EasyStatSearcher(BaseSearcher):
if self.stat_info_name not in learnware.specification.stat_spec:
continue
rkme = learnware.specification.get_stat_spec_by_name(self.stat_info_name)
if self.stat_info_name == "RKMETextStatSpecification":
if self.stat_info_name == "RKMETextSpecification":
if not set(user_rkme.language).issubset(set(rkme.language)):
continue
rkme_dim = str(list(rkme.get_z().shape)[1:])
@@ -557,8 +557,8 @@ class EasyStatSearcher(BaseSearcher):
max_search_num: int = 5,
search_method: str = "greedy",
) -> Tuple[List[float], List[Learnware], float, List[Learnware]]:
if "RKMETextStatSpecification" in user_info.stat_info:
self.stat_info_name = "RKMETextStatSpecification"
if "RKMETextSpecification" in user_info.stat_info:
self.stat_info_name = "RKMETextSpecification"
else:
self.stat_info_name = "RKMETableSpecification"
user_rkme = user_info.stat_info[self.stat_info_name]
@@ -636,7 +636,7 @@ class EasySearcher(BaseSearcher):
return [], [], 0.0, []
elif "RKMETableSpecification" in user_info.stat_info:
return self.stat_searcher(learnware_list, user_info, max_search_num, search_method)
elif "RKMETextStatSpecification" in user_info.stat_info:
elif "RKMETextSpecification" in user_info.stat_info:
return self.stat_searcher(learnware_list, user_info, max_search_num, search_method)
else:
return None, learnware_list, 0.0, None

+ 6
- 6
learnware/reuse/job_selector.py View File

@@ -9,7 +9,7 @@ from sklearn.metrics import accuracy_score
from learnware.learnware import Learnware
import learnware.specification as specification
from .base import BaseReuser
from ..specification import RKMETableSpecification, RKMETextStatSpecification
from ..specification import RKMETableSpecification, RKMETextSpecification
from ..logger import get_module_logger

logger = get_module_logger("job_selector_reuse")
@@ -47,7 +47,7 @@ class JobSelectorReuser(BaseReuser):
"""
ori_user_data = user_data
if isinstance(user_data[0], str):
user_data = RKMETextStatSpecification.get_sentence_embedding(user_data)
user_data = RKMETextSpecification.get_sentence_embedding(user_data)

select_result = self.job_selector(user_data)
pred_y_list = []
@@ -93,10 +93,10 @@ class JobSelectorReuser(BaseReuser):
else:
ori_user_data = user_data
if isinstance(user_data[0], str):
user_data = RKMETextStatSpecification.get_sentence_embedding(user_data)
user_data = RKMETextSpecification.get_sentence_embedding(user_data)
spec_name = "RKMETableSpecification"
if len(self.learnware_list) and "RKMETextStatSpecification" in self.learnware_list[0].specification.stat_spec:
spec_name = "RKMETextStatSpecification"
if len(self.learnware_list) and "RKMETextSpecification" in self.learnware_list[0].specification.stat_spec:
spec_name = "RKMETextSpecification"
learnware_rkme_spec_list = [
learnware.specification.get_stat_spec_by_name(spec_name) for learnware in self.learnware_list
]
@@ -180,7 +180,7 @@ class JobSelectorReuser(BaseReuser):
"""
task_num = len(task_rkme_list)
if isinstance(user_data[0], str):
user_data = RKMETextStatSpecification.get_sentence_embedding(user_data)
user_data = RKMETextSpecification.get_sentence_embedding(user_data)
user_rkme_spec = specification.utils.generate_rkme_spec(X=user_data, reduce=False)
K = task_rkme_matrix
v = np.array([user_rkme_spec.inner_prod(task_rkme) for task_rkme in task_rkme_list])


+ 1
- 1
learnware/specification/__init__.py View File

@@ -1,3 +1,3 @@
from .utils import generate_stat_spec, generate_rkme_spec, generate_rkme_image_spec
from .base import Specification, BaseStatSpecification
from .regular import RegularStatsSpecification, RKMEStatSpecification, RKMETableSpecification, RKMEImageSpecification
from .regular import RegularStatsSpecification, RKMEStatSpecification, RKMETableSpecification, RKMEImageSpecification, RKMETextSpecification

+ 1
- 1
learnware/specification/regular/__init__.py View File

@@ -1,4 +1,4 @@
from .text import RKMETextStatSpecification
from .text import RKMETextSpecification
from .table import RKMETableSpecification, RKMEStatSpecification
from .image import RKMEImageSpecification
from .base import RegularStatsSpecification

+ 1
- 1
learnware/specification/regular/text/__init__.py View File

@@ -1 +1 @@
from .rkme import RKMETextStatSpecification
from .rkme import RKMETextSpecification

+ 2
- 2
learnware/specification/regular/text/rkme.py View File

@@ -5,10 +5,10 @@ import os
import langdetect
from ....logger import get_module_logger

logger = get_module_logger("RKMETextStatSpecification", "INFO")
logger = get_module_logger("RKMETextSpecification", "INFO")


class RKMETextStatSpecification(RKMETableSpecification):
class RKMETextSpecification(RKMETableSpecification):
"""Reduced Kernel Mean Embedding (RKME) Specification for Text"""
def __init__(self, gamma: float = 0.1, cuda_idx: int = -1):
RKMETableSpecification.__init__(self, gamma, cuda_idx)


+ 6
- 6
learnware/specification/utils.py View File

@@ -4,7 +4,7 @@ import pandas as pd
from typing import Union, List

from .base import BaseStatSpecification
from .regular import RKMETableSpecification, RKMEImageSpecification, RKMETextStatSpecification
from .regular import RKMETableSpecification, RKMEImageSpecification, RKMETextSpecification
from ..config import C


@@ -173,10 +173,10 @@ def generate_rkme_text_spec(
nonnegative_beta: bool = True,
reduce: bool = True,
cuda_idx: int = None,
) -> RKMETextStatSpecification:
) -> RKMETextSpecification:
"""
Interface for users to generate Reduced Kernel Mean Embedding (RKME) specification for Text.
Return a RKMETextStatSpecification object, use .save() method to save as json file.
Return a RKMETextSpecification object, use .save() method to save as json file.

Parameters
----------
@@ -200,8 +200,8 @@ def generate_rkme_text_spec(

Returns
-------
RKMETextStatSpecification
A RKMETextStatSpecification object
RKMETextSpecification
A RKMETextSpecification object
"""
# Check input type
if not isinstance(X, list) or not all(isinstance(item, str) for item in X):
@@ -216,7 +216,7 @@ def generate_rkme_text_spec(
cuda_idx = 0

# Generate rkme text spec
rkme_text_spec = RKMETextStatSpecification(gamma=gamma, cuda_idx=cuda_idx)
rkme_text_spec = RKMETextSpecification(gamma=gamma, cuda_idx=cuda_idx)
rkme_text_spec.generate_stat_spec_from_data(X, reduced_set_size, step_size, steps, nonnegative_beta, reduce)
return rkme_text_spec



+ 4
- 4
tests/test_specification/test_rkme.py View File

@@ -8,7 +8,7 @@ import tempfile
import numpy as np

import learnware.specification as specification
from learnware.specification import RKMETableSpecification, RKMEImageSpecification, RKMETextStatSpecification
from learnware.specification import RKMETableSpecification, RKMEImageSpecification, RKMETextSpecification
from learnware.specification import generate_rkme_image_spec, generate_rkme_spec


@@ -79,11 +79,11 @@ class TestRKME(unittest.TestCase):

with open(rkme_path, "r") as f:
data = json.load(f)
assert data["type"] == "RKMETextStatSpecification"
assert data["type"] == "RKMETextSpecification"

rkme2 = RKMETextStatSpecification()
rkme2 = RKMETextSpecification()
rkme2.load(rkme_path)
assert rkme2.type == "RKMETextStatSpecification"
assert rkme2.type == "RKMETextSpecification"

return rkme2.get_z().shape[1]



+ 1
- 1
tests/test_text_workflow/example_files/example_yaml.yaml View File

@@ -3,6 +3,6 @@ model:
kwargs: {}
stat_specifications:
- module_path: learnware.specification
class_name: RKMETextStatSpecification
class_name: RKMETextSpecification
file_name: rkme.json
kwargs: {}

+ 6
- 6
tests/test_text_workflow/main.py View File

@@ -100,7 +100,7 @@ def prepare_learnware(data_path, model_path, init_file_path, yaml_path, save_roo

st = time.time()
# user_spec = specification.utils.generate_rkme_spec(X=X, gamma=0.1, cuda_idx=0)
user_spec = specification.RKMETextStatSpecification()
user_spec = specification.RKMETextSpecification()
user_spec.generate_stat_spec_from_data(X=X)
ed = time.time()
logger.info("Stat spec generated in %.3f s" % (ed - st))
@@ -166,9 +166,9 @@ def test_search(gamma=0.1, load_market=True):
# user_data = np.load(user_data_path)
# user_label = np.load(user_label_path)
# user_stat_spec = specification.utils.generate_rkme_spec(X=user_data, gamma=gamma, cuda_idx=0)
user_stat_spec = specification.RKMETextStatSpecification()
user_stat_spec = specification.RKMETextSpecification()
user_stat_spec.generate_stat_spec_from_data(X=user_data)
user_info = BaseUserInfo(semantic_spec=user_semantic, stat_info={"RKMETextStatSpecification": user_stat_spec})
user_info = BaseUserInfo(semantic_spec=user_semantic, stat_info={"RKMETextSpecification": user_stat_spec})
logger.info("Searching Market for user: %d" % (i))
sorted_score_list, single_learnware_list, mixture_score, mixture_learnware_list = text_market.search_learnware(
user_info
@@ -232,6 +232,6 @@ def test_search(gamma=0.1, load_market=True):


if __name__ == "__main__":
# prepare_data()
# prepare_model()
test_search(load_market=True)
prepare_data()
prepare_model()
test_search(load_market=False)

Loading…
Cancel
Save