Chromium Code Reviews| Index: appengine/findit/crash/loglinear/test/training_test.py |
| diff --git a/appengine/findit/crash/loglinear/test/training_test.py b/appengine/findit/crash/loglinear/test/training_test.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4df8e59b44c7fb1ba5ee7e0ab4ba87eb3e802730 |
| --- /dev/null |
| +++ b/appengine/findit/crash/loglinear/test/training_test.py |
| @@ -0,0 +1,64 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import unittest |
| +import random |
| +import numpy as np |
| + |
| +import crash.loglinear.test.model_test as loglinear_test |
|
Sharu Jiang
2016/12/21 23:54:17
We'd better not make training_test depend on model
wrengr
2016/12/22 19:58:00
Done.
|
| +from crash.loglinear.model import ToFeatureFunction |
| +from crash.loglinear.training import TrainableLogLinearModel |
| + |
| + |
| +training_data = [(x, x == 7) for x in xrange(10)] |
| + |
| + |
| +class TrainableLogLinearModelTest(unittest.TestCase): |
| + |
| + def setUp(self): |
| + super(TrainableLogLinearModelTest, self).setUp() |
| + feature_function = ToFeatureFunction(loglinear_test.features) |
| + initial_weights = [random.random() for _ in loglinear_test.features] |
| + self.model = TrainableLogLinearModel( |
| + loglinear_test.Y, training_data, feature_function, initial_weights) |
| + |
| + def testWeightsSetterNotAnNdarray(self): |
| + def _WeightSettingExpression(): |
| + """Wrap the ``self.model.weights = stuff`` expression. |
| + |
| + The ``assertRaises`` method expects a callable object, so we need |
| + to wrap the expression in a def. If we didn't wrap it in a def |
| + then we'd throw the exception too early, and ``assertRaises`` |
| + would never get called in order to see it. Normally we'd use a |
| + lambda for wrapping the expression up, but because the expression |
| + we want to check is actually a statement it can't be in a lambda |
| + but rather must be in a def. |
| + """ |
| + self.model.weights = 'this is not an np.ndarray' |
| + |
| + self.assertRaises(TypeError, _WeightSettingExpression) |
| + |
| + def testWeightsSetterShapeMismatch(self): |
| + def _WeightSettingExpression(): |
| + """Wrap the ``self.model.weights = stuff`` expression.""" |
| + # This np.ndarray has the wrong shape. |
| + self.model.weights = np.array([[1,2], [3,4]]) |
| + |
| + self.assertRaises(TypeError, _WeightSettingExpression) |
| + |
| + def testTrainWeights(self): |
| + """Tests that ``TrainWeights`` actually improves the loglikelihood. |
| + |
| + Actually, this is more of a test that we're calling SciPy's BFGS |
| + implementation correctly. But any bugs we find about that will show |
| + up in trying to run this rest rather than in the assertaion failing |
| + per se. |
| + """ |
| + initial_loglikelihood = self.model.LogLikelihood() |
| + self.model.TrainWeights(0.5) |
| + trained_loglikelihood = self.model.LogLikelihood() |
| + self.assertTrue(trained_loglikelihood >= initial_loglikelihood, |
| + 'Training reduced the loglikelihood from %f to %f,' |
| + ' when it should have increased it!' |
| + % (initial_loglikelihood, trained_loglikelihood)) |