Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2016 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 | |
| 6 # TODO(katesonia): Move this to the config saved in datastore. | |
| 7 _MAX_FRAME_INDEX = 7 | |
| 8 | |
| 9 | |
| 10 class TopFrameIndexFeature(object): | |
| 11 """Return the minimum frame index scaled linearly between 0 and 1. | |
| 12 | |
| 13 That is, when the smallest ``StackFrame.index`` across all files | |
| 14 and stacktraces is 0 we return 1; when it is greater than the | |
| 15 ``max_frame_index`` passed to the constructor, we return 0. And in | |
| 16 between we return values linearly interpolated between those points. | |
| 17 | |
| 18 In principle this normalization isn't strictly required, as the weight | |
| 19 of this feature can be be scaled to account for the normalization. | |
| 20 However, by normalizing things we ensure that the feature's weight is | |
| 21 independent of ``max_frame_index``, which helps training. | |
| 22 """ | |
| 23 | |
| 24 def __init__(self, max_frame_index=None): | |
| 25 """ | |
| 26 Args: | |
| 27 max_frame_index (int): An upper bound on the minimum frame index | |
| 28 to consider. This argument is optional and defaults to | |
| 29 ``_MAX_FRAME_INDEX``. | |
| 30 """ | |
| 31 if max_frame_index is None: | |
| 32 max_frame_index = _MAX_FRAME_INDEX | |
|
inferno
2016/12/06 18:07:06
init in constructor
| |
| 33 self.max_frame_index = float(max_frame_index) | |
|
Sharu Jiang
2016/12/06 20:49:19
why converting the max_frame_index to float here?
wrengr
2016/12/07 00:55:38
I was waffling back and forth on whether to do it
| |
| 34 | |
| 35 def __call__(self, result): | |
| 36 """The minimum ``StackFrame.index`` across all files and stacks. | |
| 37 | |
| 38 Although this looks like it should be a method on the ``Result`` | |
| 39 class itself, we have it be a standalone function in order to make | |
| 40 coverage tests happy. The downside of this is that we now have to | |
| 41 modify multiple files whenever the guts of ``Result`` change. The | |
| 42 upside is the aforementioned coverage tests, and that it helps | |
| 43 keep the ``Result`` class looking cleaner. | |
| 44 | |
| 45 Args: | |
| 46 result (Result): the result to analyze. | |
| 47 | |
| 48 Returns: | |
| 49 The minimum frame index, as a ``float``. If the ``Result`` has | |
| 50 no frames or if the true minimum frame index is greater than | |
| 51 ``max_frame_index``, then we return zero. Otherwise the final | |
| 52 value is scaled linearly between 0 and 1. | |
| 53 """ | |
| 54 if not result.file_to_stack_infos: | |
| 55 return 0. | |
| 56 | |
| 57 top_frame_index = min(self.max_frame_index, | |
| 58 float(min(min(frame.index for frame, _ in stack_infos) | |
| 59 for stack_infos in result.file_to_stack_infos.itervalues()))) | |
| 60 | |
| 61 return (self.max_frame_index - top_frame_index) / self.max_frame_index | |
| 62 | |
| 63 | |
| 64 class SquaredTopFrameIndexFeature(TopFrameIndexFeature): | |
| 65 """Return the minimum frame index scaled quadratically between 0 and 1. | |
| 66 | |
| 67 This feature together with ``TopFrameIndexFeature`` (and a constant | |
| 68 feature) allow us to capture any quadratic polynomial of the | |
| 69 ``TopFrameIndex``. That is, suppose we had a single feature ``c2*x**2 + | |
| 70 c1*x + 1`` with weight ``w``. Rather than using that feature directly | |
| 71 (which would require us to specify the hyperparameters ``c2`` and | |
| 72 ``c1``) we can instead use three features: ``w2*(x**2) + w1*x + w0``; | |
|
Sharu Jiang
2016/12/06 20:49:19
Just a thought, we can also use kernel to achieve
wrengr
2016/12/07 00:55:38
We could use some kernel tricks to generate a whol
| |
| 73 which enables us to avoid specifying the hyperparameters, by pushing | |
| 74 them into the weight parameters instead. | |
| 75 """ | |
| 76 def __call__(self, result): | |
| 77 linear_top_frame_index = ( | |
| 78 super(SquaredTopFrameIndexFeature, self).__call__(result)) | |
| 79 return linear_top_frame_index * linear_top_frame_index | |
| OLD | NEW |