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.

mindspore.dataset.GraphData.rst 11 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. mindspore.dataset.GraphData
  2. ===========================
  3. .. py:class:: mindspore.dataset.GraphData(dataset_file, num_parallel_workers=None, working_mode='local', hostname='127.0.0.1', port=50051, num_client=1, auto_shutdown=True)
  4. 从共享文件或数据库中读取用于GNN训练的图数据集。
  5. **参数:**
  6. - **dataset_file** (str) - 数据集文件路径。
  7. - **num_parallel_workers** (int, 可选) - 读取数据的工作线程数,默认值:None,使用mindspore.dataset.config中配置的线程数。
  8. - **working_mode** (str, 可选) - 设置工作模式,目前支持'local'/'client'/'server',默认值:'local'。
  9. - **local**:用于非分布式训练场景。
  10. - **client**:用于分布式训练场景。客户端不加载数据,而是从服务器获取数据。
  11. - **server**:用于分布式训练场景。服务器加载数据并可供客户端使用。
  12. - **hostname** (str, 可选) - 图数据集服务器的主机名。该参数仅在工作模式设置为 'client' 或 'server' 时有效,默认值:'127.0.0.1'。
  13. - **port** (int, 可选) - 图数据服务器的端口,取值范围为1024-65535。此参数仅当工作模式设置为 'client' 或 'server' 时有效,默认值:50051。
  14. - **num_client** (int, 可选) - 期望连接到服务器的最大客户端数。服务器将根据该参数分配资源。该参数仅在工作模式设置为 'server' 时有效,默认值:1。
  15. - **auto_shutdown** (bool, 可选) - 当工作模式设置为 'server' 时有效。当连接的客户端数量达到 `num_client` ,且没有客户端正在连接时,服务器将自动退出,默认值:True。
  16. .. py:method:: get_all_edges(edge_type)
  17. 获取图的所有边。
  18. **参数:**
  19. - **edge_type** (int) - 指定边的类型,在数据集转换为MindRecord格式时,需要指定 `edge_type` 的值,并在此API中对应使用。详见 `加载图数据集 <https://www.mindspore.cn/docs/programming_guide/zh-CN/master/load_dataset_gnn.html>`_ 。
  20. **返回:**
  21. numpy.ndarray,包含边的数组。
  22. **异常:**
  23. **TypeError**:参数 `edge_type` 的类型不为整型。
  24. .. py:method:: get_all_neighbors(node_list, neighbor_type, output_format=OutputFormat.NORMAL)
  25. 获取 `node_list` 所有节点的相邻节点,以 `neighbor_type` 类型返回。格式的定义参见以下示例:1表示两个节点之间连接,0表示不连接。
  26. .. list-table:: 邻接矩阵
  27. :widths: 20 20 20 20 20
  28. :header-rows: 1
  29. * -
  30. - 0
  31. - 1
  32. - 2
  33. - 3
  34. * - 0
  35. - 0
  36. - 1
  37. - 0
  38. - 0
  39. * - 1
  40. - 0
  41. - 0
  42. - 1
  43. - 0
  44. * - 2
  45. - 1
  46. - 0
  47. - 0
  48. - 1
  49. * - 3
  50. - 1
  51. - 0
  52. - 0
  53. - 0
  54. .. list-table:: 普通格式
  55. :widths: 20 20 20 20 20
  56. :header-rows: 1
  57. * - src
  58. - 0
  59. - 1
  60. - 2
  61. - 3
  62. * - dst_0
  63. - 1
  64. - 2
  65. - 0
  66. - 1
  67. * - dst_1
  68. - -1
  69. - -1
  70. - 3
  71. - -1
  72. .. list-table:: COO格式
  73. :widths: 20 20 20 20 20 20
  74. :header-rows: 1
  75. * - src
  76. - 0
  77. - 1
  78. - 2
  79. - 2
  80. - 3
  81. * - dst
  82. - 1
  83. - 2
  84. - 0
  85. - 3
  86. - 1
  87. .. list-table:: CSR格式
  88. :widths: 40 20 20 20 20 20
  89. :header-rows: 1
  90. * - offsetTable
  91. - 0
  92. - 1
  93. - 2
  94. - 4
  95. -
  96. * - dstTable
  97. - 1
  98. - 2
  99. - 0
  100. - 3
  101. - 1
  102. **参数:**
  103. - **node_list** (Union[list, numpy.ndarray]) - 给定的节点列表。
  104. - **neighbor_type** (int) - 指定相邻节点的类型。
  105. - **output_format** (OutputFormat, 可选) - 输出存储格式,默认值:mindspore.dataset.OutputFormat.NORMAL,取值范围:[OutputFormat.NORMAL, OutputFormat.COO, OutputFormat.CSR]。
  106. **返回:**
  107. 对于普通格式或COO格式,将返回numpy.ndarray类型的数组表示相邻节点。如果指定了CSR格式,将返回两个numpy.ndarray数组,第一个表示偏移表,第二个表示相邻节点。
  108. **异常:**
  109. - **TypeError** - 参数 `node_list` 的类型不为列表或numpy.ndarray。
  110. - **TypeError** - 参数 `neighbor_type` 的类型不为整型。
  111. .. py:method:: get_all_nodes(node_type)
  112. 获取图中的所有节点。
  113. **参数:**
  114. - **node_type** (int) - 指定节点的类型。在数据集转换为MindRecord格式时,需要指定 `node_type` 的值,并在此API中对应使用。详见 `加载图数据集 <https://www.mindspore.cn/docs/programming_guide/zh-CN/master/load_dataset_gnn.html>`_ 。
  115. **返回:**
  116. numpy.ndarray,包含节点的数组。
  117. **异常:**
  118. **TypeError**:参数 `node_type` 的类型不为整型。
  119. .. py:method:: get_edges_from_nodes(node_list)
  120. 从节点获取边。
  121. **参数:**
  122. - **node_list** (Union[list[tuple], numpy.ndarray]) - 含一个或多个图节点ID对的列表。
  123. **返回:**
  124. numpy.ndarray,含一个或多个边ID的数组。
  125. **异常:**
  126. **TypeError**:参数 `edge_list` 的类型不为列表或numpy.ndarray。
  127. .. py:method:: get_edge_feature(edge_list, feature_types)
  128. 获取 `edge_list` 列表中边的特征,以 `feature_types` 类型返回。
  129. **参数:**
  130. - **edge_list** (Union[list, numpy.ndarray]) - 包含边的列表。
  131. - **feature_types** (Union[list, numpy.ndarray]) - 包含给定特征类型的列表。
  132. **返回:**
  133. numpy.ndarray,包含特征的数组。
  134. **异常:**
  135. - **TypeError** - 参数 `edge_list` 的类型不为列表或numpy.ndarray。
  136. - **TypeError** - 参数 `feature_types` 的类型不为列表或numpy.ndarray。
  137. .. py:method:: get_neg_sampled_neighbors(node_list, neg_neighbor_num, neg_neighbor_type)
  138. 获取 `node_list` 列表中节所有点的负样本相邻节点,以 `neg_neighbor_type` 类型返回。
  139. **参数:**
  140. - **node_list** (Union[list, numpy.ndarray]) - 包含节点的列表。
  141. - **neg_neighbor_num** (int) - 采样的相邻节点数量。
  142. - **neg_neighbor_type** (int) - 指定负样本相邻节点的类型。
  143. **返回:**
  144. numpy.ndarray,包含相邻节点的数组。
  145. **异常:**
  146. - **TypeError** - 参数 `node_list` 的类型不为列表或numpy.ndarray。
  147. - **TypeError** - 参数 `neg_neighbor_num` 的类型不为整型。
  148. - **TypeError** - 参数 `neg_neighbor_type` 的类型不为整型。
  149. .. py:method:: get_nodes_from_edges(edge_list)
  150. 从图中的边获取节点。
  151. **参数:**
  152. - **edge_list** (Union[list, numpy.ndarray]) - 包含边的列表。
  153. **返回:**
  154. numpy.ndarray,包含节点的数组。
  155. **异常:**
  156. **TypeError:** 参数 `edge_list` 不为列表或ndarray。
  157. .. py:method:: get_node_feature(node_list, feature_types)
  158. 获取 `node_list` 中节点的特征,以 `feature_types` 类型返回。
  159. **参数:**
  160. - **node_list** (Union[list, numpy.ndarray]) - 包含节点的列表。
  161. - **feature_types** (Union[list, numpy.ndarray]) - 指定特征的类型。
  162. **返回:**
  163. numpy.ndarray,包含特征的数组。
  164. **异常:**
  165. - **TypeError** - 参数 `node_list` 的类型不为列表或numpy.ndarray。
  166. - **TypeError** - 参数 `feature_types` 的类型不为列表或numpy.ndarray。
  167. .. py:method:: get_sampled_neighbors(node_list, neighbor_nums, neighbor_types, strategy=SamplingStrategy.RANDOM)
  168. 获取已采样相邻节点信息。此API支持多跳相邻节点采样。即将上一次采样结果作为下一跳采样的输入,最多允许6跳。采样结果平铺成列表,格式为[input node, 1-hop sampling result, 2-hop samling result ...]
  169. **参数:**
  170. - **node_list** (Union[list, numpy.ndarray]) - 包含节点的列表。
  171. - **neighbor_nums** (Union[list, numpy.ndarray]) - 每跳采样的相邻节点数。
  172. - **neighbor_types** (Union[list, numpy.ndarray]) - 每跳采样的相邻节点类型。
  173. - **strategy** (SamplingStrategy, 可选) - 采样策略,默认值:mindspore.dataset.SamplingStrategy.RANDOM。取值范围:[SamplingStrategy.RANDOM, SamplingStrategy.EDGE_WEIGHT]。
  174. - **SamplingStrategy.RANDOM**:随机抽样,带放回采样。
  175. - **SamplingStrategy.EDGE_WEIGHT**:以边缘权重为概率进行采样。
  176. **返回:**
  177. numpy.ndarray,包含相邻节点的数组。
  178. **异常:**
  179. - **TypeError** - 参数 `node_list` 的类型不为列表或numpy.ndarray。
  180. - **TypeError** - 参数 `neighbor_nums` 的类型不为列表或numpy.ndarray。
  181. - **TypeError** - 参数 `neighbor_types` 的类型不为列表或numpy.ndarray。
  182. .. py:method:: graph_info()
  183. 获取图的元信息,包括节点数、节点类型、节点特征信息、边数、边类型、边特征信息。
  184. **返回:**
  185. dict,图的元信息。键为 `node_num` 、 `node_type` 、 `node_feature_type` 、 `edge_num` 、 `edge_type` 和 `edge_feature_type` 。
  186. .. py:method:: random_walk(target_nodes, meta_path, step_home_param=1.0, step_away_param=1.0, default_node=-1)
  187. 在节点中的随机游走。
  188. **参数:**
  189. - **target_nodes** (list[int]) - 随机游走中的起始节点列表。
  190. - **meta_path** (list[int]) - 每个步长的节点类型。
  191. - **step_home_param** (float, 可选) - 返回 `node2vec算法 <https://www.kdd.org/kdd2016/papers/files/rfp0218-groverA.pdf>`_ 中的超参,默认值:1.0。
  192. - **step_away_param** (float, 可选) - `node2vec算法 <https://www.kdd.org/kdd2016/papers/files/rfp0218-groverA.pdf>`_ 中的in和out超参,默认值:1.0。
  193. - **default_node** (int, 可选) - 如果找不到更多相邻节点,则为默认节点,默认值:-1,表示不给定节点。
  194. **返回:**
  195. numpy.ndarray,包含节点的数组。
  196. **异常:**
  197. - **TypeError** - 参数 `target_nodes` 的类型不为列表或numpy.ndarray。
  198. - **TypeError** - 参数 `meta_path` 的类型不为列表或numpy.ndarray。