Spaces:
Running
Running
Add example for custom objective
Browse files317 unmodified lines318319320321322323324317 unmodified linesto make predictions with the most accurate expression.## 9. Additional featuresFor the many other features available in PySR, pleaseread the [Options section](options.md).317 unmodified lines318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441317 unmodified linesto make predictions with the most accurate expression.## 9. Custom objectivesYou can also pass a custom objectives as a snippet of Julia code,which might include symbolic manipulations or custom functional forms.These do not even need to be differentiable! First, let's look at thedefault objective used (a simplified version, without weightsand with mean square error), so that you can see how to write your own:```juliafunction default_objective(tree, dataset::Dataset{T,L}, options)::L where {T,L}(prediction, completion) = eval_tree_array(tree, dataset.X, options)if !completionreturn L(Inf)enddiffs = prediction .- dataset.yreturn sum(diffs .^ 2) / length(diffs)end```Here, the `where {T,L}` syntax defines the function for arbitrary types `T` and `L`.If you have `precision=32` (default) and pass in regular floating point data,then both `T` and `L` will be equal to `Float32`. If you pass in complex data,then `T` will be `ComplexF32` and `L` will be `Float32` (since we need to returna real number from the loss function). But, you don't need to worry about this, justmake sure to return a scalar number of type `L`.The `tree` argument is the current expression being evaluated. You can readabout the `tree` fields [here](https://astroautomata.com/SymbolicRegression.jl/stable/types/).For example, let's fix a symbolic form of an expression,as a rational function. i.e., $P(X)/Q(X)$ for polynomials $P$ and $Q$.```pythonobjective = """function my_custom_objective(tree, dataset::Dataset{T,L}, options) where {T,L}# Require root node to be binary, so we can split it,# otherwise return a large loss:tree.degree != 2 && return L(Inf)P = tree.lQ = tree.r# Evaluate numerator:P_prediction, flag = eval_tree_array(P, dataset.X, options)!flag && return L(Inf)# Evaluate denominator:Q_prediction, flag = eval_tree_array(Q, dataset.X, options)!flag && return L(Inf)# Impose functional form:prediction = P_prediction ./ Q_predictiondiffs = prediction .- dataset.yreturn sum(diffs .^ 2) / length(diffs)end"""model = PySRRegressor(niterations=100,binary_operators=["*", "+", "-"],full_objective=objective,)```> **Warning**: When using a custom objective like this that performs symbolic> manipulations, many functionalities of PySR will not work, such as `.sympy()`,> `.predict()`, etc. This is because the SymPy parsing does not know about> how you are manipulating the expression, so you will need to do this yourself.Note how we did not pass `/` as a binary operator; it will just be implicitin the functional form.Let's generate an equation of the form $\frac{x_0^2 x_1 - 2}{x_2^2 + 1}$:```pythonX = np.random.randn(1000, 3)y = (X[:, 0]**2 * X[:, 1] - 2) / (X[:, 2]**2 + 1)```Finally, let's fit:```pythonmodel.fit(X, y)```> Note that the printed equation is not the same as the evaluated equation,> because the printing functionality does not know about the functional form.We can get the string format with:```pythonmodel.get_best().equation```(or, you could use `model.equations_.iloc[-1].equation`)For me, this equation was:```text(((2.3554819 + -0.3554746) - (x1 * (x0 * x0))) - (-1.0000019 - (x2 * x2)))```looking at the bracket structure of the equation, we can see that the outermostbracket is split at the `-` operator (note that we ignore the root operator inthe evaluation, as we simply evaluated each argument and divided the result) into`((2.3554819 + -0.3554746) - (x1 * (x0 * x0)))` and`(-1.0000019 - (x2 * x2))`, meaning that our discovered equation isequal to:$\frac{x_0^2 x_1 - 2.0000073}{x_2^2 - 1.0000019}$, whichis nearly the same as the true equation!## 10. Additional featuresFor the many other features available in PySR, pleaseread the [Options section](options.md).
docs/examples.md+118-1