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 10 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 "minddata/dataset/util/status.h"
  23. #include "minddata/dataset/engine/gnn/node.h"
  24. #include "minddata/dataset/engine/gnn/graph_data_impl.h"
  25. #include "minddata/dataset/engine/gnn/graph_loader.h"
  26. using namespace mindspore::dataset;
  27. using namespace mindspore::dataset::gnn;
  28. #define print_int_vec(_i, _str) \
  29. do { \
  30. std::stringstream ss; \
  31. std::copy(_i.begin(), _i.end(), std::ostream_iterator<int>(ss, " ")); \
  32. MS_LOG(INFO) << _str << " " << ss.str(); \
  33. } while (false)
  34. class MindDataTestGNNGraph : public UT::Common {
  35. protected:
  36. MindDataTestGNNGraph() = default;
  37. };
  38. TEST_F(MindDataTestGNNGraph, TestGetAllNeighbors) {
  39. std::string path = "data/mindrecord/testGraphData/testdata";
  40. GraphDataImpl graph(path, 1);
  41. Status s = graph.Init();
  42. EXPECT_TRUE(s.IsOk());
  43. MetaInfo meta_info;
  44. s = graph.GetMetaInfo(&meta_info);
  45. EXPECT_TRUE(s.IsOk());
  46. EXPECT_TRUE(meta_info.node_type.size() == 2);
  47. std::shared_ptr<Tensor> nodes;
  48. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  49. EXPECT_TRUE(s.IsOk());
  50. std::vector<NodeIdType> node_list;
  51. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  52. node_list.push_back(*itr);
  53. if (node_list.size() >= 10) {
  54. break;
  55. }
  56. }
  57. std::shared_ptr<Tensor> neighbors;
  58. s = graph.GetAllNeighbors(node_list, meta_info.node_type[1], &neighbors);
  59. EXPECT_TRUE(s.IsOk());
  60. EXPECT_TRUE(neighbors->shape().ToString() == "<10,6>");
  61. TensorRow features;
  62. s = graph.GetNodeFeature(nodes, meta_info.node_feature_type, &features);
  63. EXPECT_TRUE(s.IsOk());
  64. EXPECT_TRUE(features.size() == 4);
  65. EXPECT_TRUE(features[0]->shape().ToString() == "<10,5>");
  66. EXPECT_TRUE(features[0]->ToString() ==
  67. "Tensor (shape: <10,5>, Type: int32)\n"
  68. "[[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,"
  69. "0,0],[0,1,0,1,0]]");
  70. EXPECT_TRUE(features[1]->shape().ToString() == "<10>");
  71. EXPECT_TRUE(features[1]->ToString() ==
  72. "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]");
  73. EXPECT_TRUE(features[2]->shape().ToString() == "<10>");
  74. EXPECT_TRUE(features[2]->ToString() == "Tensor (shape: <10>, Type: int32)\n[1,2,3,1,4,3,5,3,5,4]");
  75. }
  76. TEST_F(MindDataTestGNNGraph, TestGetSampledNeighbors) {
  77. std::string path = "data/mindrecord/testGraphData/testdata";
  78. GraphDataImpl graph(path, 1);
  79. Status s = graph.Init();
  80. EXPECT_TRUE(s.IsOk());
  81. MetaInfo meta_info;
  82. s = graph.GetMetaInfo(&meta_info);
  83. EXPECT_TRUE(s.IsOk());
  84. EXPECT_TRUE(meta_info.node_type.size() == 2);
  85. std::shared_ptr<Tensor> edges;
  86. s = graph.GetAllEdges(meta_info.edge_type[0], &edges);
  87. EXPECT_TRUE(s.IsOk());
  88. std::vector<EdgeIdType> edge_list;
  89. edge_list.resize(edges->Size());
  90. std::transform(edges->begin<EdgeIdType>(), edges->end<EdgeIdType>(), edge_list.begin(),
  91. [](const EdgeIdType edge) { return edge; });
  92. TensorRow edge_features;
  93. s = graph.GetEdgeFeature(edges, meta_info.edge_feature_type, &edge_features);
  94. EXPECT_TRUE(s.IsOk());
  95. EXPECT_TRUE(edge_features[0]->ToString() ==
  96. "Tensor (shape: <40>, Type: int32)\n"
  97. "[0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0]");
  98. EXPECT_TRUE(edge_features[1]->ToString() ==
  99. "Tensor (shape: <40>, Type: float32)\n"
  100. "[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2,2.1,2.2,2.3,2.4,2.5,2.6,2."
  101. "7,2.8,2.9,3,3.1,3.2,3.3,3.4,3.5,3.6,3.7,3.8,3.9,4]");
  102. std::shared_ptr<Tensor> nodes;
  103. s = graph.GetNodesFromEdges(edge_list, &nodes);
  104. EXPECT_TRUE(s.IsOk());
  105. std::unordered_set<NodeIdType> node_set;
  106. std::vector<NodeIdType> node_list;
  107. int index = 0;
  108. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  109. index++;
  110. if (index % 2 == 0) {
  111. continue;
  112. }
  113. node_set.emplace(*itr);
  114. if (node_set.size() >= 5) {
  115. break;
  116. }
  117. }
  118. node_list.resize(node_set.size());
  119. std::transform(node_set.begin(), node_set.end(), node_list.begin(), [](const NodeIdType node) { return node; });
  120. std::shared_ptr<Tensor> neighbors;
  121. s = graph.GetSampledNeighbors(node_list, {10}, {meta_info.node_type[1]}, &neighbors);
  122. EXPECT_TRUE(s.IsOk());
  123. EXPECT_TRUE(neighbors->shape().ToString() == "<5,11>");
  124. neighbors.reset();
  125. s = graph.GetSampledNeighbors(node_list, {2, 3}, {meta_info.node_type[1], meta_info.node_type[0]}, &neighbors);
  126. EXPECT_TRUE(s.IsOk());
  127. EXPECT_TRUE(neighbors->shape().ToString() == "<5,9>");
  128. neighbors.reset();
  129. s = graph.GetSampledNeighbors(node_list, {2, 3, 4},
  130. {meta_info.node_type[1], meta_info.node_type[0], meta_info.node_type[1]}, &neighbors);
  131. EXPECT_TRUE(s.IsOk());
  132. EXPECT_TRUE(neighbors->shape().ToString() == "<5,33>");
  133. neighbors.reset();
  134. s = graph.GetSampledNeighbors({}, {10}, {meta_info.node_type[1]}, &neighbors);
  135. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  136. neighbors.reset();
  137. s = graph.GetSampledNeighbors({-1, 1}, {10}, {meta_info.node_type[1]}, &neighbors);
  138. EXPECT_TRUE(s.ToString().find("Invalid node id") != std::string::npos);
  139. neighbors.reset();
  140. s = graph.GetSampledNeighbors(node_list, {2, 50}, {meta_info.node_type[0], meta_info.node_type[1]}, &neighbors);
  141. EXPECT_TRUE(s.ToString().find("Wrong samples number") != std::string::npos);
  142. neighbors.reset();
  143. s = graph.GetSampledNeighbors(node_list, {2}, {5}, &neighbors);
  144. EXPECT_TRUE(s.ToString().find("Invalid neighbor type") != std::string::npos);
  145. neighbors.reset();
  146. s = graph.GetSampledNeighbors(node_list, {2, 3, 4}, {meta_info.node_type[1], meta_info.node_type[0]}, &neighbors);
  147. EXPECT_TRUE(s.ToString().find("The sizes of neighbor_nums and neighbor_types are inconsistent.") !=
  148. std::string::npos);
  149. neighbors.reset();
  150. s = graph.GetSampledNeighbors({301}, {10}, {meta_info.node_type[1]}, &neighbors);
  151. EXPECT_TRUE(s.ToString().find("Invalid node id:301") != std::string::npos);
  152. }
  153. TEST_F(MindDataTestGNNGraph, TestGetNegSampledNeighbors) {
  154. std::string path = "data/mindrecord/testGraphData/testdata";
  155. GraphDataImpl graph(path, 1);
  156. Status s = graph.Init();
  157. EXPECT_TRUE(s.IsOk());
  158. MetaInfo meta_info;
  159. s = graph.GetMetaInfo(&meta_info);
  160. EXPECT_TRUE(s.IsOk());
  161. EXPECT_TRUE(meta_info.node_type.size() == 2);
  162. std::shared_ptr<Tensor> nodes;
  163. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  164. EXPECT_TRUE(s.IsOk());
  165. std::vector<NodeIdType> node_list;
  166. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  167. node_list.push_back(*itr);
  168. if (node_list.size() >= 10) {
  169. break;
  170. }
  171. }
  172. std::shared_ptr<Tensor> neg_neighbors;
  173. s = graph.GetNegSampledNeighbors(node_list, 3, meta_info.node_type[1], &neg_neighbors);
  174. EXPECT_TRUE(s.IsOk());
  175. EXPECT_TRUE(neg_neighbors->shape().ToString() == "<10,4>");
  176. neg_neighbors.reset();
  177. s = graph.GetNegSampledNeighbors({}, 3, meta_info.node_type[1], &neg_neighbors);
  178. EXPECT_TRUE(s.ToString().find("Input node_list is empty.") != std::string::npos);
  179. neg_neighbors.reset();
  180. s = graph.GetNegSampledNeighbors({-1, 1}, 3, meta_info.node_type[1], &neg_neighbors);
  181. EXPECT_TRUE(s.ToString().find("Invalid node id") != std::string::npos);
  182. neg_neighbors.reset();
  183. s = graph.GetNegSampledNeighbors(node_list, 50, meta_info.node_type[1], &neg_neighbors);
  184. EXPECT_TRUE(s.ToString().find("Wrong samples number") != std::string::npos);
  185. neg_neighbors.reset();
  186. s = graph.GetNegSampledNeighbors(node_list, 3, 3, &neg_neighbors);
  187. EXPECT_TRUE(s.ToString().find("Invalid neighbor type") != std::string::npos);
  188. }
  189. TEST_F(MindDataTestGNNGraph, TestRandomWalk) {
  190. std::string path = "data/mindrecord/testGraphData/sns";
  191. GraphDataImpl graph(path, 1);
  192. Status s = graph.Init();
  193. EXPECT_TRUE(s.IsOk());
  194. MetaInfo meta_info;
  195. s = graph.GetMetaInfo(&meta_info);
  196. EXPECT_TRUE(s.IsOk());
  197. std::shared_ptr<Tensor> nodes;
  198. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  199. EXPECT_TRUE(s.IsOk());
  200. std::vector<NodeIdType> node_list;
  201. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  202. node_list.push_back(*itr);
  203. }
  204. print_int_vec(node_list, "node list ");
  205. std::vector<NodeType> meta_path(59, 1);
  206. std::shared_ptr<Tensor> walk_path;
  207. s = graph.RandomWalk(node_list, meta_path, 2.0, 0.5, -1, &walk_path);
  208. EXPECT_TRUE(s.IsOk());
  209. EXPECT_TRUE(walk_path->shape().ToString() == "<33,60>");
  210. }
  211. TEST_F(MindDataTestGNNGraph, TestRandomWalkDefaults) {
  212. std::string path = "data/mindrecord/testGraphData/sns";
  213. GraphDataImpl graph(path, 1);
  214. Status s = graph.Init();
  215. EXPECT_TRUE(s.IsOk());
  216. MetaInfo meta_info;
  217. s = graph.GetMetaInfo(&meta_info);
  218. EXPECT_TRUE(s.IsOk());
  219. std::shared_ptr<Tensor> nodes;
  220. s = graph.GetAllNodes(meta_info.node_type[0], &nodes);
  221. EXPECT_TRUE(s.IsOk());
  222. std::vector<NodeIdType> node_list;
  223. for (auto itr = nodes->begin<NodeIdType>(); itr != nodes->end<NodeIdType>(); ++itr) {
  224. node_list.push_back(*itr);
  225. }
  226. print_int_vec(node_list, "node list ");
  227. std::vector<NodeType> meta_path(59, 1);
  228. std::shared_ptr<Tensor> walk_path;
  229. s = graph.RandomWalk(node_list, meta_path, 1.0, 1.0, -1, &walk_path);
  230. EXPECT_TRUE(s.IsOk());
  231. EXPECT_TRUE(walk_path->shape().ToString() == "<33,60>");
  232. }