### Parametric Expression Specification Setup Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Sets up data and parameters for learning equations with category-specific parameters using `TemplateExpressionSpec`. This example aims to learn `y = alpha * sin(x1) + beta`, where alpha and beta vary by category. ```python import numpy as np from pysr import PySRRegressor, TemplateExpressionSpec # Create data with 2 features and 3 categories X = np.random.uniform(-3, 3, (1000, 2)) category = np.random.randint(0, 3, 1000) # Parameters for each category offsets = [0.1, 1.5, -0.5] scales = [1.0, 2.0, 0.5] ``` -------------------------------- ### Install Documentation Dependencies Source: https://github.com/milescranmer/pysr/blob/master/docs/README.md Install the necessary Python packages for building the documentation. Run this command in the base directory of the project. ```bash pip install -r docs/requirements.txt ``` -------------------------------- ### Install PyTorch Lightning Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Installs the PyTorch Lightning library, a lightweight PyTorch wrapper for high-performance AI research. This is a prerequisite for defining and training neural network models using PyTorch Lightning. ```shell !pip install pytorch_lightning ``` -------------------------------- ### Install PySR Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Installs the PySR library using pip. This command should be run in a compatible environment. ```bash !pip install -U pysr ``` -------------------------------- ### Install PySR with Pip Source: https://github.com/milescranmer/pysr/blob/master/README.md Install PySR using pip. Julia dependencies are installed upon first import. ```bash pip install pysr ``` -------------------------------- ### Run PySR Docker Container Source: https://github.com/milescranmer/pysr/blob/master/README.md Start a Docker container with PySR installed and run an IPython session within it. ```bash docker run -it --rm pysr ipython ``` -------------------------------- ### Install PySR with Modified Backend Source: https://github.com/milescranmer/pysr/blob/master/docs/src/backend.md Install PySR locally after configuring `juliapkg.json` to use your modified backend. ```bash cd PySR pip install . ``` -------------------------------- ### Serve Documentation Locally Source: https://github.com/milescranmer/pysr/blob/master/docs/README.md Use mkdocs to serve the documentation locally. This command will build the documentation and start a local web server, watching for changes. Run this command from the base directory. ```bash mkdocs serve -w pysr ``` -------------------------------- ### Install PySR in Editable Mode Source: https://github.com/milescranmer/pysr/blob/master/docs/README.md Install PySR in editable mode to allow for local development and testing. Run this command in the base directory. ```bash pip install -e . ``` -------------------------------- ### Template Expression Specification Example Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Demonstrates how to use `TemplateExpressionSpec` to guide PySR in learning equations of a specific structure. This example learns an equation of the form `sin(f(x1, x2)) + g(x3)`. ```python import numpy as np from pysr import PySRRegressor, TemplateExpressionSpec # Create data X = np.random.randn(1000, 3) y = np.sin(X[:, 0] + X[:, 1]) + X[:, 2]**2 # Define template: we want sin(f(x1, x2)) + g(x3) template = TemplateExpressionSpec( expressions=["f", "g"], variable_names=["x1", "x2", "x3"], combine="sin(f(x1, x2)) + g(x3)", ) model = PySRRegressor( expression_spec=template, binary_operators=["+", "*", "-", "/"], unary_operators=["sin"], maxsize=10, ) model.fit(X, y) ``` -------------------------------- ### Run PySR Docker Container Source: https://github.com/milescranmer/pysr/blob/master/README.md Launches an interactive Docker container, linking the current directory to '/data' and starting IPython. Use `--platform linux/amd64` if emulating architecture. ```bash docker run -it --rm -v "$PWD:/data" pysr ipython ``` -------------------------------- ### Install PySR with Conda Source: https://github.com/milescranmer/pysr/blob/master/README.md Install PySR using conda. Julia dependencies are installed upon first import. ```bash conda install -c conda-forge pysr ``` -------------------------------- ### Initialize PySRRegressor for Multi-Output Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Initializes PySRRegressor with specified binary and unary operators. This setup is for a multi-output regression task. ```python model = PySRRegressor( binary_operators=["+", "*"], ``` -------------------------------- ### Initialize and Fit PySRRegressor Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Initializes PySRRegressor with custom operators and fits the model to the data. This is a basic setup for symbolic regression. ```python model = PySRRegressor( niterations=30, binary_operators=["+", "*"], unary_operators=["cos", "exp", "sin"], **default_pysr_params, ) model.fit(X, y) ``` -------------------------------- ### Get LaTeX Expression for an Output Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Retrieves the LaTeX representation of the first symbolic expression found for a multi-output model. ```python model.latex()[0] ``` -------------------------------- ### Fit PySR with Weighted Data Source: https://github.com/milescranmer/pysr/blob/master/docs/src/options.md Assign weights to data rows using inverse uncertainty squared. This example demonstrates fitting the model with custom weights and specifying the number of processes. ```python sigma = ... weights = 1/sigma**2 model = PySRRegressor(procs=10) model.fit(X, y, weights=weights) ``` -------------------------------- ### Custom Binary Operator Example Source: https://github.com/milescranmer/pysr/blob/master/docs/src/operators.md Define a custom binary operator by providing a Julia function string. Ensure it handles Float32/Float64 and returns NaN for invalid inputs. ```python PySRRegressor( ..., unary_operators=["myfunction(x) = x^2"], binary_operators=["myotherfunction(x, y) = x^2*y"], extra_sympy_mappings={ "myfunction": lambda x: x**2, "myotherfunction": lambda x, y: x**2 * y, }, ) ``` -------------------------------- ### Add Julia Package using PySR Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Installs the 'Primes.jl' package into the Julia environment managed by PySR. This is necessary to use Julia functions from external packages. ```python from pysr import jl jl.seval(""" import Pkg Pkg.add("Primes") """) ``` -------------------------------- ### PySRRegressor with Dimensional Constraint Penalty Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Initializes a PySRRegressor model with specified operators, a custom loss function, and a significant penalty for dimensional violations. This setup is used to find dimensionally consistent expressions. ```python model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["square"], elementwise_loss=elementwise_loss, complexity_of_constants=2, maxsize=25, niterations=100, populations=50, # Amount to penalize dimensional violations: dimensional_constraint_penalty=10**5, ) ``` -------------------------------- ### Generate and Build Documentation Source Source: https://github.com/milescranmer/pysr/blob/master/docs/README.md Navigate to the docs directory, generate the documentation source files, and then return to the base directory. This step is necessary before serving the docs. ```bash cd docs && ./gen_docs.sh && cd .. ``` -------------------------------- ### Execute Basic Julia Code Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Demonstrates executing a simple Julia code block using the `%%julia` magic command. This initializes a Julia environment within the notebook. ```julia %%julia # Automatically activates Julia magic x = 1 println(x + 2) ``` -------------------------------- ### Initialize and Train PyTorch Model Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Initializes the SumNet model and sets training parameters like total steps and learning rate. It then uses PyTorch Lightning's Trainer to fit the model to the prepared data on a GPU. ```python pl.seed_everything(0) model = SumNet() model.total_steps = total_steps model.max_lr = 1e-2 ``` ```python trainer = pl.Trainer(max_steps=total_steps, accelerator="gpu", devices=1) ``` ```python trainer.fit(model, train_dataloaders=train, val_dataloaders=test) ``` -------------------------------- ### View TensorBoard Logs Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Command to launch TensorBoard to visualize the logs generated by PySR. ```bash tensorboard --logdir logs/ ``` -------------------------------- ### Get Best Expression in SymPy Format Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Retrieves the best discovered expression in SymPy format. This allows for symbolic manipulation and analysis. ```python model.sympy() ``` -------------------------------- ### Display Initial Weights Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Displays the first five calculated weights. This is a quick check to see the computed weights. ```python weights[:5] ``` -------------------------------- ### Get Specific Equation in SymPy Format Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Retrieves a specific discovered expression (by index) in SymPy format. Useful for analyzing equations other than the best one. ```python model.sympy(2) ``` -------------------------------- ### Build PySR Docker Image Source: https://github.com/milescranmer/pysr/blob/master/README.md Build the PySR Docker image by cloning the repository and running the docker build command within the repo's directory. ```bash docker build -t pysr . ``` -------------------------------- ### Generate Sample Data with NumPy Source: https://github.com/milescranmer/pysr/blob/master/README.md Generates a dataset with 100 data points and 5 features using NumPy. The target variable 'y' is defined by a mathematical expression involving features X[:, 3] and X[:, 0]. ```python import numpy as np X = 2 * np.random.randn(100, 5) y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 0.5 ``` -------------------------------- ### Custom Square Root Operator Source: https://github.com/milescranmer/pysr/blob/master/docs/src/operators.md Example of a custom square root operator that handles non-negative inputs and returns NaN for negative inputs, ensuring compatibility with Float32/Float64. ```julia my_sqrt(x) = x >= 0 ? sqrt(x) : convert(typeof(x), NaN) ``` -------------------------------- ### Initialize PySRRegressor Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Sets up random data and initializes a PySRRegressor model with specific parameters for deterministic behavior, serial parallelism, and limited iterations for testing purposes. ```python rstate = np.random.RandomState(0) X = np.random.randn(10, 2) y = np.random.randn(10) model = PySRRegressor(deterministic=True, parallelism="serial", random_state=0, verbosity=0, progress=False, niterations=1, ncycles_per_iteration=1) str(model) ``` -------------------------------- ### Get Best Equation String Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Retrieves the string representation of the best equation found by the PySR model. This method is useful when the model uses a custom objective function. ```python model.get_best().equation ``` -------------------------------- ### Initialize Julia Runtime Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Initializes the Julia runtime for PySR. This is a prerequisite for running Julia code. ```python from pysr import jl ``` -------------------------------- ### Checkout Specific Versions Source: https://github.com/milescranmer/pysr/blob/master/docs/src/backend.md Navigate to the cloned repositories and checkout specific versions of PySR and its backend. ```bash cd PySR git checkout # You can see the current backend version in `pysr/juliapkg.json` cd ../SymbolicRegression.jl git checkout ``` -------------------------------- ### Load Data with Pickle Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Loads data from 'nnet_recordings.pkl' and assigns specific variables (f_input, f_output, g_input, g_output) for use in symbolic regression. ```python import pickle as pkl net_recordings = pkl.load(open("nnet_recordings.pkl", "rb")) f_input = nnet_recordings["f_input"] f_output = nnet_recordings["f_output"] g_input = nnet_recordings["g_input"] g_output = nnet_recordings["g_output"] ``` -------------------------------- ### Build Apptainer Container for PySR Source: https://github.com/milescranmer/pysr/blob/master/README.md Use this command to build a PySR Apptainer container image from an Apptainer.def file. This is useful for cluster environments without root access. ```bash apptainer build --notest pysr.sif Apptainer.def ``` -------------------------------- ### Prepare DataLoaders Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Splits the generated data into training and testing sets and creates PyTorch DataLoaders. This prepares the data for efficient batch processing during model training and validation. ```python from multiprocessing import cpu_count Xt = torch.tensor(X).float() zt = torch.tensor(z).float() X_train, X_test, z_train, z_test = train_test_split(Xt, zt, random_state=0) train_set = TensorDataset(X_train, z_train) train = DataLoader( train_set, batch_size=128, num_workers=cpu_count(), shuffle=True, pin_memory=True ) test_set = TensorDataset(X_test, z_test) test = DataLoader(test_set, batch_size=256, num_workers=cpu_count(), pin_memory=True) ``` -------------------------------- ### Configure juliapkg.json for Local Development Source: https://github.com/milescranmer/pysr/blob/master/docs/src/backend.md Modify the `pysr/juliapkg.json` file to point to your local copy of the SymbolicRegression.jl backend for development. ```json ... "packages": { "SymbolicRegression": { "uuid": "8254be44-1295-4e6a-a16d-46603ac705cb", "dev": true, "path": "/path/to/SymbolicRegression.jl" }, ... ``` -------------------------------- ### Execute Single Julia Line Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Shows how to execute a single line of Julia code using the `%julia` magic command, building upon previously defined Julia variables. ```julia %julia println(x + 3) ``` -------------------------------- ### Generate Sample Data for Equation Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Generates random input data X and corresponding target values y for a specific equation y = (X[:, 0]**2 * X[:, 1] - 2) / (X[:, 2]**2 + 1). ```python import numpy as np X = np.random.randn(1000, 3) y = (X[:, 0]**2 * X[:, 1] - 2) / (X[:, 2]**2 + 1) ``` -------------------------------- ### Import Julia Function using %%julia magic Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Imports the 'prime' function from the 'Primes.jl' package using the %%julia IPython magic command. This is an alternative to using jl.seval for executing Julia code in notebooks. ```julia %julia using Primes: prime ``` -------------------------------- ### Build PySR Docker Image Source: https://github.com/milescranmer/pysr/blob/master/README.md Builds a Docker image named 'pysr' for your system's architecture. This image includes IPython for interactive use. ```bash docker build -t pysr . ``` -------------------------------- ### Call Custom Julia Function Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Demonstrates calling the previously defined custom Julia function `my_loss` using the `%julia` magic command. ```julia %julia my_loss(2) ``` -------------------------------- ### Load PySR Model from File Source: https://github.com/milescranmer/pysr/blob/master/README.md Loads a previously saved PySR model from a pickle file. This allows resuming training or using a pre-trained model. ```python model = PySRRegressor.from_file("hall_of_fame.2022-08-10_100832.281.pkl") ``` -------------------------------- ### Save Data with Pickle Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Saves the 'nnet_recordings' data to a file named 'nnet_recordings.pkl' using pickle for later use. ```python import pickle as pkl with open("nnet_recordings.pkl", "wb") as f: pkl.dump(nnet_recordings, f) ``` -------------------------------- ### Create Symbolic Regression Dataset Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Creates input features (X) and target values (y) for symbolic regression. The target values are generated based on a known relationship involving prime numbers and include a small amount of random noise. ```python import numpy as np X = np.random.randint(0, 100, 100)[:, None] y = [primes[3*X[i, 0] + 1] - 5 + np.random.randn()*0.001 for i in range(100)] ``` -------------------------------- ### Configure PySR Parameters Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Sets default parameters for PySR, including 30 populations and 'best' model selection strategy. These parameters control the evolution and selection process of symbolic expressions. ```python default_pysr_params = dict( populations=30, model_selection="best", ) ``` -------------------------------- ### Generate Dataset Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Creates a sample dataset with 100 samples and 5 features using numpy. The target variable 'y' is a function of the features, including a cosine and a squared term. ```python # Dataset np.random.seed(0) X = 2 * np.random.randn(100, 5) y = 2.5382 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 2 ``` -------------------------------- ### Detailed PySRRegressor Configuration Source: https://github.com/milescranmer/pysr/blob/master/README.md This snippet demonstrates a comprehensive configuration of PySRRegressor, utilizing numerous parameters for advanced customization. It's intended as a feature demonstration and not for direct use. ```python model = PySRRegressor( populations=8, # ^ Assuming we have 4 cores, this means 2 populations per core, so one is always running. population_size=50, # ^ Slightly larger populations, for greater diversity. ncycles_per_iteration=500, # ^ Generations between migrations. niterations=10000000, # Run forever early_stop_condition=( "stop_if(loss, complexity) = loss < 1e-6 && complexity < 10" # Stop early if we find a good and simple equation ), timeout_in_seconds=60 * 60 * 24, # ^ Alternatively, stop after 24 hours have passed. maxsize=50, # ^ Allow greater complexity. maxdepth=10, # ^ But, avoid deep nesting. binary_operators=["*", "+", "-", "/"], unary_operators=["square", "cube", "exp", "cos2(x)=cos(x)^2"], constraints={ "/": (-1, 9), "square": 9, "cube": 9, "exp": 9, }, # ^ Limit the complexity within each argument. # "inv": (-1, 9) states that the numerator has no constraint, # but the denominator has a max complexity of 9. # "exp": 9 simply states that `exp` can only have # an expression of complexity 9 as input. nested_constraints={ "square": {"square": 1, "cube": 1, "exp": 0}, "cube": {"square": 1, "cube": 1, "exp": 0}, "exp": {"square": 1, "cube": 1, "exp": 0}, }, # ^ Nesting constraints on operators. For example, # "square(exp(x))" is not allowed, since "square": {"exp": 0}. complexity_of_operators={"": 2, "exp": 3}, # ^ Custom complexity of particular operators. complexity_of_constants=2, # ^ Punish constants more than variables select_k_features=4, # ^ Train on only the 4 most important features progress=True, # ^ Can set to false if printing to a file. weight_randomize=0.1, # ^ Randomize the tree much more frequently cluster_manager=None, # ^ Can be set to, e.g., "slurm", to run a slurm # cluster. Just launch one script from the head node. precision=64, # ^ Higher precision calculations. warm_start=True, # ^ Start from where left off. turbo=True, # ^ Faster evaluation (experimental) extra_sympy_mappings={"cos2": lambda x: sympy.cos(x)**2}, # extra_torch_mappings={sympy.cos: torch.cos}, # ^ Not needed as cos already defined, but this # is how you define custom torch operators. # extra_jax_mappings={sympy.cos: "jnp.cos"}, # ^ For JAX, one passes a string. ) ``` -------------------------------- ### Import Julia Package Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Imports the 'Primes.jl' package into the Julia runtime. ```python jl.seval("import Primes") ``` -------------------------------- ### Run PySR Benchmarks Source: https://github.com/milescranmer/pysr/blob/master/benchmarks/README.md This bash script iterates through tags in 'tags.txt', checks out each tag, and runs a benchmark script. It is designed to run benchmarks on a cluster node. ```bash for x in $(cat tags.txt); do sleep 120 && git checkout $x &> /dev/null && nohup ./benchmark.sh > performance_v3_$x.txt &; done ``` -------------------------------- ### Use Built-in Loss Function (LPDistLoss) Source: https://github.com/milescranmer/pysr/blob/master/docs/src/options.md Utilize built-in loss functions for efficiency, such as the L3 norm (LPDistLoss{3}). These can also be used with weights. ```python PySRRegressor(..., elementwise_loss="LPDistLoss{3}()") ``` ```python model = PySRRegressor(..., weights=weights, elementwise_loss="LPDistLoss{3}()") model.fit(..., weights=weights) ``` -------------------------------- ### Run Apptainer Container for PySR Source: https://github.com/milescranmer/pysr/blob/master/README.md Launch the PySR Apptainer container using this command after building it. This allows you to run PySR within the isolated environment. ```bash apptainer run pysr.sif ``` -------------------------------- ### Clone Backend and PySR Repositories Source: https://github.com/milescranmer/pysr/blob/master/docs/src/backend.md Clone the source code for both SymbolicRegression.jl and PySR to begin customization. ```bash git clone https://github.com/MilesCranmer/SymbolicRegression.jl git clone https://github.com/MilesCranmer/PySR ``` -------------------------------- ### Load Saved PySR Model from Pickle File Source: https://github.com/milescranmer/pysr/blob/master/docs/src/options.md Load a previously saved PySR model state from a pickle file using the `from_file` class method. ```python model = PySRRegressor.from_file(pickle_filename) ``` -------------------------------- ### Import PySR Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Imports the PySR library. This is a necessary step before using any PySR functionalities. ```python import pysr ``` -------------------------------- ### Symbolic Regression for Multiple Outputs Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Performs symbolic regression for multiple output targets simultaneously. Requires NumPy and custom operator definitions. ```python X = 2 * np.random.randn(100, 5) y = 1 / X[:, [0, 1, 2]] model = PySRRegressor( binary_operators=["+", "*"], unary_operators=["inv(x) = 1/x"], extra_sympy_mappings={"inv": lambda x: 1/x}, ) model.fit(X, y) ``` -------------------------------- ### PySR API Call for Benchmarking Source: https://github.com/milescranmer/pysr/blob/master/benchmarks/README.md This Python code demonstrates the API call to the PySR library used within the benchmark script. It specifies operators, iterations, and other performance-related parameters. ```python eq = pysr(X, y, binary_operators=["plus", "mult", "div", "pow"], unary_operators=["sin"], niterations=20, procs=4, parsimony=1e-10, population_size=1000, ncyclesperiteration=1000) ``` -------------------------------- ### Import Libraries and Load Julia Extension Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Imports necessary libraries for PySR and NumPy, and loads the juliacall extension for Jupyter. Set PYSR_AUTOLOAD_EXTENSIONS=no to disable automatic extension loading. ```python # NBVAL_IGNORE_OUTPUT import numpy as np from pysr import PySRRegressor, jl ``` -------------------------------- ### Initialize PySRRegressor Model Source: https://github.com/milescranmer/pysr/blob/master/README.md Initializes the PySRRegressor with specified parameters including maximum equation size, number of iterations, binary and unary operators, custom operators, and a custom elementwise loss function. ```python from pysr import PySRRegressor model = PySRRegressor( maxsize=20, niterations=40, # < Increase me for better results binary_operators=["+", "*"], unary_operators=[ "cos", "exp", "sin", "inv(x) = 1/x", # ^ Custom operator (julia syntax) ], extra_sympy_mappings={"inv": lambda x: 1 / x}, # ^ Define operator for SymPy as well elementwise_loss="loss(prediction, target) = (prediction - target)^2", # ^ Custom loss function (julia syntax) ) ``` -------------------------------- ### Generate Sample Data for Regression Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Generates a synthetic dataset for regression tasks, including input features X, target variable y, and noise sigma. Sets a random seed for reproducibility. ```python np.random.seed(0) N = 3000 upper_sigma = 5 X = 2 * np.random.rand(N, 5) sigma = np.random.rand(N) * (5 - 0.1) + 0.1 eps = sigma * np.random.randn(N) y = 5 * np.cos(3.5 * X[:, 0]) - 1.3 + eps ``` -------------------------------- ### Build PySR Docker Image with Specific Versions Source: https://github.com/milescranmer/pysr/blob/master/README.md Builds a Docker image named 'pysr' with specified versions of Python and Julia. Use `--build-arg` to set `JLVERSION` and `PYVERSION`. ```bash docker build -t pysr --build-arg JLVERSION=1.10.0 --build-arg PYVERSION=3.11.6 . ``` -------------------------------- ### Using Differential Operators for Integration Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Shows how to define a PySR model that uses differential operators (D) to find an indefinite integral of a given function. The 'combine' parameter specifies the differential equation to solve. ```python import numpy as np from pysr import PySRRegressor, TemplateExpressionSpec x = np.random.uniform(1, 10, (1000,)) # Integrand sampling points y = 1 / (x**2 * np.sqrt(x**2 - 1)) # Evaluation of the integrand expression_spec = TemplateExpressionSpec( expressions=["f"], variable_names=["x"], combine="df = D(f, 1); df(x)", ) model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["sqrt"], expression_spec=expression_spec, maxsize=20, ) model.fit(x[:, np.newaxis], y) ``` -------------------------------- ### PySRRegressor.from_file Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Loads a PySRRegressor model from a file. ```APIDOC ## PySRRegressor.from_file ### Description Loads a PySRRegressor model from a file. ### Method (Not specified, assumed to be a method call) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### Print Learned Equations Source: https://github.com/milescranmer/pysr/blob/master/README.md Prints the learned equations from the trained PySR model. The output includes pick, score, equation, loss, and complexity for each equation found. ```python print(model) ``` -------------------------------- ### Evaluating Individual PySR Expressions Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Demonstrates how to extract specific expression trees (e.g., f1, shared) from a PySR model and evaluate them at given input points using Julia's compiled functions. ```python from pysr import jl from pysr.julia_helpers import jl_array SR = jl.SymbolicRegression # Get individual trees f1_tree = julia_expr.trees.f1 shared_tree = julia_expr.trees.shared # Evaluate at specific points (x1=1, x2=2, x3=3) test_inputs = jl_array(np.array([[1.0], [2.0], [3.0]])) f1_result, _ = SR.eval_tree_array(f1_tree, test_inputs, model.julia_options_) shared_result, _ = SR.eval_tree_array(shared_tree, test_inputs, model.julia_options_) print(f"f1 at (1,2,3): {f1_result[0]}") # Should be ~4.0 for x2^2 print(f"shared at (1,2,3): {shared_result[0]}") # Should be ~2.718 for exp(1) ``` -------------------------------- ### Define Neural Network Components Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Defines a multi-layer perceptron (MLP) and a SumNet class using PyTorch and PyTorch Lightning. The SumNet incorporates a sum inductive bias, learning functions 'g' and 'f' separately before combining them. ```python import torch from torch import nn, optim from torch.nn import functional as F from torch.utils.data import DataLoader, TensorDataset import pytorch_lightning as pl hidden = 128 total_steps = 50_000 def mlp(size_in, size_out, act=nn.ReLU): return nn.Sequential( nn.Linear(size_in, hidden), act(), nn.Linear(hidden, hidden), act(), nn.Linear(hidden, hidden), act(), nn.Linear(hidden, size_out), ) class SumNet(pl.LightningModule): def __init__(self): super().__init__() ######################################################## # The same inductive bias as above! self.g = mlp(5, 1) self.f = mlp(1, 1) def forward(self, x): y_i = self.g(x)[:, :, 0] y = torch.sum(y_i, dim=1, keepdim=True) / y_i.shape[1] z = self.f(y) return z[:, 0] ######################################################## # PyTorch Lightning bookkeeping: def training_step(self, batch, batch_idx): x, z = batch predicted_z = self(x) loss = F.mse_loss(predicted_z, z) return loss def validation_step(self, batch, batch_idx): return self.training_step(batch, batch_idx) def configure_optimizers(self): optimizer = torch.optim.Adam(self.parameters(), lr=self.max_lr) scheduler = { "scheduler": torch.optim.lr_scheduler.OneCycleLR( optimizer, max_lr=self.max_lr, total_steps=self.trainer.estimated_stepping_batches, final_div_factor=1e4, ), "interval": "step", } return [optimizer], [scheduler] ``` -------------------------------- ### Fit PySR Model with TensorBoard Logging Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Fits a PySR model while logging search progress and hyperparameters to TensorBoard. The logger is configured to write to a specified directory at a given interval. ```python import numpy as np from pysr import PySRRegressor, TensorBoardLoggerSpec rstate = np.random.RandomState(42) # Uniform dist between -3 and 3: X = rstate.uniform(-3, 3, (1000, 2)) y = np.exp(X[:, 0]) + X[:, 1] # Create a logger that writes to "logs/run*": logger_spec = TensorBoardLoggerSpec( log_dir="logs/run", log_interval=10, # Log every 10 iterations ) model = PySRRegressor( binary_operators=["+", "*", "-", "/"], logger_spec=logger_spec, ) model.fit(X, y) ``` -------------------------------- ### Import Libraries Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Imports essential libraries for data manipulation, plotting, and PySR regression. Includes sympy, numpy, matplotlib, PySRRegressor, and train_test_split. ```python import sympy import numpy as np from matplotlib import pyplot as plt from pysr import PySRRegressor from sklearn.model_selection import train_test_split ``` -------------------------------- ### Inspect PySR Equations Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Retrieves and displays the 'complexity', 'loss', and 'equation' columns from the fitted PySR model's equations. ```python model.equations_[["complexity", "loss", "equation"]] ``` -------------------------------- ### Denoising Symbolic Regression Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Applies denoising to the target data before symbolic regression using a Gaussian process. Fits a model with the 'denoise=True' option. ```python X = np.random.randn(100, 5) noise = np.random.randn(100) * 0.1 y = np.exp(X[:, 0]) + X[:, 1] + X[:, 2] + noise ``` ```python model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["exp"], denoise=True, ) model.fit(X, y) print(model) ``` -------------------------------- ### Print Discovered Equations Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Prints all discovered expressions from the fitted PySRRegressor model. This is useful for inspecting the results. ```python model ``` -------------------------------- ### Predict with Default and Specific Equations Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Uses the `predict` method to generate predictions with the default best equation and a specified equation by index. Calculates Mean Squared Error for both. ```python ypredict = model.predict(X) ypredict_simpler = model.predict(X, 2) print("Default selection MSE:", np.power(ypredict - y, 2).mean()) print("Manual selection MSE for index 2:", np.power(ypredict_simpler - y, 2).mean()) ``` -------------------------------- ### Generate X, y Data for PySR Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Creates input features (X) and target values (y) for PySR. The target values are generated using the pre-computed primes and include a small amount of random noise. ```python import numpy as np X = np.random.randint(0, 100, 100)[:, None] y = [primes[3 * X[i, 0] + 1] - 5 + np.random.randn() * 0.001 for i in range(100)] ``` -------------------------------- ### Import Libraries for PySR Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Imports necessary libraries, including NumPy for numerical operations and PySR for symbolic regression. ```python import numpy as np from pysr import * ``` -------------------------------- ### Data Generation with Astropy Units Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Generates sample data for Newton's law of gravitation using astropy for physical units. This data is used to demonstrate dimensional constraints in PySR. ```python import numpy as np from astropy import units as u, constants as const M = (np.random.rand(100) + 0.1) * const.M_sun m = 100 * (np.random.rand(100) + 0.1) * u.kg r = (np.random.rand(100) + 0.1) * const.R_earth G = const.G F = G * M * m / r**2 ``` -------------------------------- ### Fit PySRRegressor with Custom Weighted Loss Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Initializes and fits PySRRegressor using a custom element-wise loss function that incorporates weights. This allows for fitting data with heteroscedastic noise. ```python model = PySRRegressor( elementwise_loss="myloss(x, y, w) = w * abs(x - y)", # Custom loss function with weights. niterations=20, populations=20, # Use more populations binary_operators=["+", "*"], unary_operators=["cos"], ) model.fit(X, y, weights=weights) ``` -------------------------------- ### Train PySR Model with Complex Numbers Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Demonstrates training a PySRRegressor model using complex-valued input data (X) and target values (y). The model automatically handles complex numbers and learns complex-valued constants. ```python import numpy as np X = np.random.randn(100, 1) + 1j * np.random.randn(100, 1) y = (1 + 2j) * np.cos(X[:, 0] * (0.5 - 0.2j)) model = PySRRegressor( binary_operators=["+", "-", "*"], unary_operators=["cos"], niterations=100, ) model.fit(X, y) ``` -------------------------------- ### TensorBoardLoggerSpec Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Specification for TensorBoard logging. ```APIDOC ## TensorBoardLoggerSpec ### Description Specification for TensorBoard logging. ### Method (Not specified, assumed to be a class) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### PySRRegressor.latex Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Returns the symbolic expression of the model in LaTeX format. ```APIDOC ## PySRRegressor.latex ### Description Returns the symbolic expression of the model in LaTeX format. ### Method (Not specified, assumed to be a method call) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### Symbolic Regression with Custom Unary Operator Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Uses a custom unary operator 'inv(x)' for symbolic regression. Requires NumPy and defines a custom mapping for the operator. ```python X = 2 * np.random.randn(100, 5) y = 1 / X[:, 0] model = PySRRegressor( binary_operators=["+", "*"], unary_operators=["inv(x) = 1/x"], extra_sympy_mappings={"inv": lambda x: 1/x}, ) model.fit(X, y) print(model) ``` -------------------------------- ### Exporting to JAX Model Source: https://github.com/milescranmer/pysr/blob/master/docs/src/options.md Obtain a JAX callable function and its parameters from the fitted PySR model. This allows for numerical evaluation and training within the JAX ecosystem. Ensure custom operators are mapped. ```python jax_model = model.jax() jax_model['callable'](X, jax_model['parameters']) ``` -------------------------------- ### Train PySR Model Source: https://github.com/milescranmer/pysr/blob/master/README.md Trains the initialized PySRRegressor model on the provided dataset (X, y). This process launches a Julia process for multithreaded equation search. ```python model.fit(X, y) ``` -------------------------------- ### Generate Prime Number Dataset Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Generates a dictionary of prime numbers using the custom Julia 'p' function, mapping integers to their corresponding primes. This is used to create a dataset for symbolic regression. ```python primes = {i: jl.p(i*1.0) for i in range(1, 999)} ``` -------------------------------- ### Define Custom Julia Loss Function Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Illustrates defining a custom Julia function, `my_loss`, within a Jupyter notebook using the `%%julia` magic. This function calculates the square of its input. ```julia %%julia function my_loss(x) x ^ 2 end ``` -------------------------------- ### Create Dataset using Custom Julia Operator Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Generates a dictionary of prime numbers by calling the custom Julia 'p' function through PySR's jl interface. This is used to create a dataset for symbolic regression. ```python primes = {i: jl.p(i * 1.0) for i in range(1, 999)} ``` -------------------------------- ### Plotting Predictions vs. Truth Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Visualizes the performance of the symbolic regression model by plotting predicted values against the true target values. Requires Matplotlib. ```python from matplotlib import pyplot as plt plt.scatter(y[:, 0], model.predict(X)[:, 0]) plt.xlabel('Truth') plt.ylabel('Prediction') plt.show() ``` -------------------------------- ### Configure and Train PySR Model with Custom Operator Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Configures and trains a PySRRegressor model. It includes a custom unary operator 'p' and its corresponding SymPy mapping. The model is trained on the generated dataset. ```python from pysr import PySRRegressor import sympy class sympy_p(sympy.Function): pass model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["p"], niterations=100, extra_sympy_mappings={"p": sympy_p} ) model.fit(X, y) ``` -------------------------------- ### PySRRegressor.latex_table Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Returns the symbolic expression of the model as a LaTeX table. ```APIDOC ## PySRRegressor.latex_table ### Description Returns the symbolic expression of the model as a LaTeX table. ### Method (Not specified, assumed to be a method call) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### Initialize PySRRegressor with Custom Operator Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Initializes the PySRRegressor with specified binary and unary operators. It includes the custom 'p' operator and maps it to a SymPy function for equation simplification. ```python from pysr import PySRRegressor import sympy class sympy_p(sympy.Function): pass model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["p"], niterations=20, extra_sympy_mappings={"p": sympy_p}, ) ``` -------------------------------- ### Visualize Fit with Selected Equation Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Plots the original data points and the predictions made by the PySR model using the selected best equation. This visually assesses the quality of the fit. ```python plt.scatter(X[:, 0], y, alpha=0.1) y_prediction = model.predict(X, index=best_idx) plt.scatter(X[:, 0], y_prediction) ``` -------------------------------- ### Set LD_LIBRARY_PATH for GLIBCXX Issues Source: https://github.com/milescranmer/pysr/blob/master/README.md Export the LD_LIBRARY_PATH to include the Julia lib directory to resolve GLIBCXX version conflicts. This is typically added to your .bashrc or .zshrc file. ```bash export LD_LIBRARY_PATH=$HOME/.julia/juliaup/julia-1.10.0+0.x64.linux.gnu/lib/julia/:$LD_LIBRARY_PATH ``` -------------------------------- ### PySRRegressor.fit Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Controls a symbolic regression search with various options. ```APIDOC ## PySRRegressor.fit ### Description Controls a symbolic regression search with various options. ### Method (Not specified, assumed to be a method call) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### Fit PySR Model and Check Output Type Source: https://github.com/milescranmer/pysr/blob/master/pysr/test/test_nb.ipynb Fits the initialized PySRRegressor model to the generated data and checks the type of the `equations_` attribute after fitting. This confirms that the model has successfully generated equations. ```python model.fit(X, y) type(model.equations_).__name__ ``` -------------------------------- ### BibTeX Entry for Symbolic Distillation of Neural Networks Source: https://github.com/milescranmer/pysr/blob/master/CITATION.md Use this BibTeX entry to cite research on symbolic distillation of neural networks. It includes title, authors, journal, year, and arXiv details. ```bibtex @article{cranmerDiscovering2020, title={Discovering Symbolic Models from Deep Learning with Inductive Biases}, author={Miles Cranmer and Alvaro Sanchez-Gonzalez and Peter Battaglia and Rui Xu and Kyle Cranmer and David Spergel and Shirley Ho}, journal={NeurIPS 2020}, year={2020}, eprint={2006.11287}, archivePrefix={arXiv}, primaryClass={cs.LG} } ``` -------------------------------- ### PySRRegressor.refresh Source: https://github.com/milescranmer/pysr/blob/master/docs/src/_api_template.md Refreshes the symbolic regression model. ```APIDOC ## PySRRegressor.refresh ### Description Refreshes the symbolic regression model. ### Method (Not specified, assumed to be a method call) ### Parameters (Parameters not explicitly detailed in the source) ### Request Example (Not specified) ### Response (Not specified) ``` -------------------------------- ### Import Julia Function using PySR Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Imports the 'prime' function from the 'Primes.jl' package into the current Julia session managed by PySR. This makes the function available for use. ```python jl.seval("using Primes: prime") ``` -------------------------------- ### Simple Symbolic Regression Search Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Finds a symbolic expression for a given dataset using default binary operators. Requires NumPy. ```python X = 2 * np.random.randn(100, 5) y = 2 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 2 model = PySRRegressor(binary_operators=["+", "-", "*", "/"]) model.fit(X, y) print(model) ``` -------------------------------- ### Exporting to PyTorch Model Source: https://github.com/milescranmer/pysr/blob/master/docs/src/options.md Convert the fitted PySR model into a PyTorch module for further use, such as training or integration into a larger PyTorch workflow. Ensure custom operators are mapped. ```python torch_model = model.pytorch() torch_model(X) ``` -------------------------------- ### Fitting PySR Model with Unit Information Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Fits the PySRRegressor model using numerical data and specifying the units for input features (X) and the target variable (y). This enables the model to enforce dimensional consistency during the search. ```python # Get numerical arrays to fit: X = pd.DataFrame(dict( M=M.to("M_sun").value, m=m.to("kg").value, r=r.to("R_earth").value, )) y = F.value model.fit( X, y, X_units=["Constants.M_sun", "kg", "Constants.R_earth"], y_units="kg * m / s^2" ) ``` -------------------------------- ### Generate Multi-Output Data Source: https://github.com/milescranmer/pysr/blob/master/examples/pysr_demo.ipynb Generates synthetic data for a multi-output regression problem where the target variable y has multiple columns, each dependent on different input features. ```python X = 2 * np.random.randn(100, 5) y = 1 / X[:, [0, 1, 2]] ``` -------------------------------- ### Feature Selection for Symbolic Regression Source: https://github.com/milescranmer/pysr/blob/master/docs/src/examples.md Demonstrates PySR's built-in feature selection mechanism to handle high-dimensional data. Fits a model with a limited number of selected features. ```python X = np.random.randn(300, 30) y = X[:, 3]**2 - X[:, 19]**2 + 1.5 ``` ```python model = PySRRegressor( binary_operators=["+", "-", "*", "/"], unary_operators=["exp"], select_k_features=5, ) ``` ```python model.fit(X, y) ```