| 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 math |
| 6 import numpy as np |
| 7 import random |
| 8 import unittest |
| 9 |
| 10 from crash.loglinear import ToFeatureFunction |
| 11 from crash.loglinear import LogLinearModel |
| 12 |
| 13 |
| 14 # Some arbitrary features. |
| 15 # We don't use double lambdas because gpylint complains about that. |
| 16 def feature0(x): |
| 17 return lambda y: 1.0 if y == (x > 5) else 0.0 |
| 18 |
| 19 |
| 20 def feature1(x): |
| 21 return lambda y: 1.0 if y == ((x % 2) == 1) else 0.0 |
| 22 |
| 23 |
| 24 def feature2(x): |
| 25 return lambda y: 1.0 if y == (x <= 7) else 0.0 |
| 26 |
| 27 |
| 28 features = [feature0, feature1, feature2] |
| 29 X = range(10) |
| 30 Y = [False, True] |
| 31 |
| 32 |
| 33 class LoglinearTest(unittest.TestCase): |
| 34 |
| 35 def testToFeatureFunction(self): |
| 36 """Test that ``ToFeatureFunction`` obeys the equality its docstring says.""" |
| 37 f = ToFeatureFunction(features) |
| 38 for x in X: |
| 39 for y in Y: |
| 40 for i in xrange(len(features)): |
| 41 self.assertEqual(features[i](x)(y), f(x)(y)[i]) |
| 42 |
| 43 def testLogLinearModel(self): |
| 44 """An arbitrary test to get 100% code coverage. |
| 45 |
| 46 Right now this test simply calls every method. The only assertions are |
| 47 that log-domain and normal-domain things are related appropriately; |
| 48 and similarly for the quadrance and l2-norm. Since the one is defined |
| 49 in terms of the other in exactly the way written here, those should |
| 50 be trivially true. However, if the implementation changes, then they |
| 51 may become flaky due to floating point fuzz. Really this should be |
| 52 replaced by a collection of semantically meaningful tests, i.e., |
| 53 ones that actually look for bugs we might realistically need to |
| 54 guard against. At least this test is good for detecting typo-style |
| 55 errors where we try accessing fields/methods that don't exist. |
| 56 """ |
| 57 weights = [random.random() for _ in features] |
| 58 |
| 59 model = LogLinearModel(Y, ToFeatureFunction(features), weights, 0.1) |
| 60 model.ClearAllMemos() |
| 61 model = LogLinearModel(Y, ToFeatureFunction(features), weights) |
| 62 self.assertListEqual(weights, model.weights.tolist()) |
| 63 self.assertEqual(math.sqrt(model.quadrance), model.l2) |
| 64 |
| 65 for x in X: |
| 66 self.assertEqual(math.exp(model.LogZ(x)), model.Z(x)) |
| 67 model.Expectation(x, lambda y: np.array([1.0])) |
| 68 for y in Y: |
| 69 model.Features(x)(y) |
| 70 model.Score(x)(y) |
| 71 self.assertEqual( |
| 72 math.exp(model.LogProbability(x)(y)), |
| 73 model.Probability(x)(y)) |
| OLD | NEW |