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

Side by Side Diff: appengine/findit/crash/test/loglinear_test.py

Issue 2517383005: Implementing loglinear classification (without training), for CL classification (Closed)
Patch Set: rebase Created 4 years 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
(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 # TODO(wrengr): how to make this test something reasonable?
inferno 2016/12/06 18:07:06 rephrase this: Make this test reasonable in terms
44 def testLogLinearModel(self):
45 """An arbitrary test to get 100% code coverage."""
46 weights = [random.random() for _ in features]
47
48 model = LogLinearModel(Y, ToFeatureFunction(features), weights, 0.1)
49 model.ClearAllMemos()
50 model = LogLinearModel(Y, ToFeatureFunction(features), weights)
51 self.assertListEqual(weights, model.weights.tolist())
52 # TODO(wrengr): this may be flaky due to floating point fuzz.
inferno 2016/12/06 18:07:06 Just one todo for the next three is better # TODO
53 self.assertEqual(math.sqrt(model.quadrance), model.l2)
54
55 for x in X:
56 # TODO(wrengr): this may be flaky due to floating point fuzz.
57 self.assertEqual(math.exp(model.logZ(x)), model.Z(x))
58 model.Expectation(x, lambda y: np.array([1.0]))
59 for y in Y:
60 model.Features(x)(y)
61 model.Score(x)(y)
62 # TODO(wrengr): this may be flaky due to floating point fuzz.
63 self.assertEqual(
64 math.exp(model.LogProbability(x)(y)),
65 model.Probability(x)(y))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698