Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
90fd5d4
1
Parent(s):
c5c6896
Clean up torch tests
Browse files- pysr/test/test_torch.py +15 -33
pysr/test/test_torch.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import platform
|
2 |
import unittest
|
3 |
|
4 |
import numpy as np
|
@@ -7,42 +6,28 @@ import sympy
|
|
7 |
|
8 |
from .. import PySRRegressor, sympy2torch
|
9 |
|
10 |
-
# Need to initialize Julia before importing torch...
|
11 |
-
|
12 |
-
|
13 |
-
def _import_torch():
|
14 |
-
if platform.system() == "Darwin":
|
15 |
-
# Import PyJulia, then Torch
|
16 |
-
from ..julia_helpers import init_julia
|
17 |
-
|
18 |
-
init_julia()
|
19 |
-
|
20 |
-
import torch
|
21 |
-
else:
|
22 |
-
# Import Torch, then PyJulia
|
23 |
-
# https://github.com/pytorch/pytorch/issues/78829
|
24 |
-
import torch
|
25 |
-
return torch
|
26 |
-
|
27 |
|
28 |
class TestTorch(unittest.TestCase):
|
29 |
def setUp(self):
|
30 |
np.random.seed(0)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
32 |
def test_sympy2torch(self):
|
33 |
-
torch = _import_torch()
|
34 |
x, y, z = sympy.symbols("x y z")
|
35 |
cosx = 1.0 * sympy.cos(x) + y
|
36 |
|
37 |
-
X = torch.tensor(np.random.randn(1000, 3))
|
38 |
-
true = 1.0 * torch.cos(X[:, 0]) + X[:, 1]
|
39 |
torch_module = sympy2torch(cosx, [x, y, z])
|
40 |
self.assertTrue(
|
41 |
np.all(np.isclose(torch_module(X).detach().numpy(), true.detach().numpy()))
|
42 |
)
|
43 |
|
44 |
def test_pipeline_pandas(self):
|
45 |
-
torch = _import_torch()
|
46 |
X = pd.DataFrame(np.random.randn(100, 10))
|
47 |
y = np.ones(X.shape[0])
|
48 |
model = PySRRegressor(
|
@@ -71,13 +56,12 @@ class TestTorch(unittest.TestCase):
|
|
71 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
72 |
|
73 |
np.testing.assert_almost_equal(
|
74 |
-
tformat(torch.tensor(X.values)).detach().numpy(),
|
75 |
np.square(np.cos(X.values[:, 1])), # Selection 1st feature
|
76 |
decimal=3,
|
77 |
)
|
78 |
|
79 |
def test_pipeline(self):
|
80 |
-
torch = _import_torch()
|
81 |
X = np.random.randn(100, 10)
|
82 |
y = np.ones(X.shape[0])
|
83 |
model = PySRRegressor(
|
@@ -106,22 +90,22 @@ class TestTorch(unittest.TestCase):
|
|
106 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
107 |
|
108 |
np.testing.assert_almost_equal(
|
109 |
-
tformat(torch.tensor(X)).detach().numpy(),
|
110 |
np.square(np.cos(X[:, 1])), # 2nd feature
|
111 |
decimal=3,
|
112 |
)
|
113 |
|
114 |
def test_mod_mapping(self):
|
115 |
-
torch = _import_torch()
|
116 |
x, y, z = sympy.symbols("x y z")
|
117 |
expression = x**2 + sympy.atanh(sympy.Mod(y + 1, 2) - 1) * 3.2 * z
|
118 |
|
119 |
module = sympy2torch(expression, [x, y, z])
|
120 |
|
121 |
-
X = torch.rand(100, 3).float() * 10
|
122 |
|
123 |
true_out = (
|
124 |
-
X[:, 0] ** 2
|
|
|
125 |
)
|
126 |
torch_out = module(X)
|
127 |
|
@@ -130,7 +114,6 @@ class TestTorch(unittest.TestCase):
|
|
130 |
)
|
131 |
|
132 |
def test_custom_operator(self):
|
133 |
-
torch = _import_torch()
|
134 |
X = np.random.randn(100, 3)
|
135 |
y = np.ones(X.shape[0])
|
136 |
model = PySRRegressor(
|
@@ -156,7 +139,7 @@ class TestTorch(unittest.TestCase):
|
|
156 |
model.set_params(
|
157 |
equation_file="equation_file_custom_operator.csv",
|
158 |
extra_sympy_mappings={"mycustomoperator": sympy.sin},
|
159 |
-
extra_torch_mappings={"mycustomoperator": torch.sin},
|
160 |
)
|
161 |
model.refresh(checkpoint_file="equation_file_custom_operator.csv")
|
162 |
self.assertEqual(str(model.sympy()), "sin(x1)")
|
@@ -165,13 +148,12 @@ class TestTorch(unittest.TestCase):
|
|
165 |
tformat = model.pytorch()
|
166 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=sin(x1))")
|
167 |
np.testing.assert_almost_equal(
|
168 |
-
tformat(torch.tensor(X)).detach().numpy(),
|
169 |
np.sin(X[:, 1]),
|
170 |
decimal=3,
|
171 |
)
|
172 |
|
173 |
def test_feature_selection_custom_operators(self):
|
174 |
-
torch = _import_torch()
|
175 |
rstate = np.random.RandomState(0)
|
176 |
X = pd.DataFrame({f"k{i}": rstate.randn(2000) for i in range(10, 21)})
|
177 |
cos_approx = lambda x: 1 - (x**2) / 2 + (x**4) / 24 + (x**6) / 720
|
@@ -196,7 +178,7 @@ class TestTorch(unittest.TestCase):
|
|
196 |
|
197 |
np_output = model.predict(X.values)
|
198 |
|
199 |
-
torch_output = torch_module(torch.tensor(X.values)).detach().numpy()
|
200 |
|
201 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
202 |
np.testing.assert_almost_equal(y.values, torch_output, decimal=3)
|
|
|
|
|
1 |
import unittest
|
2 |
|
3 |
import numpy as np
|
|
|
6 |
|
7 |
from .. import PySRRegressor, sympy2torch
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
class TestTorch(unittest.TestCase):
|
11 |
def setUp(self):
|
12 |
np.random.seed(0)
|
13 |
|
14 |
+
# Need to import after juliacall:
|
15 |
+
import torch
|
16 |
+
|
17 |
+
self.torch = torch
|
18 |
+
|
19 |
def test_sympy2torch(self):
|
|
|
20 |
x, y, z = sympy.symbols("x y z")
|
21 |
cosx = 1.0 * sympy.cos(x) + y
|
22 |
|
23 |
+
X = self.torch.tensor(np.random.randn(1000, 3))
|
24 |
+
true = 1.0 * self.torch.cos(X[:, 0]) + X[:, 1]
|
25 |
torch_module = sympy2torch(cosx, [x, y, z])
|
26 |
self.assertTrue(
|
27 |
np.all(np.isclose(torch_module(X).detach().numpy(), true.detach().numpy()))
|
28 |
)
|
29 |
|
30 |
def test_pipeline_pandas(self):
|
|
|
31 |
X = pd.DataFrame(np.random.randn(100, 10))
|
32 |
y = np.ones(X.shape[0])
|
33 |
model = PySRRegressor(
|
|
|
56 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
57 |
|
58 |
np.testing.assert_almost_equal(
|
59 |
+
tformat(self.torch.tensor(X.values)).detach().numpy(),
|
60 |
np.square(np.cos(X.values[:, 1])), # Selection 1st feature
|
61 |
decimal=3,
|
62 |
)
|
63 |
|
64 |
def test_pipeline(self):
|
|
|
65 |
X = np.random.randn(100, 10)
|
66 |
y = np.ones(X.shape[0])
|
67 |
model = PySRRegressor(
|
|
|
90 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
91 |
|
92 |
np.testing.assert_almost_equal(
|
93 |
+
tformat(self.torch.tensor(X)).detach().numpy(),
|
94 |
np.square(np.cos(X[:, 1])), # 2nd feature
|
95 |
decimal=3,
|
96 |
)
|
97 |
|
98 |
def test_mod_mapping(self):
|
|
|
99 |
x, y, z = sympy.symbols("x y z")
|
100 |
expression = x**2 + sympy.atanh(sympy.Mod(y + 1, 2) - 1) * 3.2 * z
|
101 |
|
102 |
module = sympy2torch(expression, [x, y, z])
|
103 |
|
104 |
+
X = self.torch.rand(100, 3).float() * 10
|
105 |
|
106 |
true_out = (
|
107 |
+
X[:, 0] ** 2
|
108 |
+
+ self.torch.atanh(self.torch.fmod(X[:, 1] + 1, 2) - 1) * 3.2 * X[:, 2]
|
109 |
)
|
110 |
torch_out = module(X)
|
111 |
|
|
|
114 |
)
|
115 |
|
116 |
def test_custom_operator(self):
|
|
|
117 |
X = np.random.randn(100, 3)
|
118 |
y = np.ones(X.shape[0])
|
119 |
model = PySRRegressor(
|
|
|
139 |
model.set_params(
|
140 |
equation_file="equation_file_custom_operator.csv",
|
141 |
extra_sympy_mappings={"mycustomoperator": sympy.sin},
|
142 |
+
extra_torch_mappings={"mycustomoperator": self.torch.sin},
|
143 |
)
|
144 |
model.refresh(checkpoint_file="equation_file_custom_operator.csv")
|
145 |
self.assertEqual(str(model.sympy()), "sin(x1)")
|
|
|
148 |
tformat = model.pytorch()
|
149 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=sin(x1))")
|
150 |
np.testing.assert_almost_equal(
|
151 |
+
tformat(self.torch.tensor(X)).detach().numpy(),
|
152 |
np.sin(X[:, 1]),
|
153 |
decimal=3,
|
154 |
)
|
155 |
|
156 |
def test_feature_selection_custom_operators(self):
|
|
|
157 |
rstate = np.random.RandomState(0)
|
158 |
X = pd.DataFrame({f"k{i}": rstate.randn(2000) for i in range(10, 21)})
|
159 |
cos_approx = lambda x: 1 - (x**2) / 2 + (x**4) / 24 + (x**6) / 720
|
|
|
178 |
|
179 |
np_output = model.predict(X.values)
|
180 |
|
181 |
+
torch_output = torch_module(self.torch.tensor(X.values)).detach().numpy()
|
182 |
|
183 |
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
|
184 |
np.testing.assert_almost_equal(y.values, torch_output, decimal=3)
|