Spaces:
Sleeping
Sleeping
File size: 7,364 Bytes
9c31a35 976f8d8 3d7c303 9bfcbfa a2fd8f3 976f8d8 5a621f9 0501132 9b2a102 7d4300a 9c31a35 51a6b05 90fd5d4 9c31a35 7d4300a 9c31a35 5fe5010 90fd5d4 9c31a35 7d4300a 9c31a35 7d4300a c7187a6 3821242 4b56660 3821242 c7187a6 593c674 c7187a6 593c674 c7187a6 fbb7cf7 c7187a6 5fe5010 c7187a6 90fd5d4 c7187a6 5b0cc10 c7187a6 9bfcbfa b07eb2d fbb7cf7 4b56660 fbb7cf7 7d4300a dca02e2 593c674 7d4300a 9bfcbfa 593c674 7d4300a 9bfcbfa fbb7cf7 9bfcbfa dca02e2 5fe5010 9bfcbfa 90fd5d4 c7187a6 5b0cc10 9bfcbfa 962c25c 932dcf5 962c25c 90fd5d4 962c25c 90fd5d4 962c25c 5b0cc10 962c25c d4d95e5 fbb7cf7 4b56660 fbb7cf7 d4d95e5 c3a1736 593c674 d4d95e5 593c674 d4d95e5 fbb7cf7 dca02e2 d4d95e5 90fd5d4 d4d95e5 fbb7cf7 c3a1736 d398bf9 c3a1736 d4d95e5 90fd5d4 c7187a6 5b0cc10 d4d95e5 47dbec6 5a621f9 c3293a8 7cda629 c9cead8 5a621f9 c9cead8 47dbec6 4b56660 7cda629 47dbec6 7cda629 47dbec6 7cda629 c9cead8 47dbec6 c9cead8 47dbec6 fbb7cf7 90fd5d4 47dbec6 5b0cc10 a2fd8f3 ef66f4a a2fd8f3 ef66f4a a2fd8f3 ef66f4a a2fd8f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import unittest
import numpy as np
import pandas as pd
import sympy
import pysr
from pysr import PySRRegressor, sympy2torch
class TestTorch(unittest.TestCase):
def setUp(self):
np.random.seed(0)
# Need to import after juliacall:
import torch
self.torch = torch
def test_sympy2torch(self):
x, y, z = sympy.symbols("x y z")
cosx = 1.0 * sympy.cos(x) + y
X = self.torch.tensor(np.random.randn(1000, 3))
true = 1.0 * self.torch.cos(X[:, 0]) + X[:, 1]
torch_module = sympy2torch(cosx, [x, y, z])
self.assertTrue(
np.all(np.isclose(torch_module(X).detach().numpy(), true.detach().numpy()))
)
def test_pipeline_pandas(self):
X = pd.DataFrame(np.random.randn(100, 10))
y = np.ones(X.shape[0])
model = PySRRegressor(
progress=False,
max_evals=10000,
model_selection="accuracy",
extra_sympy_mappings={},
output_torch_format=True,
)
model.fit(X, y)
equations = pd.DataFrame(
{
"Equation": ["1.0", "cos(x1)", "square(cos(x1))"],
"Loss": [1.0, 0.1, 1e-5],
"Complexity": [1, 2, 3],
}
)
equations["Complexity Loss Equation".split(" ")].to_csv(
"equation_file.csv.bkup"
)
model.refresh(checkpoint_file="equation_file.csv")
tformat = model.pytorch()
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
np.testing.assert_almost_equal(
tformat(self.torch.tensor(X.values)).detach().numpy(),
np.square(np.cos(X.values[:, 1])), # Selection 1st feature
decimal=3,
)
def test_pipeline(self):
X = np.random.randn(100, 10)
y = np.ones(X.shape[0])
model = PySRRegressor(
progress=False,
max_evals=10000,
model_selection="accuracy",
output_torch_format=True,
)
model.fit(X, y)
equations = pd.DataFrame(
{
"Equation": ["1.0", "cos(x1)", "square(cos(x1))"],
"Loss": [1.0, 0.1, 1e-5],
"Complexity": [1, 2, 3],
}
)
equations["Complexity Loss Equation".split(" ")].to_csv(
"equation_file.csv.bkup"
)
model.refresh(checkpoint_file="equation_file.csv")
tformat = model.pytorch()
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
np.testing.assert_almost_equal(
tformat(self.torch.tensor(X)).detach().numpy(),
np.square(np.cos(X[:, 1])), # 2nd feature
decimal=3,
)
def test_mod_mapping(self):
x, y, z = sympy.symbols("x y z")
expression = x**2 + sympy.atanh(sympy.Mod(y + 1, 2) - 1) * 3.2 * z
module = sympy2torch(expression, [x, y, z])
X = self.torch.rand(100, 3).float() * 10
true_out = (
X[:, 0] ** 2
+ self.torch.atanh(self.torch.fmod(X[:, 1] + 1, 2) - 1) * 3.2 * X[:, 2]
)
torch_out = module(X)
np.testing.assert_array_almost_equal(
true_out.detach(), torch_out.detach(), decimal=3
)
def test_custom_operator(self):
X = np.random.randn(100, 3)
y = np.ones(X.shape[0])
model = PySRRegressor(
progress=False,
max_evals=10000,
model_selection="accuracy",
output_torch_format=True,
)
model.fit(X, y)
equations = pd.DataFrame(
{
"Equation": ["1.0", "mycustomoperator(x1)"],
"Loss": [1.0, 0.1],
"Complexity": [1, 2],
}
)
equations["Complexity Loss Equation".split(" ")].to_csv(
"equation_file_custom_operator.csv.bkup"
)
model.set_params(
equation_file="equation_file_custom_operator.csv",
extra_sympy_mappings={"mycustomoperator": sympy.sin},
extra_torch_mappings={"mycustomoperator": self.torch.sin},
)
model.refresh(checkpoint_file="equation_file_custom_operator.csv")
self.assertEqual(str(model.sympy()), "sin(x1)")
# Will automatically use the set global state from get_hof.
tformat = model.pytorch()
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=sin(x1))")
np.testing.assert_almost_equal(
tformat(self.torch.tensor(X)).detach().numpy(),
np.sin(X[:, 1]),
decimal=3,
)
def test_avoid_simplification(self):
# SymPy should not simplify without permission
torch = self.torch
ex = pysr.export_sympy.pysr2sympy(
"square(exp(sign(0.44796443))) + 1.5 * x1",
# ^ Normally this would become exp1 and require
# its own mapping
feature_names_in=["x1"],
extra_sympy_mappings={"square": lambda x: x**2},
)
m = pysr.export_torch.sympy2torch(ex, ["x1"])
rng = np.random.RandomState(0)
X = rng.randn(10, 1)
np.testing.assert_almost_equal(
m(torch.tensor(X)).detach().numpy(),
np.square(np.exp(np.sign(0.44796443))) + 1.5 * X[:, 0],
decimal=3,
)
def test_issue_656(self):
# Should correctly map numeric symbols to floats
E_plus_x1 = sympy.exp(1) + sympy.symbols("x1")
m = pysr.export_torch.sympy2torch(E_plus_x1, ["x1"])
X = np.random.randn(10, 1)
np.testing.assert_almost_equal(
m(self.torch.tensor(X)).detach().numpy(),
np.exp(1) + X[:, 0],
decimal=3,
)
def test_feature_selection_custom_operators(self):
rstate = np.random.RandomState(0)
X = pd.DataFrame({f"k{i}": rstate.randn(2000) for i in range(10, 21)})
def cos_approx(x):
return 1 - (x**2) / 2 + (x**4) / 24 + (x**6) / 720
y = X["k15"] ** 2 + 2 * cos_approx(X["k20"])
model = PySRRegressor(
progress=False,
unary_operators=["cos_approx(x) = 1 - x^2 / 2 + x^4 / 24 + x^6 / 720"],
select_k_features=3,
maxsize=10,
early_stop_condition=1e-5,
extra_sympy_mappings={"cos_approx": cos_approx},
extra_torch_mappings={"cos_approx": cos_approx},
random_state=0,
deterministic=True,
procs=0,
multithreading=False,
)
np.random.seed(0)
model.fit(X.values, y.values)
torch_module = model.pytorch()
np_output = model.predict(X.values)
torch_output = torch_module(self.torch.tensor(X.values)).detach().numpy()
np.testing.assert_almost_equal(y.values, np_output, decimal=3)
np.testing.assert_almost_equal(y.values, torch_output, decimal=3)
def runtests(just_tests=False):
"""Run all tests in test_torch.py."""
tests = [TestTorch]
if just_tests:
return tests
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for test in tests:
suite.addTests(loader.loadTestsFromTestCase(test))
runner = unittest.TextTestRunner()
return runner.run(suite)
|