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

Side by Side 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 unified diff | Download patch
OLDNEW
1 # Copyright 2017 The Chromium Authors. All rights reserved. 1 # Copyright 2017 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import math 5 import math
6 6
7 from crash.loglinear.feature import MetaFeatureValue 7 from crash.loglinear.feature import MetaFeatureValue
8 from libs.meta_object import Element 8 from libs.meta_object import Element
9 from libs.meta_object import MetaDict 9 from libs.meta_object import MetaDict
10 10
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 return math.fabs(self._value)**2 62 return math.fabs(self._value)**2
63 63
64 def IsZero(self, epsilon): 64 def IsZero(self, epsilon):
65 return math.fabs(self._value) <= epsilon 65 return math.fabs(self._value) <= epsilon
66 66
67 67
68 class MetaWeight(MetaDict): 68 class MetaWeight(MetaDict):
69 """Dict-like class mapping features in ``Metafeature`` to their weights.""" 69 """Dict-like class mapping features in ``Metafeature`` to their weights."""
70 70
71 def IsZero(self, epsilon): 71 def IsZero(self, epsilon):
72 """A MetaWeight is zero only when all the sub weights are zeros.""" 72 if not self._value:
73 return True
74
73 return all(weight.IsZero(epsilon) for weight in self.itervalues()) 75 return all(weight.IsZero(epsilon) for weight in self.itervalues())
74 76
75 def DropZeroWeights(self, epsilon=0.): 77 def DropZeroWeights(self, epsilon=0.):
76 """Drops all zero weights.""" 78 """Drops all zero weights."""
79 # Make all sub meta weights drop their zero weights.
80 for weight in self.itervalues():
81 if not weight.is_element:
82 weight.DropZeroWeights(epsilon=epsilon)
83
77 self._value = {name: weight for name, weight in self.iteritems() 84 self._value = {name: weight for name, weight in self.iteritems()
78 if not weight.IsZero(epsilon)} 85 if not weight.IsZero(epsilon)}
79 86
80 def __len__(self): 87 def __len__(self):
81 return len(self._value) 88 return len(self._value)
82 89
83 def __mul__(self, meta_feature): 90 def __mul__(self, meta_feature):
84 """``MetaWeight`` can multiply with ``MetaFeatureValue``.""" 91 """``MetaWeight`` can multiply with ``MetaFeatureValue``."""
85 assert len(self) == len(meta_feature), Exception( 92 # MetaWeight is a dense representation of a sparse array. So MetaWeight and
Sharu Jiang 2017/03/14 05:06:13 This assertion should be deleted. Because the ``Dr
86 'MetaWeight can only multiply with ``MetaFeatureValue`` with the ' 93 # MetaFeature don't necessarily have the same length.
87 'same length')
88
89 # MetaWeight is a dense representation of a sparse array.
90 return math.fsum(meta_feature[name] * weight 94 return math.fsum(meta_feature[name] * weight
91 for name, weight in self.iteritems()) 95 for name, weight in self.iteritems())
92 96
93 __rmul__ = __mul__ 97 __rmul__ = __mul__
94 98
95 def __eq__(self, other): 99 def __eq__(self, other):
96 if len(self) != len(other): 100 if len(self) != len(other):
97 return False 101 return False
98 102
99 for key, value in self.iteritems(): 103 for key, value in self.iteritems():
(...skipping 21 matching lines...) Expand all
121 @property 125 @property
122 def quadrance(self): 126 def quadrance(self):
123 """The square of the l2 norm of the meta weight. 127 """The square of the l2 norm of the meta weight.
124 128
125 This value is often more helpful to have direct access to, as it 129 This value is often more helpful to have direct access to, as it
126 avoids the need for non-rational functions (e.g., sqrt) and shows up 130 avoids the need for non-rational functions (e.g., sqrt) and shows up
127 as its own quantity in many places. Also, computing it directly avoids 131 as its own quantity in many places. Also, computing it directly avoids
128 the error introduced by squaring the square-root of an IEEE-754 float. 132 the error introduced by squaring the square-root of an IEEE-754 float.
129 """ 133 """
130 return math.fsum(weight.quadrance for weight in self.itervalues()) 134 return math.fsum(weight.quadrance for weight in self.itervalues())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698