Coder Social home page Coder Social logo

deep-symbolic-mathematics / tpsr Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 7.0 20.68 MB

[NeurIPS 2023] This is the official code for the paper "TPSR: Transformer-based Planning for Symbolic Regression"

Home Page: https://openreview.net/forum?id=0rVXQEeFEL

License: MIT License

Python 99.58% Shell 0.23% Makefile 0.19%
deep-learning language-model planning-algorithms symbolic-math symbolic-regression transformers interpretable-ml ai4math ai4science equation-discovery

tpsr's Introduction

Deep Symbolic Regression with Transformers & Lookahead Planning

Official Implementation of Transformer-based Planning for Symbolic Regression (NeurIPS 2023).

Overview

In this paper, we introduce TPSR, a novel transformer-based planning framework for symbolic regression by leveraging priors of large-scale pretrained models and incorporating lookahead planning. TPSR incorporates Monte Carlo Tree Search (MCTS) into the transformer decoding process of symbolic regression models. Unlike conventional decoding strategies, TPSR enables the integration of non-differentiable feedback, such as fitting accuracy and complexity, as external sources of knowledge into the transformer-based equation generation process.


TPSR uncovering the governing symbolic mathematics of data, providing enhanced extrapolation capabilities.

Preperation: Data and Pre-trained Backbone Models

  1. Download Pre-trained Models:
    • End-to-End (E2E) SR Transformer model is available here.
    • NeSymReS model is available here.

After downloading, extract both models to this directory. They should be located under the symbolicregression/weights/ and nesymres/weights/ sub-folders, respectively.

  1. Download Benchmark Datasets:

Extract the datasets to this directory, Feynman datasets should be in datasets/feynman/, and PMLB datasets should be in datasets/pmlb/.

Installation

To run the code with deafult E2E backbone model, create a conda environment and install the dependencies by running the following command.

conda create --name tpsr
conda activate tpsr
pip install -r requirements.txt

If you're interested to run experiments with NeSymReS backbone, install its additional dependencies from here. You can follow these steps:

conda create --name tpsr
conda activate tpsr
cd nesymres
pip install -e src/
pip install -r requirements.txt
pip install lightning==1.9

Run

We have created run.sh script to execute Transformer-based Planning for Automated Equation Discovery based on the reward defined in reward.py with the combination of equation's fitting accuracy and complexity. To run the script for different datasets, configure the following parameters:

Parameters Description Example Values
backbone_model Backbone Pre-trained Model Type (e2e/nesymres) e2e
eval_in_domain Evaluate backbone pre-trained model on In-Domain dataset (Yes/No) True/False
eval_mcts_in_domain Evaluate TPSR on In-Domain dataset (Yes/No) True/False
eval_on_pmlb Evaluate backbone pre-trained model on PMLB (Yes/No) True/False
eval_mcts_on_pmlb Evaluate TPSR on PMLB (Yes/No) True/False
horizon Horizon of lookahead planning (maxlen of equation tokens) 200
rollout Number of rollouts ($r$) in TPSR 3
num_beams Beam size ($b$) in TPSR's evaluation step to simulate completed equations 1
width Top-k ($k$) in TPSR's expansion step to expand tree width 3
no_seq_cache Use sequence caching (Yes/No) False
no_prefix_cache Use top-k caching (Yes/No) False
ucb_constant Exploration weight in UCB 1.0
uct_alg UCT algorithm $\in$ {uct, p_uct, var_p_uct} uct
max_input_points Maximum input points observed by pre-trained model ($N$) 200
max_number_bags Maximum number of bags for input points ($B$) 10
pmlb_data_type PMLB data group $\in$ {feynman, strogatz, black-box} feynman
target_noise Target noise added to y_to_fit in PMLB 0.0
beam_type Decoding type for pre-trained models $\in$ {search, sampling} sampling
beam_size Decoding size ($s$) for pre-trained models (beam size, or sampling size) 10
n_trees_to_refine Number of refinements in decodings $\in$ {1,..., $s$ } 10
prediction_sigmas Sigmas of extrapolation eval data sampling (In-domain) 1,2,4,8,16
eval_input_length_modulo Number of eval points (In-domain). Set to 50 yields $N_{test}=[50,100,150,200]$ per extrapolation range. 50

Run - PMLB Datasets (Feynman/ Strogatz/ Blackbox)

Pre-trained E2E Model (Sampling / Beam Search):

python run.py --eval_on_pmlb True \
                   --pmlb_data_type feynman \
                   --target_noise 0.0 \
                   --beam_type sampling \ # or search
                   --beam_size 10 \
                   --n_trees_to_refine 10 \
                   --max_input_points 200 \
                   --max_number_bags 10 \
                   --save_results True

Transformer-based Planning with E2E Backbone:

python run.py --eval_mcts_on_pmlb True \
                   --pmlb_data_type feynman \
                   --target_noise 0.0 \
                   --lam 0.1 \
                   --horizon 200 \
                   --width 3 \
                   --num_beams 1 \
                   --rollout 3 \
                   --no_seq_cache False \
                   --no_prefix_cache True \
                   --max_input_points 200 \
                   --max_number_bags 10 \
                   --save_results True

For running the code on Strogatz or Black-box datasets, simply adjust the pmlb_data_type parameter to either strogatz or blackbox. The commands provided above are set for the Feynman datasets. You can also modify the target_noise and other parameters to suit your experiments. Running each command saves the results for all datasets and metrics in a .csv file.

Run - In-Domain Datasets

In-Domain datasets are generated, following the validation data gneration protocol suggested in E2E. For details, refer to the generate_datapoints function here. You can also modify data generation parameters here. For example, you can adjust parameters like prediction_sigmas to control extrapolation. A sigma 1 aligns with the training data range, while >1 is for extrapolation ranges. The In-domain validation datasets are generated on-the-fly. For consistent evaluations across models, consider setting a fixed seed.

Pre-trained E2E Model (Sampling / Beam Search):

python run.py --eval_in_domain True \
                   --beam_type sampling \ # or search
                   --beam_size 10 \
                   --n_trees_to_refine 10 \
                   --max_input_points 200 \
                   --eval_input_length_modulo 50 \
                   --prediction_sigmas 1,2,4,8,16 \
                   --save_results True

Transformer-based Planning with E2E Backbone:

python run.py --eval_mcts_in_domain True \
                   --lam 0.1 \
                   --horizon 200 \
                   --width 3 \
                   --num_beams 1 \
                   --rollout 3 \
                   --no_seq_cache False \
                   --no_prefix_cache True \
                   --max_input_points 200 \
                   --eval_input_length_modulo 50 \
                   --prediction_sigmas 1,2,4,8,16 \
                   --save_results True \
                   --debug

Demo

We have also included a small demo that runs TPSR with both E2E and NesymReS backbones on your dataset. You can play with it here

E2E+TPSR:

python tpsr_demo.py --backbone_model e2e --no_seq_cache True --no_prefix_cache True

NeSymReS+TPSR:

python tpsr_demo.py --backbone_model nesymres --no_seq_cache True --no_prefix_cache True

Citation

If you find the paper or the repo helpful, please cite it with

@inproceedings{
tpsr2023,
title={Transformer-based Planning for Symbolic Regression},
author={Parshin Shojaee and Kazem Meidani and Amir Barati Farimani and Chandan K. Reddy},
booktitle={Thirty-seventh Conference on Neural Information Processing Systems},
year={2023},
url={https://openreview.net/forum?id=0rVXQEeFEL}
}

License

This repository is licensed under MIT licence.

This work is built on top of other open source projects, including End-to-End Symbolic Regression with Transformers (E2E), Neural Symbolic Regression that scales (NeSymReS), Dyna Gym, and transformers. We thank the original contributors of these works for open-sourcing their valuable source codes.

Contact Us

For any questions or issues, you are welcome to open an issue in this repo, or contact us at [email protected], and [email protected] .

tpsr's People

Contributors

mmeidani avatar parshinsh avatar parshinshojaee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tpsr's Issues

AttributeError: module 'signal' has no attribute 'SIGALRM'

Thanks for your great work.

When running the run.sh or run.py, I got an attribute error while it was rescaling the expression: refined_candidate[0]["predicted_tree"] = scaler.rescale_function(env, refined_candidate[0]["predicted_tree"], *scale_params[0])

Traceback (most recent call last):
  File "D:\xx\Anaconda\envs\deeplearning39\lib\site-packages\IPython\core\interactiveshell.py", line 3526, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-60ea49bf5158>", line 2, in <module>
    scaler.rescale_function(env, refined_candidate[0]["predicted_tree"], *scale_params[0])
  File "D:\xx\TPSR\timeout.py", line 16, in wrapper
    signal.signal(signal.SIGALRM, _handle_timeout)
AttributeError: module 'signal' has no attribute 'SIGALRM'

I would need assistance with fixing an error. Can you please guide me on how to resolve this issue?

ERROR: No matching distribution found for functorch==0.1.0

Hi,

Thanks for great research work.

I'm trying to install by following the instructions, but encounter an error, see below.

The conda create command works fine:

(base) joel@ubuntu:~/TPSR$ conda create --name tpsr python=3.7
Channels:
 - defaults
Platform: linux-aarch64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/joel/miniconda3/envs/tpsr

  added / updated specs:
    - python=3.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2022.12.7          |   py37hd43f75c_0         149 KB
    openssl-1.1.1w             |       h2f4d8fa_0         3.7 MB
    pip-22.3.1                 |   py37hd43f75c_0         2.7 MB
    python-3.7.16              |       h89984f6_0        44.8 MB
    setuptools-65.6.3          |   py37hd43f75c_0         1.1 MB
    wheel-0.38.4               |   py37hd43f75c_0          63 KB
    ------------------------------------------------------------
                                           Total:        52.5 MB

The following NEW packages will be INSTALLED:

  _libgcc_mutex      pkgs/main/linux-aarch64::_libgcc_mutex-0.1-main
  _openmp_mutex      pkgs/main/linux-aarch64::_openmp_mutex-5.1-51_gnu
  ca-certificates    pkgs/main/linux-aarch64::ca-certificates-2023.12.12-hd43f75c_0
  certifi            pkgs/main/linux-aarch64::certifi-2022.12.7-py37hd43f75c_0
  ld_impl_linux-aar~ pkgs/main/linux-aarch64::ld_impl_linux-aarch64-2.38-h8131f2d_1
  libffi             pkgs/main/linux-aarch64::libffi-3.4.4-h419075a_0
  libgcc-ng          pkgs/main/linux-aarch64::libgcc-ng-11.2.0-h1234567_1
  libgomp            pkgs/main/linux-aarch64::libgomp-11.2.0-h1234567_1
  libstdcxx-ng       pkgs/main/linux-aarch64::libstdcxx-ng-11.2.0-h1234567_1
  ncurses            pkgs/main/linux-aarch64::ncurses-6.4-h419075a_0
  openssl            pkgs/main/linux-aarch64::openssl-1.1.1w-h2f4d8fa_0
  pip                pkgs/main/linux-aarch64::pip-22.3.1-py37hd43f75c_0
  python             pkgs/main/linux-aarch64::python-3.7.16-h89984f6_0
  readline           pkgs/main/linux-aarch64::readline-8.2-h998d150_0
  setuptools         pkgs/main/linux-aarch64::setuptools-65.6.3-py37hd43f75c_0
  sqlite             pkgs/main/linux-aarch64::sqlite-3.41.2-h998d150_0
  tk                 pkgs/main/linux-aarch64::tk-8.6.12-h241ca14_0
  wheel              pkgs/main/linux-aarch64::wheel-0.38.4-py37hd43f75c_0
  xz                 pkgs/main/linux-aarch64::xz-5.4.5-h998d150_0
  zlib               pkgs/main/linux-aarch64::zlib-1.2.13-h998d150_0


Proceed ([y]/n)?


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate tpsr
#
# To deactivate an active environment, use
#
#     $ conda deactivate

However, the pip install -r requirements.txt command fails:

(base) joel@ubuntu:~/TPSR$ pip install -r requirements.txt
Defaulting to user installation because normal site-packages is not writeable
Collecting args==0.1.0 (from -r requirements.txt (line 1))
  Downloading args-0.1.0.tar.gz (3.0 kB)
  Preparing metadata (setup.py) ... done
Collecting attrs==21.4.0 (from -r requirements.txt (line 2))
  Downloading attrs-21.4.0-py2.py3-none-any.whl (60 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 60.6/60.6 kB 736.7 kB/s eta 0:00:00
Collecting backcall==0.2.0 (from -r requirements.txt (line 3))
  Downloading backcall-0.2.0-py2.py3-none-any.whl (11 kB)
Collecting cachetools==5.3.0 (from -r requirements.txt (line 4))
  Downloading cachetools-5.3.0-py3-none-any.whl (9.3 kB)
Collecting certifi==2021.10.8 (from -r requirements.txt (line 5))
  Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 149.2/149.2 kB 2.1 MB/s eta 0:00:00
Collecting charset-normalizer==2.0.12 (from -r requirements.txt (line 6))
  Downloading charset_normalizer-2.0.12-py3-none-any.whl (39 kB)
Collecting clint==0.5.1 (from -r requirements.txt (line 7))
  Downloading clint-0.5.1.tar.gz (29 kB)
  Preparing metadata (setup.py) ... done
Collecting cloudpickle==2.2.1 (from -r requirements.txt (line 8))
  Downloading cloudpickle-2.2.1-py3-none-any.whl (25 kB)
Collecting coverage==6.3.2 (from -r requirements.txt (line 9))
  Downloading coverage-6.3.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (211 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 211.9/211.9 kB 4.5 MB/s eta 0:00:00
Collecting cycler==0.11.0 (from -r requirements.txt (line 10))
  Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)
Collecting decorator==5.1.1 (from -r requirements.txt (line 11))
  Downloading decorator-5.1.1-py3-none-any.whl (9.1 kB)
Collecting deepspeed==0.8.0 (from -r requirements.txt (line 12))
  Downloading deepspeed-0.8.0.tar.gz (749 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 749.9/749.9 kB 3.4 MB/s eta 0:00:00
  Preparing metadata (setup.py) ... done
Collecting fonttools==4.31.2 (from -r requirements.txt (line 13))
  Downloading fonttools-4.31.2-py3-none-any.whl (899 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 899.5/899.5 kB 3.8 MB/s eta 0:00:00
ERROR: Could not find a version that satisfies the requirement functorch==0.1.0 (from versions: 1.13.0, 1.13.1, 2.0.0)
ERROR: No matching distribution found for functorch==0.1.0

Here is some information on my platform:

(base) joel@ubuntu:~/TPSR$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 22.04.3 LTS
Release:	22.04
Codename:	jammy
(base) joel@ubuntu:~/TPSR$ uname -a
Linux ubuntu 5.15.0-91-generic #101-Ubuntu SMP Tue Nov 14 13:29:11 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

Thanks for any advise on how to resolve!

/Joel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.