PySR icon indicating copy to clipboard operation
PySR copied to clipboard

Not possible to load a model fitted with TemplateExpressionSpec

Open MilesCranmer opened this issue 1 year ago • 4 comments

Unfortunately template expressions aren't yet compatible with loading from file—should update the error message to mention this.

Discussed in https://github.com/MilesCranmer/PySR/discussions/844

Originally posted by ibengtsson February 28, 2025 Hi!

First of all, thank you for a great library.

I'm having some issues when working with TemplateExpresssionSpec, and it could very well be a fault on my end... If I create a model with a TemplateExpression, fit and save it, and then try and load it (with from_file) in a different script, I cannot do any model predictions. I just get the error:

ValueError: Failed to evaluate the expression. If you are using a custom operator, make sure to define it in `extra_sympy_mappings`, e.g., `model.set_params(extra_sympy_mappings={'inv': lambda x: 1/x})`, where `lambda x: 1/x` is a valid SymPy function defining the operator. You can then run `model.refresh()` to re-load the expressions.

I don't use any custom operators in my TemplateEpxressionSpec, but maybe I still need to add some mappings to make the loading work?

Grateful for any help!

MilesCranmer avatar Mar 01 '25 18:03 MilesCranmer

Thank you for adding this as an issue Miles! Do you think this would be fixed in the short-term or should I do some manual workaround in the meantime?

ibengtsson avatar Mar 06 '25 08:03 ibengtsson

It is a deep issue on the Julia backend side of things, I don't think there are any workarounds possible.

The issue is basically because a template expression type refers to the definition of the function. See the E type parameter here. This is necessary for type stability in Julia. But it also means that when you restart Julia, even if you re-run the same code to create that function, the type itself might be a different object. So this kind of invalidates the state of the search.

So what we need to have is a custom serializer of AbstractExpression objects that can serialize the code of the template expression itself, and be able to re-instantiate under the same function object. Then we would need to define custom serializers for every part of the search state including Population and HallOfFame because they also implicitly contain the function object in their type.

So it's a bit tricky. I'm also not sure if there is any robust solution at all, because re-running the same code to define the function is kind of dependent on the current Julia runtime (such as what other libraries you are referring to). So it will be hard.

It is definitely possible though! It would probably need a day or two of full-time effort from me. And obviously more for those who don't know the codebase. (Maybe an LLM/Cursor can help get some of this done though!)

MilesCranmer avatar Mar 06 '25 14:03 MilesCranmer

Thank you for clarifying the issues! I'm not really fluent in Julia, but now I know were to start if I'll give it a try at least.

ibengtsson avatar Mar 10 '25 09:03 ibengtsson

Fixed on master as of 2.0.0-beta.3. Pickle round-trips of fitted TemplateExpressionSpec models work in a fresh process: fit, pickle, remove the run directory, load and predict with prediction parity, and lambda_format rebuilds from julia_state_stream_. Covered by test_template_expression_pickle_roundtrip_in_fresh_process (ca310584). The original confusion applied to v1, where dumping failed at dump time; current master raises nothing and reloads cleanly.

MilesCranmer avatar Aug 22 '26 16:08 MilesCranmer