| 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 import random |
| 6 import unittest |
| 7 |
| 8 from crash.loglinear.feature import FeatureValue |
| 9 from crash.loglinear.model import ToFeatureFunction |
| 10 |
| 11 |
| 12 class LoglinearTestCase(unittest.TestCase): # pragma: no cover |
| 13 """Common code for testing ``model.py`` and ``training.py``.""" |
| 14 |
| 15 def setUp(self): |
| 16 """Set up some basic parts of our loglinear model. |
| 17 |
| 18 These parts describe a silly model for detecting whether an integer |
| 19 in [0..9] is the number 7. So ``X`` is the set of integers [0..9], |
| 20 and ``Y`` is the set of ``bool`` values. The independent variable |
| 21 is boolean-valued because we only have two categories: "yes, x == |
| 22 7" and "no, x != 7". This doesn't take advantage of the fact that |
| 23 loglinear models can categorize larger sets of labels, but it's good |
| 24 enough for testing purposes. |
| 25 |
| 26 In addition to specifying ``X`` and ``Y``, we also specify a set of |
| 27 features and choose some random weights for them. |
| 28 """ |
| 29 super(LoglinearTestCase, self).setUp() |
| 30 |
| 31 # Some arbitrary features. |
| 32 # We don't use double lambdas because gpylint complains about that. |
| 33 def feature0(x): |
| 34 return lambda y: FeatureValue('feature0', y == (x > 5), None, None) |
| 35 |
| 36 def feature1(x): |
| 37 return lambda y: FeatureValue('feature1', y == ((x % 2) == 1), None, None) |
| 38 |
| 39 def feature2(x): |
| 40 return lambda y: FeatureValue('feature2', y == (x <= 7), None, None) |
| 41 |
| 42 self._feature_list = [feature0, feature1, feature2] |
| 43 self._feature_function = ToFeatureFunction(self._feature_list) |
| 44 self._qty_features = len(self._feature_list) |
| 45 self._X = range(10) |
| 46 self._Y = [False, True] |
| 47 self._weights = [random.random() for _ in xrange(self._qty_features)] |
| OLD | NEW |