PySR for (integer) sequences and constant optimization #907
-
|
Thanks for the great code you are providing to the community! import numpy as np
from scipy.special import gamma
X = np.array([1, 2, 3, 4, 5, 6, 7, 8]).reshape(-1, 1)
y = 0.5 * (-4)**X[:,0] * gamma(2*(X[:,0]-1)+1) / gamma((X[:,0]-1)+1)
from pysr import PySRRegressor
model = PySRRegressor(
maxsize=16,
niterations=1000, # < Increase me for better results
binary_operators=["+", "*", "^"],
unary_operators=[
"gammaratio(x::T) where {T} = (x > -1) ? gamma(2*x+1)/gamma(x+1) : T(NaN)",
],
extra_sympy_mappings={"gammaratio": lambda x: gamma(2*x+1) / gamma(x+1)},
elementwise_loss="loss(prediction, target) = (prediction - target)^2",
complexity_of_operators={"+": 1, "*": 1, "^": 2, "gammaratio": 1},
complexity_of_constants=1,
complexity_of_variables=2,
nested_constraints={
"gammaratio": {"gammaratio": 0, "^": 0},
"^": {"gammaratio": 0, "^": 0},
},
constraints={
"^": (1, 2),
"gammaratio": 4
},
)
model.fit(X, y)After around 5000 iterations, PySR finds |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
What happens if you normalize your data first? The initialisation for constants is a unit variance Gaussian so it might take many steps to get to something at large magnitudes |
Beta Was this translation helpful? Give feedback.
-
|
I think part of the issue is that the loss function is entirely dominated by a couple of points: [ins] In [3]: X
Out[3]:
array([[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8]])
[ins] In [4]: y
Out[4]:
array([-2.00000000e+00, 1.60000000e+01, -3.84000000e+02, 1.53600000e+04,
-8.60160000e+05, 6.19315200e+07, -5.44997376e+09, 5.66797271e+11])It is trying to fit this with mean-square error so it will primarily just focus on the last two. If I change the loss function to be relative error: model = PySRRegressor(
# ...
elementwise_loss="(pred, targ) -> abs(pred - targ)/(abs(pred)+1)",
)then it seems to work |
Beta Was this translation helpful? Give feedback.
I think part of the issue is that the loss function is entirely dominated by a couple of points:
It is trying to fit this with mean-square error so it will primarily just focus on the last two.
If I change the loss function to be relative error:
then it seems to work