Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: appengine/findit/crash/loglinear/weight.py

Issue 2753503002: [Predator] Fix weight flaky loglinear training test. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: appengine/findit/crash/loglinear/weight.py
diff --git a/appengine/findit/crash/loglinear/weight.py b/appengine/findit/crash/loglinear/weight.py
index a1fb4ef98894d86cec1ae393f6fabd6accc8b933..5b0f9be8192d087daddc7bbab936613245276fa8 100644
--- a/appengine/findit/crash/loglinear/weight.py
+++ b/appengine/findit/crash/loglinear/weight.py
@@ -69,24 +69,28 @@ class MetaWeight(MetaDict):
"""Dict-like class mapping features in ``Metafeature`` to their weights."""
def IsZero(self, epsilon):
- """A MetaWeight is zero only when all the sub weights are zeros."""
+ if not self._value:
+ return True
+
return all(weight.IsZero(epsilon) for weight in self.itervalues())
def DropZeroWeights(self, epsilon=0.):
"""Drops all zero weights."""
+ # Make all sub meta weights drop their zero weights.
+ for weight in self.itervalues():
+ if not weight.is_element:
+ weight.DropZeroWeights(epsilon=epsilon)
+
self._value = {name: weight for name, weight in self.iteritems()
- if not weight.IsZero(epsilon)}
+ if not weight.IsZero(epsilon)}
def __len__(self):
return len(self._value)
def __mul__(self, meta_feature):
"""``MetaWeight`` can multiply with ``MetaFeatureValue``."""
- assert len(self) == len(meta_feature), Exception(
Sharu Jiang 2017/03/14 05:06:13 This assertion should be deleted. Because the ``Dr
- 'MetaWeight can only multiply with ``MetaFeatureValue`` with the '
- 'same length')
-
- # MetaWeight is a dense representation of a sparse array.
+ # MetaWeight is a dense representation of a sparse array. So MetaWeight and
+ # MetaFeature don't necessarily have the same length.
return math.fsum(meta_feature[name] * weight
for name, weight in self.iteritems())

Powered by Google App Engine
This is Rietveld 408576698