Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. From fa7cefa73a6157dd9f105e9dc5e7e0b16f3253d6 Mon Sep 17 00:00:00 2001
  2. From: Ryan Curtin <ryan@ratml.org>
  3. Date: Tue, 9 Jul 2019 20:15:29 -0400
  4. Subject: [PATCH] Fix cover tree statistic computation.
  5.  
  6. ---
  7. .../core/tree/cover_tree/cover_tree_impl.hpp | 53 ++++++++++---------
  8. 1 file changed, 28 insertions(+), 25 deletions(-)
  9.  
  10. diff --git a/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp b/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
  11. index d4749027f..a16073431 100644
  12. --- a/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
  13. +++ b/src/mlpack/core/tree/cover_tree/cover_tree_impl.hpp
  14. @@ -21,6 +21,18 @@
  15. namespace mlpack {
  16. namespace tree {
  17.  
  18. +// Build the statistics, bottom-up.
  19. +template<typename TreeType, typename StatisticType>
  20. +void BuildStatistics(TreeType* node)
  21. +{
  22. + // Recurse first.
  23. + for (size_t i = 0; i < node->NumChildren(); ++i)
  24. + BuildStatistics<TreeType, StatisticType>(&node->Child(i));
  25. +
  26. + // Now build the statistic.
  27. + node->Stat() = StatisticType(*node);
  28. +}
  29. +
  30. // Create the cover tree.
  31. template<
  32. typename MetricType,
  33. @@ -88,9 +100,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  34. {
  35. children.push_back(&(old->Child(i)));
  36.  
  37. - // Set its parent correctly, and rebuild the statistic.
  38. + // Set its parent correctly.
  39. old->Child(i).Parent() = this;
  40. - old->Child(i).Stat() = StatisticType(old->Child(i));
  41. }
  42.  
  43. // Remove all the children so they don't get erased.
  44. @@ -110,8 +121,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  45. else
  46. scale = (int) ceil(log(furthestDescendantDistance) / log(base));
  47.  
  48. - // Initialize statistic.
  49. - stat = StatisticType(*this);
  50. + // Initialize statistics recursively after the entire tree construction is
  51. + // complete.
  52. + BuildStatistics<CoverTree, StatisticType>(this);
  53.  
  54. Log::Info << distanceComps << " distance computations during tree "
  55. << "construction." << std::endl;
  56. @@ -181,8 +193,6 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  57.  
  58. // Set its parent correctly.
  59. old->Child(i).Parent() = this;
  60. - // Rebuild the statistic.
  61. - old->Child(i).Stat() = StatisticType(old->Child(i));
  62. }
  63.  
  64. // Remove all the children so they don't get erased.
  65. @@ -202,8 +212,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  66. else
  67. scale = (int) ceil(log(furthestDescendantDistance) / log(base));
  68.  
  69. - // Initialize statistic.
  70. - stat = StatisticType(*this);
  71. + // Initialize statistics recursively after the entire tree construction is
  72. + // complete.
  73. + BuildStatistics<CoverTree, StatisticType>(this);
  74.  
  75. Log::Info << distanceComps << " distance computations during tree "
  76. << "construction." << std::endl;
  77. @@ -272,9 +283,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  78. {
  79. children.push_back(&(old->Child(i)));
  80.  
  81. - // Set its parent correctly, and rebuild the statistic.
  82. + // Set its parent correctly.
  83. old->Child(i).Parent() = this;
  84. - old->Child(i).Stat() = StatisticType(old->Child(i));
  85. }
  86.  
  87. // Remove all the children so they don't get erased.
  88. @@ -294,8 +304,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  89. else
  90. scale = (int) ceil(log(furthestDescendantDistance) / log(base));
  91.  
  92. - // Initialize statistic.
  93. - stat = StatisticType(*this);
  94. + // Initialize statistics recursively after the entire tree construction is
  95. + // complete.
  96. + BuildStatistics<CoverTree, StatisticType>(this);
  97.  
  98. Log::Info << distanceComps << " distance computations during tree "
  99. << "construction." << std::endl;
  100. @@ -363,9 +374,8 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  101. {
  102. children.push_back(&(old->Child(i)));
  103.  
  104. - // Set its parent correctly, and rebuild the statistic.
  105. + // Set its parent correctly.
  106. old->Child(i).Parent() = this;
  107. - old->Child(i).Stat() = StatisticType(old->Child(i));
  108. }
  109.  
  110. // Remove all the children so they don't get erased.
  111. @@ -385,8 +395,9 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  112. else
  113. scale = (int) ceil(log(furthestDescendantDistance) / log(base));
  114.  
  115. - // Initialize statistic.
  116. - stat = StatisticType(*this);
  117. + // Initialize statistics recursively after the entire tree construction is
  118. + // complete.
  119. + BuildStatistics<CoverTree, StatisticType>(this);
  120.  
  121. Log::Info << distanceComps << " distance computations during tree "
  122. << "construction." << std::endl;
  123. @@ -429,15 +440,11 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  124. {
  125. this->scale = INT_MIN;
  126. numDescendants = 1;
  127. - stat = StatisticType(*this);
  128. return;
  129. }
  130.  
  131. // Otherwise, create the children.
  132. CreateChildren(indices, distances, nearSetSize, farSetSize, usedSetSize);
  133. -
  134. - // Initialize statistic.
  135. - stat = StatisticType(*this);
  136. }
  137.  
  138. // Manually create a cover tree node.
  139. @@ -472,9 +479,6 @@ CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::CoverTree(
  140. // If necessary, create a local metric.
  141. if (localMetric)
  142. this->metric = new MetricType();
  143. -
  144. - // Initialize the statistic.
  145. - stat = StatisticType(*this);
  146. }
  147.  
  148. template<
  149. @@ -1526,11 +1530,10 @@ inline void CoverTree<MetricType, StatisticType, MatType, RootPointPolicy>::
  150. // Now take its child.
  151. children.push_back(&(old->Child(0)));
  152.  
  153. - // Set its parent and parameters correctly, and rebuild the statistic.
  154. + // Set its parent and parameters correctly.
  155. old->Child(0).Parent() = this;
  156. old->Child(0).ParentDistance() = old->ParentDistance();
  157. old->Child(0).DistanceComps() = old->DistanceComps();
  158. - old->Child(0).Stat() = StatisticType(old->Child(0));
  159.  
  160. // Remove its child (so it doesn't delete it).
  161. old->Children().erase(old->Children().begin() + old->Children().size() - 1);
  162. --
  163. 2.20.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement