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.

local_node.cc 4.3 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "dataset/engine/gnn/local_node.h"
  17. #include <algorithm>
  18. #include <string>
  19. #include <utility>
  20. #include "dataset/engine/gnn/edge.h"
  21. #include "dataset/util/random.h"
  22. namespace mindspore {
  23. namespace dataset {
  24. namespace gnn {
  25. LocalNode::LocalNode(NodeIdType id, NodeType type) : Node(id, type), rnd_(GetRandomDevice()) { rnd_.seed(GetSeed()); }
  26. Status LocalNode::GetFeatures(FeatureType feature_type, std::shared_ptr<Feature> *out_feature) {
  27. auto itr = features_.find(feature_type);
  28. if (itr != features_.end()) {
  29. *out_feature = itr->second;
  30. return Status::OK();
  31. } else {
  32. std::string err_msg = "Invalid feature type:" + std::to_string(feature_type);
  33. RETURN_STATUS_UNEXPECTED(err_msg);
  34. }
  35. }
  36. Status LocalNode::GetAllNeighbors(NodeType neighbor_type, std::vector<NodeIdType> *out_neighbors, bool exclude_itself) {
  37. std::vector<NodeIdType> neighbors;
  38. auto itr = neighbor_nodes_.find(neighbor_type);
  39. if (itr != neighbor_nodes_.end()) {
  40. if (exclude_itself) {
  41. neighbors.resize(itr->second.size());
  42. std::transform(itr->second.begin(), itr->second.end(), neighbors.begin(),
  43. [](const std::shared_ptr<Node> node) { return node->id(); });
  44. } else {
  45. neighbors.resize(itr->second.size() + 1);
  46. neighbors[0] = id_;
  47. std::transform(itr->second.begin(), itr->second.end(), neighbors.begin() + 1,
  48. [](const std::shared_ptr<Node> node) { return node->id(); });
  49. }
  50. } else {
  51. MS_LOG(DEBUG) << "No neighbors. node_id:" << id_ << " neighbor_type:" << neighbor_type;
  52. if (!exclude_itself) {
  53. neighbors.emplace_back(id_);
  54. }
  55. }
  56. *out_neighbors = std::move(neighbors);
  57. return Status::OK();
  58. }
  59. Status LocalNode::GetSampledNeighbors(const std::vector<std::shared_ptr<Node>> &neighbors, int32_t samples_num,
  60. std::vector<NodeIdType> *out) {
  61. std::vector<NodeIdType> shuffled_id(neighbors.size());
  62. std::iota(shuffled_id.begin(), shuffled_id.end(), 0);
  63. std::shuffle(shuffled_id.begin(), shuffled_id.end(), rnd_);
  64. int32_t num = std::min(samples_num, static_cast<int32_t>(neighbors.size()));
  65. for (int32_t i = 0; i < num; ++i) {
  66. out->emplace_back(neighbors[shuffled_id[i]]->id());
  67. }
  68. return Status::OK();
  69. }
  70. Status LocalNode::GetSampledNeighbors(NodeType neighbor_type, int32_t samples_num,
  71. std::vector<NodeIdType> *out_neighbors) {
  72. std::vector<NodeIdType> neighbors;
  73. neighbors.reserve(samples_num);
  74. auto itr = neighbor_nodes_.find(neighbor_type);
  75. if (itr != neighbor_nodes_.end()) {
  76. while (neighbors.size() < samples_num) {
  77. RETURN_IF_NOT_OK(GetSampledNeighbors(itr->second, samples_num - neighbors.size(), &neighbors));
  78. }
  79. } else {
  80. MS_LOG(DEBUG) << "There are no neighbors. node_id:" << id_ << " neighbor_type:" << neighbor_type;
  81. // If there are no neighbors, they are filled with kDefaultNodeId
  82. for (int32_t i = 0; i < samples_num; ++i) {
  83. neighbors.emplace_back(kDefaultNodeId);
  84. }
  85. }
  86. *out_neighbors = std::move(neighbors);
  87. return Status::OK();
  88. }
  89. Status LocalNode::AddNeighbor(const std::shared_ptr<Node> &node) {
  90. auto itr = neighbor_nodes_.find(node->type());
  91. if (itr != neighbor_nodes_.end()) {
  92. itr->second.push_back(node);
  93. } else {
  94. neighbor_nodes_[node->type()] = {node};
  95. }
  96. return Status::OK();
  97. }
  98. Status LocalNode::UpdateFeature(const std::shared_ptr<Feature> &feature) {
  99. auto itr = features_.find(feature->type());
  100. if (itr != features_.end()) {
  101. RETURN_STATUS_UNEXPECTED("Feature already exists");
  102. } else {
  103. features_[feature->type()] = feature;
  104. return Status::OK();
  105. }
  106. }
  107. } // namespace gnn
  108. } // namespace dataset
  109. } // namespace mindspore