Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import math | |
| 6 | |
| 7 from crash.loglinear.feature import MetaFeatureValue | |
| 8 from libs.meta_object import Element | |
| 9 from libs.meta_object import MetaDict | |
| 10 | |
| 11 | |
| 12 class Weight(Element): | |
| 13 """Float-like class that represents the weight for a feature.""" | |
| 14 | |
| 15 def __init__(self, value): | |
| 16 super(Weight, self).__init__() | |
| 17 self._value = float(value) | |
| 18 | |
| 19 @property | |
| 20 def value(self): | |
| 21 return self._value | |
| 22 | |
| 23 def __mul__(self, number): | |
| 24 return Weight(self._value * float(number)) | |
| 25 | |
| 26 __rmul__ = __mul__ | |
| 27 | |
| 28 def __len__(self): | |
| 29 return 1 | |
| 30 | |
| 31 def __float__(self): | |
| 32 return self.value | |
| 33 | |
| 34 def __eq__(self, other): | |
| 35 return self._value == other._value | |
| 36 | |
| 37 def __ne__(self, other): | |
| 38 return not self.__eq__(other) | |
| 39 | |
| 40 @property | |
| 41 def l0(self): | |
| 42 """The l0-norm of the weight. | |
| 43 | |
| 44 N.B., despite being popularly called the "l0-norm", this isn't | |
| 45 actually a norm in the mathematical sense.""" | |
| 46 return float(bool(self._value)) | |
| 47 | |
| 48 @property | |
| 49 def l1(self): | |
| 50 """The l1 (aka: Manhattan) norm of the weight.""" | |
| 51 return math.fabs(self._value) | |
| 52 | |
| 53 @property | |
| 54 def quadrance(self): | |
| 55 """The square of the l2 norm of the weight. | |
| 56 | |
| 57 This value is often more helpful to have direct access to, as it | |
| 58 avoids the need for non-rational functions (e.g., sqrt) and shows up | |
| 59 as its own quantity in many places. Also, computing it directly avoids | |
| 60 the error introduced by squaring the square-root of an IEEE-754 float. | |
| 61 """ | |
| 62 return math.fabs(self._value)**2 | |
| 63 | |
| 64 def IsZero(self, epsilon): | |
| 65 return math.fabs(self._value) < epsilon | |
| 66 | |
| 67 | |
| 68 class MetaWeight(MetaDict): | |
| 69 """Dict-like class mapping features in ``Metafeature`` to their weights.""" | |
| 70 | |
| 71 def IsZero(self, epsilon): | |
| 72 """A MetaWeight is zero only when all the sub weights are zeros.""" | |
| 73 return all(weight.IsZero(epsilon) for weight in self.itervalues()) | |
| 74 | |
| 75 def DropZeroWeights(self, epsilon=0.): | |
|
chanli
2017/01/19 04:26:06
Not sure if 0. will work here... Maybe it does?
Sharu Jiang
2017/01/19 23:49:52
Good catch... I should use "math.fabs(self._value)
| |
| 76 """Drops all zero weights.""" | |
| 77 self._value = {name: weight for name, weight in self.iteritems() | |
| 78 if not weight.IsZero(epsilon)} | |
| 79 | |
| 80 def __len__(self): | |
| 81 return len(self._value) | |
| 82 | |
| 83 def __mul__(self, meta_feature): | |
| 84 """``MetaWeight`` can multiply with ``MetaFeatureValue``.""" | |
| 85 assert len(self) == len(meta_feature), Exception( | |
| 86 'MetaWeight can only multiply with ``MetaFeatureValue`` with the ' | |
| 87 'same length') | |
| 88 | |
| 89 # MetaWeight is a dense representation of a sparse array. | |
| 90 return math.fsum(meta_feature[name] * weight | |
| 91 for name, weight in self.iteritems()) | |
| 92 | |
| 93 __rmul__ = __mul__ | |
| 94 | |
| 95 def __eq__(self, other): | |
| 96 if len(self) != len(other): | |
| 97 return False | |
| 98 | |
| 99 for key, value in self.iteritems(): | |
| 100 if value != other.get(key): | |
| 101 return False | |
| 102 | |
| 103 return True | |
| 104 | |
| 105 def __ne__(self, other): | |
| 106 return not self.__eq__(other) | |
| 107 | |
| 108 @property | |
| 109 def l0(self): | |
| 110 """The l0-norm of the meta weight. | |
| 111 | |
| 112 N.B., despite being popularly called the "l0-norm", this isn't | |
| 113 actually a norm in the mathematical sense.""" | |
| 114 return math.fsum(weight.l0 for weight in self.itervalues()) | |
| 115 | |
| 116 @property | |
| 117 def l1(self): | |
| 118 """The l1 (aka: Manhattan) norm of the meta weight.""" | |
| 119 return math.fsum(weight.l1 for weight in self.itervalues()) | |
| 120 | |
| 121 @property | |
| 122 def quadrance(self): | |
| 123 """The square of the l2 norm of the meta weight. | |
| 124 | |
| 125 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 | |
| 127 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. | |
| 129 """ | |
| 130 return math.fsum(weight.quadrance for weight in self.itervalues()) | |
| OLD | NEW |