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.

client.rst 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ============================================================
  2. Learnware Client
  3. ============================================================
  4. Introduction
  5. ====================
  6. ``Learnware Client`` is a python api that provides a convenient interface for interacting with the official market. You can easily use the client to upload, download and search learnwares.
  7. Installation
  8. ====================
  9. ``Learnware Client`` is contained in the ``learnware`` package. You can install it using pip:
  10. .. code-block:: bash
  11. pip install learnware
  12. Prepare access token
  13. ====================
  14. Before using the ``Learnware Client``, you'll need to obtain a token from the `official website <https://www.lamda.nju.edu.cn/learnware/>`_. Just login to the website and click "client token" tab in the user center.
  15. Use Client
  16. ============================
  17. Initialize a Learware Client
  18. -------------------------------
  19. .. code-block:: python
  20. import learnware
  21. from learnware.client import LearnwareClient
  22. client = LearnwareClient()
  23. # login to official market
  24. client.login(email="your email", token="your token")
  25. Upload Leanware
  26. -------------------------------
  27. Before uploading a learnware, you'll need to prepare the semantic specification of your learnware. You can create a semantic specification by a helper function ``create_semantic_specification``.
  28. .. code-block:: python
  29. input_description = {
  30. "Dimension": 16,
  31. {
  32. "Description": {
  33. "0": "gender",
  34. "1": "age",
  35. "2": "f2",
  36. "5": "f5"
  37. }
  38. }
  39. }
  40. output_description = {
  41. "Dimension": 3,
  42. "Description": {
  43. "0": "the probability of being a cat",
  44. "1": "the probability of being a dog",
  45. "2": "the probability of being a bird"
  46. }
  47. }
  48. semantic_spec = client.create_semantic_specification(
  49. name="mylearnware1",
  50. description="this is my learnware",
  51. data_type="Table",
  52. task_type="Classification",
  53. library_type="Scikit-learn",
  54. senarioes=["Business", "Financial"],
  55. input_description, output_description)
  56. # data_type, task_type, library_type, senarioes are enums, you can find possible values in `learnware.C`
  57. After defining the semantic specification,
  58. you can upload your learnware using ``upload_learnware`` function:
  59. .. code-block:: python
  60. learnware_id = client.upload_learnware(
  61. semantic_spec=semantic_spec,
  62. zip_path="path to your learnware zipfile")
  63. Here, ``zip_path`` is the local path of your learnware zipfile.
  64. Semantic Specification Search
  65. -------------------------------
  66. You can search learnwares in official market using semantic specification. All the learnwares that match the semantic specification will be returned by the api. For example, the code below searches learnwares with `Table` data type:
  67. .. code-block:: python
  68. semantic_spec = client.create_semantic_specification(
  69. name="",
  70. description="",
  71. data_type="Table",
  72. task_type="",
  73. library_type="",
  74. senarioes=[],
  75. input_description={}, output_description={})
  76. specification = learnware.specification.Specification()
  77. specification.update_semantic_spec(specification)
  78. learnware_list = client.search_learnware(specification)
  79. Statistical Specification Search
  80. ---------------------------------
  81. You can search learnware by providing a statistical specification. The statistical specification is a json file that contains the statistical information of your training data. For example, the code below searches learnwares with `RKMETableSpecification`:
  82. .. code-block:: python
  83. import learnware.specification as specification
  84. user_spec = specification.RKMETableSpecification()
  85. user_spec.load(os.path.join(unzip_path, "rkme.json"))
  86. specification = learnware.specification.Specification()
  87. specification.update_stat_spec(user_spec)
  88. learnware_list = client.search_learnware(specification)
  89. # you can view the scores of the searched learnwares
  90. for learnware in learnware_list:
  91. print(f'learnware_id: {learnware["learnware_id"]}, score: {learnware["matching"]}')
  92. Combine Semantic and Statistical Search
  93. ----------------------------------------
  94. You can provide both semantic and statistical specification to search learnwares. The engine will first filter learnwares by semantic specification and then search by statistical specification. For example, the code below searches learnwares with `Table` data type and `RKMETableSpecification`:
  95. .. code-block:: python
  96. semantic_spec = client.create_semantic_specification(
  97. name="",
  98. description="",
  99. data_type="Table",
  100. task_type="",
  101. library_type="",
  102. senarioes=[],
  103. input_description={}, output_description={})
  104. stat_spec = specification.RKMETableSpecification()
  105. stat_spec.load(os.path.join(unzip_path, "rkme.json"))
  106. specification = learnware.specification.Specification()
  107. specification.update_semantic_spec(semantic_spec)
  108. specification.update_stat_spec(stat_spec)
  109. learnware_list = client.search_learnware(specification)
  110. Download and Use Learnware
  111. -------------------------------
  112. When you get a learnware id, you can download and initiate the learnware with the following code:
  113. .. code-block:: python
  114. client.download_learnware(learnware_id, zip_path)
  115. client.install_environment(zip_path)
  116. learnware = client.load_learnware(zip_path)
  117. # you can use the learnware to make prediction now