|
| 1 | +import unittest |
| 2 | +import pandas as pd |
| 3 | +import numpy as np |
| 4 | +import numba |
| 5 | +import h5py |
| 6 | +import hpat |
| 7 | +from hpat.tests.test_utils import (count_array_REPs, count_parfor_REPs, |
| 8 | + count_parfor_OneDs, count_array_OneDs, dist_IR_contains) |
| 9 | + |
| 10 | + |
| 11 | +class TestML(unittest.TestCase): |
| 12 | + def test_logistic_regression(self): |
| 13 | + def test_impl(n, d): |
| 14 | + iterations = 3 |
| 15 | + X = np.ones((n,d))+.5 |
| 16 | + Y = np.ones(n) |
| 17 | + D = X.shape[1] |
| 18 | + w = np.ones(D)-0.5 |
| 19 | + for i in range(iterations): |
| 20 | + w -= np.dot(((1.0 / (1.0 + np.exp(-Y * np.dot(X,w))) - 1.0) * Y), X) |
| 21 | + return w |
| 22 | + |
| 23 | + hpat_func = hpat.jit(test_impl) |
| 24 | + n = 11 |
| 25 | + d = 4 |
| 26 | + np.testing.assert_allclose(hpat_func(n, d), test_impl(n, d)) |
| 27 | + self.assertEqual(count_array_OneDs(), 3) |
| 28 | + self.assertEqual(count_parfor_OneDs(), 3) |
| 29 | + |
| 30 | + def test_logistic_regression_acc(self): |
| 31 | + def test_impl(N, D): |
| 32 | + iterations = 3 |
| 33 | + g = 2 * np.ones(D) - 1 |
| 34 | + X = 2 * np.ones((N, D)) - 1 |
| 35 | + Y = (np.dot(X, g) > 0.0) == (np.ones(N) > .90) |
| 36 | + |
| 37 | + w = 2 * np.ones(D) - 1 |
| 38 | + for i in range(iterations): |
| 39 | + w -= np.dot(((1.0 / (1.0 + np.exp(-Y * np.dot(X, w))) - 1.0) * Y), X) |
| 40 | + #R = np.dot(X,w) > 0.0 |
| 41 | + #accuracy = np.sum(R == Y) / N |
| 42 | + return w |
| 43 | + |
| 44 | + hpat_func = hpat.jit(test_impl) |
| 45 | + n = 11 |
| 46 | + d = 4 |
| 47 | + #np.testing.assert_allclose(hpat_func(n, d), test_impl(n, d)) |
| 48 | + #self.assertEqual(count_array_OneDs(), 3) |
| 49 | + #self.assertEqual(count_parfor_OneDs(), 3) |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + unittest.main() |
0 commit comments