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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. Prepare access token
  8. ====================
  9. 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.
  10. How to Use Client
  11. ============================
  12. Initialize a Learware Client
  13. -------------------------------
  14. .. code-block:: python
  15. import learnware
  16. from learnware.client import LearnwareClient
  17. client = LearnwareClient()
  18. # login to official market
  19. client.login(email="your email", token="your token")
  20. Upload Leanware
  21. -------------------------------
  22. 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``.
  23. .. code-block:: python
  24. input_description = {
  25. "Dimension": 16,
  26. {
  27. "Description": {
  28. "0": "gender",
  29. "1": "age",
  30. "2": "f2",
  31. "5": "f5"
  32. }
  33. }
  34. }
  35. output_description = {
  36. "Dimension": 3,
  37. "Description": {
  38. "0": "the probability of being a cat",
  39. "1": "the probability of being a dog",
  40. "2": "the probability of being a bird"
  41. }
  42. }
  43. semantic_spec = client.create_semantic_specification(
  44. name="mylearnware1",
  45. description="this is my learnware",
  46. data_type="Table",
  47. task_type="Classification",
  48. library_type="Scikit-learn",
  49. senarioes=["Business", "Financial"],
  50. input_description, output_description)
  51. # data_type, task_type, library_type, senarioes are enums, you can find possible values in `learnware.C`
  52. After defining the semantic specification,
  53. you can upload your learnware using ``upload_learnware`` function:
  54. .. code-block:: python
  55. learnware_id = client.upload_learnware(
  56. semantic_spec=semantic_spec,
  57. zip_path="path to your learnware zipfile")
  58. Here, ``zip_path`` is the local path of your learnware zipfile.
  59. Semantic Specification Search
  60. -------------------------------
  61. 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:
  62. .. code-block:: python
  63. semantic_spec = client.create_semantic_specification(
  64. name="",
  65. description="",
  66. data_type="Table",
  67. task_type="",
  68. library_type="",
  69. senarioes=[],
  70. input_description={}, output_description={})
  71. specification = learnware.specification.Specification()
  72. specification.update_semantic_spec(specification)
  73. learnware_list = client.search_learnware(specification)
  74. Statistical Specification Search
  75. ---------------------------------
  76. 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`:
  77. .. code-block:: python
  78. import learnware.specification as specification
  79. user_spec = specification.RKMETableSpecification()
  80. user_spec.load(os.path.join(unzip_path, "rkme.json"))
  81. specification = learnware.specification.Specification()
  82. specification.update_stat_spec(user_spec)
  83. learnware_list = client.search_learnware(specification)
  84. # you can view the scores of the searched learnwares
  85. for learnware in learnware_list:
  86. print(f'learnware_id: {learnware["learnware_id"]}, score: {learnware["matching"]}')
  87. Combine Semantic and Statistical Search
  88. ----------------------------------------
  89. 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`:
  90. .. code-block:: python
  91. semantic_spec = client.create_semantic_specification(
  92. name="",
  93. description="",
  94. data_type="Table",
  95. task_type="",
  96. library_type="",
  97. senarioes=[],
  98. input_description={}, output_description={})
  99. stat_spec = specification.RKMETableSpecification()
  100. stat_spec.load(os.path.join(unzip_path, "rkme.json"))
  101. specification = learnware.specification.Specification()
  102. specification.update_semantic_spec(semantic_spec)
  103. specification.update_stat_spec(stat_spec)
  104. learnware_list = client.search_learnware(specification)
  105. Download and Use Learnware
  106. -------------------------------
  107. When you get a learnware id, you can download and initiate the learnware with the following code:
  108. .. code-block:: python
  109. client.download_learnware(learnware_id, zip_path)
  110. client.install_environment(zip_path)
  111. learnware = client.load_learnware(zip_path)
  112. # you can use the learnware to make prediction now