Spaces:
Running
Running
MilesCranmer
commited on
Commit
•
5fe5010
1
Parent(s):
7602382
Order torch imports after Julia init
Browse files- test/test_torch.py +9 -1
test/test_torch.py
CHANGED
@@ -2,7 +2,6 @@ import unittest
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
from pysr import sympy2torch, PySRRegressor
|
5 |
-
import torch
|
6 |
import sympy
|
7 |
from functools import partial
|
8 |
|
@@ -14,6 +13,8 @@ class TestTorch(unittest.TestCase):
|
|
14 |
def test_sympy2torch(self):
|
15 |
x, y, z = sympy.symbols("x y z")
|
16 |
cosx = 1.0 * sympy.cos(x) + y
|
|
|
|
|
17 |
X = torch.tensor(np.random.randn(1000, 3))
|
18 |
true = 1.0 * torch.cos(X[:, 0]) + X[:, 1]
|
19 |
torch_module = sympy2torch(cosx, [x, y, z])
|
@@ -49,6 +50,8 @@ class TestTorch(unittest.TestCase):
|
|
49 |
|
50 |
tformat = model.pytorch()
|
51 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
|
|
|
|
52 |
np.testing.assert_almost_equal(
|
53 |
tformat(torch.tensor(X.values)).detach().numpy(),
|
54 |
np.square(np.cos(X.values[:, 1])), # Selection 1st feature
|
@@ -81,6 +84,8 @@ class TestTorch(unittest.TestCase):
|
|
81 |
|
82 |
tformat = model.pytorch()
|
83 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
|
|
|
|
84 |
np.testing.assert_almost_equal(
|
85 |
tformat(torch.tensor(X)).detach().numpy(),
|
86 |
np.square(np.cos(X[:, 1])), # 2nd feature
|
@@ -93,6 +98,7 @@ class TestTorch(unittest.TestCase):
|
|
93 |
|
94 |
module = sympy2torch(expression, [x, y, z])
|
95 |
|
|
|
96 |
X = torch.rand(100, 3).float() * 10
|
97 |
|
98 |
true_out = (
|
@@ -133,6 +139,7 @@ class TestTorch(unittest.TestCase):
|
|
133 |
|
134 |
tformat = model.pytorch()
|
135 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=sin(x1))")
|
|
|
136 |
np.testing.assert_almost_equal(
|
137 |
tformat(torch.tensor(X)).detach().numpy(),
|
138 |
np.sin(X[:, 1]),
|
@@ -152,6 +159,7 @@ class TestTorch(unittest.TestCase):
|
|
152 |
torch_module = model.pytorch()
|
153 |
|
154 |
np_output = model.predict(X.values)
|
|
|
155 |
torch_output = torch_module(torch.tensor(X.values)).detach().numpy()
|
156 |
|
157 |
np.testing.assert_almost_equal(np_output, torch_output, decimal=4)
|
|
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
from pysr import sympy2torch, PySRRegressor
|
|
|
5 |
import sympy
|
6 |
from functools import partial
|
7 |
|
|
|
13 |
def test_sympy2torch(self):
|
14 |
x, y, z = sympy.symbols("x y z")
|
15 |
cosx = 1.0 * sympy.cos(x) + y
|
16 |
+
|
17 |
+
import torch
|
18 |
X = torch.tensor(np.random.randn(1000, 3))
|
19 |
true = 1.0 * torch.cos(X[:, 0]) + X[:, 1]
|
20 |
torch_module = sympy2torch(cosx, [x, y, z])
|
|
|
50 |
|
51 |
tformat = model.pytorch()
|
52 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
53 |
+
import torch
|
54 |
+
|
55 |
np.testing.assert_almost_equal(
|
56 |
tformat(torch.tensor(X.values)).detach().numpy(),
|
57 |
np.square(np.cos(X.values[:, 1])), # Selection 1st feature
|
|
|
84 |
|
85 |
tformat = model.pytorch()
|
86 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=cos(x1)**2)")
|
87 |
+
|
88 |
+
import torch
|
89 |
np.testing.assert_almost_equal(
|
90 |
tformat(torch.tensor(X)).detach().numpy(),
|
91 |
np.square(np.cos(X[:, 1])), # 2nd feature
|
|
|
98 |
|
99 |
module = sympy2torch(expression, [x, y, z])
|
100 |
|
101 |
+
import torch
|
102 |
X = torch.rand(100, 3).float() * 10
|
103 |
|
104 |
true_out = (
|
|
|
139 |
|
140 |
tformat = model.pytorch()
|
141 |
self.assertEqual(str(tformat), "_SingleSymPyModule(expression=sin(x1))")
|
142 |
+
import torch
|
143 |
np.testing.assert_almost_equal(
|
144 |
tformat(torch.tensor(X)).detach().numpy(),
|
145 |
np.sin(X[:, 1]),
|
|
|
159 |
torch_module = model.pytorch()
|
160 |
|
161 |
np_output = model.predict(X.values)
|
162 |
+
import torch
|
163 |
torch_output = torch_module(torch.tensor(X.values)).detach().numpy()
|
164 |
|
165 |
np.testing.assert_almost_equal(np_output, torch_output, decimal=4)
|