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.

gnn_graph_test.cc 8.5 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <string>
  18. #include <memory>
  19. #include <unordered_set>
  20. #include "common/common.h"
  21. #include "gtest/gtest.h"
  22. #include "dataset/util/status.h"
  23. #include "dataset/engine/gnn/node.h"
  24. #include "dataset/engine/gnn/graph_loader.h"
  25. using namespace mindspore::dataset;
  26. using namespace mindspore::dataset::gnn;
  27. #define print_int_vec(_i, _str) \
  28. do { \
  29. std::stringstream ss; \
  30. std::copy(_i.begin(), _i.end(), std::ostream_iterator<int>(ss, " ")); \
  31. MS_LOG(INFO) << _str << " " << ss.str(); \
  32. } while (false)
  33. class MindDataTestGNNGraph : public UT::Common {
  34. protected:
  35. MindDataTestGNNGraph() = default;
  36. };
  37. TEST_F(MindDataTestGNNGraph, TestGraphLoader) {
  38. std::string path = "data/mindrecord/testGraphData/testdata";
  39. GraphLoader gl(path, 4);
  40. EXPECT_TRUE(gl.InitAndLoad().IsOk());
  41. NodeIdMap n_id_map;
  42. EdgeIdMap e_id_map;
  43. NodeTypeMap n_type_map;
  44. EdgeTypeMap e_type_map;
  45. NodeFeatureMap n_feature_map;
  46. EdgeFeatureMap e_feature_map;
  47. DefaultFeatureMap default_feature_map;
  48. EXPECT_TRUE(gl.GetNodesAndEdges(&n_id_map, &e_id_map, &n_type_map, &e_type_map, &n_feature_map, &e_feature_map,
  49. &default_feature_map)
  50. .IsOk());
  51. EXPECT_EQ(n_id_map.size(), 20);
  52. EXPECT_EQ(e_id_map.size(), 40);
  53. EXPECT_EQ(n_type_map[2].size(), 10);
  54. EXPECT_EQ(n_type_map[1].size(), 10);
  55. }
  56. TEST_F(MindDataTestGNNGraph, TestGetAllNeighbors) {
  57. std::string path = "data/mindrecord/testGraphData/testdata";
  58. Graph graph(path, 1);
  59. Status s = graph.Init();
  60. EXPECT_TRUE(s.IsOk());
  61. MetaInfo meta_info;
  62. s = graph.GetMetaInfo(&meta_info);
  63. EXPECT_TRUE(s.IsOk());
  64. EXPECT_TRUE(meta_info.node_type.size() == 2);
  65. std::shared_ptr<Tensor> nodes;
  66. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  67. EXPECT_TRUE(s.IsOk());
  68. std::vector<NodeIdType> node_list;
  69. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  70. node_list.push_back(*itr);
  71. if (node_list.size() >= 10) {
  72. break;
  73. }
  74. }
  75. std::shared_ptr<Tensor> neighbors;
  76. s = graph.GetAllNeighbors(node_list, meta_info.node_type[1], &neighbors);
  77. EXPECT_TRUE(s.IsOk());
  78. EXPECT_TRUE(neighbors->shape().ToString() == "<10,6>");
  79. TensorRow features;
  80. s = graph.GetNodeFeature(nodes, meta_info.node_feature_type, &features);
  81. EXPECT_TRUE(s.IsOk());
  82. EXPECT_TRUE(features.size() == 4);
  83. EXPECT_TRUE(features[0]->shape().ToString() == "<10,5>");
  84. EXPECT_TRUE(features[0]->ToString() ==
  85. "Tensor (shape: <10,5>, Type: int32)\n"
  86. "[[0,1,0,0,0],[1,0,0,0,1],[0,0,1,1,0],[0,0,0,0,0],[1,1,0,1,0],[0,0,0,0,1],[0,1,0,0,0],[0,0,0,1,1],[0,1,1,"
  87. "0,0],[0,1,0,1,0]]");
  88. EXPECT_TRUE(features[1]->shape().ToString() == "<10>");
  89. EXPECT_TRUE(features[1]->ToString() ==
  90. "Tensor (shape: <10>, Type: float32)\n[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]");
  91. EXPECT_TRUE(features[2]->shape().ToString() == "<10>");
  92. EXPECT_TRUE(features[2]->ToString() == "Tensor (shape: <10>, Type: int32)\n[1,2,3,1,4,3,5,3,5,4]");
  93. }
  94. TEST_F(MindDataTestGNNGraph, TestGetSampledNeighbors) {
  95. std::string path = "data/mindrecord/testGraphData/testdata";
  96. Graph graph(path, 1);
  97. Status s = graph.Init();
  98. EXPECT_TRUE(s.IsOk());
  99. MetaInfo meta_info;
  100. s = graph.GetMetaInfo(&meta_info);
  101. EXPECT_TRUE(s.IsOk());
  102. EXPECT_TRUE(meta_info.node_type.size() == 2);
  103. std::shared_ptr<Tensor> edges;
  104. s = graph.GetAllEdges(meta_info.edge_type[0], &edges);
  105. EXPECT_TRUE(s.IsOk());
  106. std::vector<EdgeIdType> edge_list;
  107. edge_list.resize(edges->Size());
  108. std::transform(edges->begin<EdgeIdType>(), edges->end<EdgeIdType>(), edge_list.begin(),
  109. [](const EdgeIdType edge) { return edge; });
  110. std::shared_ptr<Tensor> nodes;
  111. s = graph.GetNodesFromEdges(edge_list, &nodes);
  112. EXPECT_TRUE(s.IsOk());
  113. std::unordered_set<NodeIdType> node_set;
  114. std::vector<NodeIdType> node_list;
  115. int index = 0;
  116. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  117. index++;
  118. if (index % 2 == 0) {
  119. continue;
  120. }
  121. node_set.emplace(*itr);
  122. if (node_set.size() >= 5) {
  123. break;
  124. }
  125. }
  126. node_list.resize(node_set.size());
  127. std::transform(node_set.begin(), node_set.end(), node_list.begin(), [](const NodeIdType node) { return node; });
  128. std::shared_ptr<Tensor> neighbors;
  129. s = graph.GetSampledNeighbors(node_list, {10}, {meta_info.node_type[1]}, &neighbors);
  130. EXPECT_TRUE(s.IsOk());
  131. EXPECT_TRUE(neighbors->shape().ToString() == "<5,11>");
  132. neighbors.reset();
  133. s = graph.GetSampledNeighbors(node_list, {2, 3}, {meta_info.node_type[1], meta_info.node_type[0]}, &neighbors);
  134. EXPECT_TRUE(s.IsOk());
  135. EXPECT_TRUE(neighbors->shape().ToString() == "<5,9>");
  136. neighbors.reset();
  137. s = graph.GetSampledNeighbors(node_list, {2, 3, 4},
  138. {meta_info.node_type[1], meta_info.node_type[0], meta_info.node_type[1]}, &neighbors);
  139. EXPECT_TRUE(s.IsOk());
  140. EXPECT_TRUE(neighbors->shape().ToString() == "<5,33>");
  141. neighbors.reset();
  142. s = graph.GetSampledNeighbors({}, {10}, {meta_info.node_type[1]}, &neighbors);
  143. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  144. neighbors.reset();
  145. s = graph.GetSampledNeighbors(node_list, {2, 3, 4}, {meta_info.node_type[1], meta_info.node_type[0]}, &neighbors);
  146. EXPECT_TRUE(s.ToString().find("The sizes of neighbor_nums and neighbor_types are inconsistent.") !=
  147. std::string::npos);
  148. neighbors.reset();
  149. s = graph.GetSampledNeighbors({301}, {10}, {meta_info.node_type[1]}, &neighbors);
  150. EXPECT_TRUE(s.ToString().find("Invalid node id:301") != std::string::npos);
  151. }
  152. TEST_F(MindDataTestGNNGraph, TestGetNegSampledNeighbors) {
  153. std::string path = "data/mindrecord/testGraphData/testdata";
  154. Graph graph(path, 1);
  155. Status s = graph.Init();
  156. EXPECT_TRUE(s.IsOk());
  157. MetaInfo meta_info;
  158. s = graph.GetMetaInfo(&meta_info);
  159. EXPECT_TRUE(s.IsOk());
  160. EXPECT_TRUE(meta_info.node_type.size() == 2);
  161. std::shared_ptr<Tensor> nodes;
  162. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  163. EXPECT_TRUE(s.IsOk());
  164. std::vector<NodeIdType> node_list;
  165. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  166. node_list.push_back(*itr);
  167. if (node_list.size() >= 10) {
  168. break;
  169. }
  170. }
  171. std::shared_ptr<Tensor> neg_neighbors;
  172. s = graph.GetNegSampledNeighbors(node_list, 3, meta_info.node_type[1], &neg_neighbors);
  173. EXPECT_TRUE(s.IsOk());
  174. EXPECT_TRUE(neg_neighbors->shape().ToString() == "<10,4>");
  175. neg_neighbors.reset();
  176. s = graph.GetNegSampledNeighbors({}, 3, meta_info.node_type[1], &neg_neighbors);
  177. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  178. neg_neighbors.reset();
  179. s = graph.GetNegSampledNeighbors(node_list, 3, 3, &neg_neighbors);
  180. EXPECT_TRUE(s.ToString().find("Invalid node type:3") != std::string::npos);
  181. }
  182. TEST_F(MindDataTestGNNGraph, TestRandomWalk) {
  183. std::string path = "data/mindrecord/testGraphData/sns";
  184. Graph graph(path, 1);
  185. Status s = graph.Init();
  186. EXPECT_TRUE(s.IsOk());
  187. MetaInfo meta_info;
  188. s = graph.GetMetaInfo(&meta_info);
  189. EXPECT_TRUE(s.IsOk());
  190. std::shared_ptr<Tensor> nodes;
  191. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  192. EXPECT_TRUE(s.IsOk());
  193. std::vector<NodeIdType> node_list;
  194. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  195. node_list.push_back(*itr);
  196. }
  197. print_int_vec(node_list, "node list ");
  198. std::vector<NodeType> meta_path(59, 1);
  199. std::shared_ptr<Tensor> walk_path;
  200. s = graph.RandomWalk(node_list, meta_path, 2.0, 0.5, -1, &walk_path);
  201. EXPECT_TRUE(s.IsOk());
  202. EXPECT_TRUE(walk_path->shape().ToString() == "<33,60>");
  203. }