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.

t_solver.rst 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. .. _solver:
  2. AutoGL Solver
  3. =============
  4. AutoGL project use ``solver`` to handle the auto-solvation of tasks. Currently, we support the following tasks:
  5. * ``AutoNodeClassifier`` for semi-supervised node classification
  6. * ``AutoGraphClassifier`` for supervised graph classification
  7. * ``AutoLinkPredictor`` for link prediction
  8. Initialization
  9. --------------
  10. A solver can either be initialized from its ``__init__()`` or from a config dictionary or file.
  11. Initialize from ``__init__()``
  12. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. If you want to build a solver by ``__init__()``, you need to pass the key modules to it. You can either pass the keywords of corresponding modules or the initialized instances:
  14. .. code-block:: python
  15. from autogl.solver import AutoNodeClassifier
  16. # 1. initialize from keywords
  17. solver = AutoNodeClassifier(
  18. feature_module='deepgl',
  19. graph_models=['gat','gcn'],
  20. hpo_module='anneal',
  21. ensemble_module='voting',
  22. device='auto'
  23. )
  24. # 2. initialize using instances
  25. from autogl.module import AutoFeatureEngineer, AutoGCN, AutoGAT, AnnealAdvisorHPO, Voting
  26. solver = AutoNodeClassifier(
  27. feature_module=AutoFeatureEngineer(),
  28. graph_models=[AutoGCN(device='cuda'), AutoGAT(device='cuda')],
  29. hpo_module=AnnealAdvisorHPO(max_evals=10),
  30. ensemble_module=Voting(size=2),
  31. device='cuda'
  32. )
  33. Where, the argument ``device`` means where to perform the training and searching, by setting to ``auto``, the ``cuda`` is used when it is available.
  34. If you want to disable one module, you can set it to ``None``:
  35. .. code-block:: python
  36. solver = AutoNodeClassifier(feature_module=None, hpo_module=None, ensemble_module=None)
  37. You can also pass some important arguments of modules directly to solver, which will automatically be set for you:
  38. .. code-block:: python
  39. solver = AutoNodeClassifier(hpo_module='anneal', max_evals=10)
  40. Refer to :ref:`solver documentation` for more details of argument default value or important argument lists.
  41. Initialize from config dictionary or file
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. You can also initialize a solver directly from a config dictionary or file. Currently, the AutoGL solver supports config file type of ``yaml`` or ``json``. You need to use ``from_config()`` when you want to initialize in this way:
  44. .. code-block:: python
  45. # initialize from config file
  46. path_to_config = 'your/path/to/config'
  47. solver = AutoNodeClassifier.from_config(path_to_config)
  48. # initialize from a dictionary
  49. config = {
  50. 'models':{'gcn': None, 'gat': None},
  51. 'hpo': {'name': 'tpe', 'max_evals': 10},
  52. 'ensemble': {'name': 'voting', 'size': 2}
  53. }
  54. solver = AutoNodeClassifier.from_config(config)
  55. Refer to the config dictionary description :ref:`config` for more details.
  56. Optimization
  57. ------------
  58. After initializing a solver, you can optimize it on the given datasets (please refer to :ref:`dataset` and :ref:`dataset documentation` for creating datasets).
  59. You can use ``fit()`` or ``fit_predict()`` to perform optimization, which shares similar argument lists:
  60. .. code-block:: python
  61. # load your dataset here
  62. dataset = some_dataset()
  63. solver.fit(dataset, inplace=True)
  64. The inplace argument is used for saving memory if set to ``True``. It will modify your dataset in an inplace manner during feature engineering.
  65. You can also specify the ``train_split`` and ``val_split`` arguments to let solver auto-split the given dataset. If these arguments are given, the split dataset will be used instead of the default split specified by the dataset provided. All the models will be trained on ``train dataset``. Their hyperparameters will be optimized based on the performance of ``valid dataset``, as well as the final ensemble method. For example:
  66. .. code-block:: python
  67. # split 0.2 of total nodes/graphs for train and 0.4 of nodes/graphs for validation,
  68. # the rest 0.4 is left for test.
  69. solver.fit(dataset, train_split=0.2, val_split=0.4)
  70. # split 600 nodes/graphs for train and 400 nodes/graphs for validation,
  71. # the rest nodes are left for test.
  72. solver.fit(dataset, train_split=600, val_split=400)
  73. For the node classification problem, we also support balanced sampling of train and valid: force the number of sampled nodes in different classes to be the same. The balanced mode can be turned on by setting ``balanced=True`` in ``fit()``, which is by default set to ``True``.
  74. .. note:: Solver will maintain the models with the best hyper-parameter of each model architecture you pass to solver (the ``graph_models`` argument when initialized). The maintained models will then be ensembled by ensemble module.
  75. After ``fit()``, solver maintains the performances of every single model and the ensemble model in one leaderboard instance. You can output the performances on valid dataset by:
  76. .. code-block:: python
  77. # get current leaderboard of the solver
  78. lb = solver.get_leaderboard()
  79. # show the leaderboard info
  80. lb.show()
  81. You can refer to the leaderboard documentation in :ref:`solver documentation` for more usage.
  82. Prediction
  83. ----------
  84. After optimized on the given dataset, you can make predictions using the fitted ``solver``.
  85. Prediction using ensemble
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~
  87. You can use the ensemble model constructed by solver to make the prediction, which is recommended and is the default choice:
  88. .. code-block:: python
  89. solver.predict()
  90. If you do not pass any dataset, the dataset during fitting will be used to give the prediction.
  91. You can also pass the dataset when predicting, please make sure the ``inplaced`` argument is properly set.
  92. .. code-block:: python
  93. solver.predict(dataset, inplaced=True, inplace=True)
  94. The ``predict()`` function also has ``inplace`` argument, which is the same as the one in ``fit()``. As for the ``inplaced``, it means whether the passed dataset is already modified inplace or not (probably by ``fit()`` function). If you use ``fit()`` before, please make the ``inplaced`` of ``predict()`` stay the same with ``inplace`` in ``fit()``.
  95. Prediction using one single model
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. You can also make the prediction using the best single model the solver maintains by:
  98. .. code-block:: python
  99. solver.predict(use_ensemble=False, use_best=True)
  100. Also, you can name the single model maintained by solver to make predictions.
  101. .. code-block:: python
  102. solver.predict(use_ensemble=False, use_best=False, name=the_name_of_model)
  103. The names of models can be derived by calling ``solver.trained_models.keys()``, which is the same as the names maintained by the leaderboard of solver.
  104. .. note::
  105. By default, solver will only make predictions on the ``test`` split of given datasets. Please make sure the passed dataset has the ``test`` split when making predictions. You can also change the default prediction split by setting argument ``mask`` to ``train`` or ``valid``.
  106. Appendix
  107. --------
  108. .. _config:
  109. Config structure
  110. ~~~~~~~~~~~~~~~~
  111. The structure of the config file or config is introduced here. The config should be a dict, with five optional keys, namely ``feature``, ``models``, ``trainer``, ``hpo`` and ``ensemble``. You can simply do not add one field if you want to use the default option. The default value of each module is the same as the one in ``__init__()``.
  112. For key ``feature``, ``hpo`` and ``ensemble``, their corresponding values are all dictionaries, which contains one must key ``name`` and other arguments when initializing the corresponding modules. The value of key ``name`` specifies which algorithm should be used, where ``None`` can be passed if you do not want to enable the module. Other arguments are used to initialize the specified algorithm.
  113. For key ``trainer``, you should specify the hyperparameter space of trainer. See :ref:`trainer` or :ref:`train documentation` for the detailed hyperparameter space of different trainers.
  114. For key ``models``, the value is another dictionary with its keys being models that need optimized and the corresponding values being the hyperparameter space of that model. See :ref:`model` or :ref:`model documentation` for the detailed hyperparameter space of different models.
  115. Below shows some examples of the config dictionary.
  116. .. code-block:: python
  117. config_for_node_classification = {
  118. 'feature': {
  119. 'name': 'deepgl', # name of auto feature module
  120. # following are the deepgl specified auto feature engineer arguments
  121. 'fixlen': 100,
  122. 'max_epoch': 5
  123. },
  124. 'models': {
  125. 'gcn':
  126. # specify the hp space of gcn
  127. [
  128. {'parameterName': 'num_layers', 'type': 'DISCRETE', 'feasiblePoints': '2,3,4'},
  129. {'parameterName': 'hidden', 'type': 'NUMERICAL_LIST', 'numericalType': 'INTEGER', 'length': 3,
  130. 'minValue': [8, 8, 8], 'maxValue': [64, 64, 64], 'scalingType': 'LOG'},
  131. {'parameterName': 'dropout', 'type': 'DOUBLE', 'maxValue': 0.9, 'minValue': 0.1, 'scalingType': 'LINEAR'},
  132. {'parameterName': 'act', 'type': 'CATEGORICAL', 'feasiblePoints': ['leaky_relu', 'relu', 'elu', 'tanh']}
  133. ],
  134. 'gat': None, # set to None to use default hp space
  135. 'gin': None
  136. }
  137. 'trainer': [
  138. # trainer hp space
  139. {'parameterName': 'max_epoch', 'type': 'INTEGER', 'maxValue': 300, 'minValue': 10, 'scalingType': 'LINEAR'},
  140. {'parameterName': 'early_stopping_round', 'type': 'INTEGER', 'maxValue': 30, 'minValue': 10, 'scalingType': 'LINEAR'},
  141. {'parameterName': 'lr', 'type': 'DOUBLE', 'maxValue': 0.001, 'minValue': 0.0001, 'scalingType': 'LOG'},
  142. {'parameterName': 'weight_decay', 'type': 'DOUBLE', 'maxValue': 0.005, 'minValue': 0.0005, 'scalingType': 'LOG'}
  143. ],
  144. 'hpo': {
  145. 'name': 'autone', # name of hpo module
  146. # following are the autone specified auto hpo arguments
  147. 'max_evals': 10,
  148. 'subgraphs': 10,
  149. 'sub_evals': 5
  150. },
  151. 'ensemble': {
  152. 'name': 'voting', # name of ensemble module
  153. # following are the voting specified auto ensemble arguments
  154. 'size': 2
  155. }
  156. }
  157. config_for_graph_classification = {
  158. 'feature': None, # set to None to disable this module
  159. # do not add field `model` to use default settings of solver
  160. 'trainer': [
  161. # trainer hp space
  162. {'parameterName': 'max_epoch', 'type': 'INTEGER', 'maxValue': 300, 'minValue': 10, 'scalingType': 'LINEAR'},
  163. {'parameterName': 'batch_size', 'type': 'INTEGER', 'maxValue': 128, 'minValue': 32, 'scalingType': 'LOG'},
  164. {'parameterName': 'early_stopping_round', 'type': 'INTEGER', 'maxValue': 30, 'minValue': 10, 'scalingType': 'LINEAR'},
  165. {'parameterName': 'lr', 'type': 'DOUBLE', 'maxValue': 1e-3, 'minValue': 1e-4, 'scalingType': 'LOG'},
  166. {'parameterName': 'weight_decay', 'type': 'DOUBLE', 'maxValue': 5e-3, 'minValue': 5e-4, 'scalingType': 'LOG'},
  167. ],
  168. 'hpo': {
  169. 'name': 'random', # name of hpo module
  170. # following are the random specified auto hpo arguments
  171. 'max_evals': 10
  172. },
  173. 'ensemble': None # set to None to disable this module
  174. }