Skip to content

[EXTENSION FOR CONTROL AND FILTERING]Code to reproduce the results of the paper In-context learning for model-free system identification

License

Notifications You must be signed in to change notification settings

buswayne/sysid-transformers-control

 
 

Repository files navigation

Control experiments

  • Linear
    • Fixed G
      • Decoder-only with e, u
      • Decoder-only with e_integral, u
      • Decoder-only with e, e_integral, u
      • Decoder-only with y, u, r
      • The one working $\rightarrow$ adding noise
      • Encoder-decoder with y, u, e
      • Encoder-decoder with y, u, r
  • Nonlinear
    • Fixed G
      • Decoder-only with e, u [nome_modello, validation loss]
      • Decoder-only with e_integral, u
      • Decoder-only with e, e_integral, u
      • Decoder-only with y, u, r
      • The one working $\rightarrow$ adding noise
      • Encoder-decoder with y, u, e
      • Encoder-decoder with y, u, r

For control and filtering

Required modifications:

in the library filterpy, EKF.py, modify these two functions:

def predict_x(self, u=0):
    """
    Predicts the next state of X. If you need to
    compute the next state yourself, override this function. You would
    need to do this, for example, if the usual Taylor expansion to
    generate F is not providing accurate results for you.
    """
    #self.x = dot(self.F, self.x) + dot(self.B, u)
    #self.x = dot(np.array(self.F(self.x, u)), self.x) + dot(np.array(self.B(self.x, u)), u)
    self.x = np.array(self.F(self.x, u))# + A_linearized * (self.z - self.x)

def predict(self, u=0):
    """
    Predict next state (prior) using the Kalman filter state propagation
    equations.

    Parameters
    ----------

    u : np.array
        Optional control vector. If non-zero, it is multiplied by B
        to create the control input into the system.
    """


    A_linearized = np.array(self.A(self.x, u))
    # self.P = dot(self.F(self.x, u), self.P).dot(self.F(self.x, u).T) + self.Q
    self.predict_x(u)          # this is x(k+1|k)
    self.P = dot(A_linearized, self.P).dot(A_linearized.T) + self.Q   # this is P(k+1|k)

    # save prior
    self.x_prior = np.copy(self.x)
    self.P_prior = np.copy(self.P)

In-context learning for model-free system identification

This repository contains the Python code to reproduce the results of the paper In-context learning for model-free system identification by Marco Forgione, Filippo Pura and Dario Piga.

We introduce the concept of model-free in-context learning for System Identification, where a meta model is trained to describe an entire class of dynamical systems, instead of a single instance. The meta model is able to understand the underlying dynamics from a context of provided input/output samples and to perform a task such as one-step-ahead prediction or multi-step-ahead simulation, that would otherwise require a model trained on each particular dataset.

One-step-ahead model-free prediction

Decoder-only (GPT-like) Transformer architecture for model-free one-step-ahead prediction:

Multi-step-ahead model-free simulation

Encoder-decoder (machine-translation-like) Transformer architecture for model-free multi-step-ahead simulation:

Main files

The training scripts are:

  • train_onestep_lin.py: Decoder-only Transformer for one-step-ahead prediction on the LTI system class
  • train_onestep_wh.py: Decoder-only Transformer for one-step-ahead prediction on the WH system class
  • train_sim_lin.py: Encoder-decoder Transformer for multi-step-ahead simulation on the LTI system class
  • train_sim_wh.py: Encoder-decoder Transformer for multi-step-ahead simulation on the WH system class

The scripts above except train_onestep_lin.py accept command-line arguments to customize the architecture and aspects of the training. For instance, the large one-step-ahead Transformer for the WH class described in the paper may be trained with the command:

python train_onestep_wh.py --out-file ckpt_onestep_wh_large --seq-len 1024  --n-layer 12 --n-head 12 --n-embd 768 --batch-size 20 --cuda-device cuda:1

Already-trained weights of all the Transformers discussed in the example reported in the paper are available as assets in the v0.2 Release.

Jupyter notebooks that load the trained model and make predictions/simulations on new data are also available in the repo, e.g. test_onestep_lin.ipynb for one-step prediction on the LTI class.

Software requirements

Experiments were performed on a Python 3.11 conda environment with

  • numpy
  • scipy
  • matplotlib
  • python-control
  • pytorch (v2.0.1)

These dependencies may be installed through the commands:

conda install numpy scipy matplotlib
conda install -c conda-forge control slycot
conda install pytorch -c pytorch

Hardware requirements

While the scripts can run on CPU, execution may be frustratingly slow. For faster training, a GPU is highly recommended. To run the paper's examples, we utilized a dedicated server equipped with an nVidia RTX 3090 GPU.

About

[EXTENSION FOR CONTROL AND FILTERING]Code to reproduce the results of the paper In-context learning for model-free system identification

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Jupyter Notebook 98.7%
  • Python 1.3%