Build sim from data for use in reinforcement learning and bonsai platform for machine teaching.
Перейти к файлу
Ali Zaidi 4041ff825e UPDATE: initial states in episode_start and in cartpole-updated-simparam.yaml
initial states should be defined before episode_step. Previously these were randomly initialized as standard gaussians in episode_start using the keys defined by simulator.states. Now a new value is provdied in simulator.yaml called initial_states which takes a dictionary of initial_values. Moreover, episode_start looks in the `config` Dict (for instance when using scenario parameters in Inkling from lessons) and updates initial_states if provided.

TODO: update documentation and all simulator.yaml files
2021-07-13 17:07:21 -07:00
.github UPDATE: pytests with full requirements 2021-07-13 09:50:00 -07:00
.tours UPDATE: moab example 2021-01-19 12:46:58 -08:00
conf UPDATE: initial states in episode_start and in cartpole-updated-simparam.yaml 2021-07-13 17:07:21 -07:00
csv_data ADD: quanser data collected from MBA 2021-07-07 09:24:54 -07:00
img Adding jupyter notebook for presales qualification of data driven sim creation. It is using the nbgrader package to grade its feasibility with the Validate button. The release version is intended for usage for the 'students'. Major change with datadrivenmodel is the addition of selecting the lag for iterations between state transitions from the config 2020-11-04 01:29:34 -08:00
notebooks Update eda.ipynb 2021-04-26 01:17:19 -07:00
tests fixup: Format Python code with Black 2021-04-23 00:46:17 +00:00
.dockerignore update: ddm refactor1 2020-12-15 21:45:32 +00:00
.gitignore UPDATE: sweeping complete, and notebooks moved to NB directory 2021-04-22 17:08:11 -07:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2020-09-08 15:20:22 -07:00
Dockerfile UPDATE: ensure dockerfile doesnt setup env vars and has simulator.policy=bonsai 2021-07-07 16:17:31 -07:00
LICENSE.txt Adding disclaimer and updating environment.yml to not need sdk2 2020-09-16 17:03:22 -07:00
README.md WIP: gboost sweeping 2021-04-16 17:34:40 -07:00
SECURITY.md Initial SECURITY.md commit 2020-09-08 15:20:27 -07:00
all_models.py 🐛 fix: track history of data in ddm_predictor and define schema episode_step properly 2021-06-21 19:13:53 -07:00
assessment_metrics_loader.py UPDATE: notebook to use hydra config instead of yaml and rich.logging instead of manual prints 2021-03-25 20:38:25 -07:00
base.py UPDATE: make torch installation optional for simulator packages, upgrade mba to 0.1.3, and update dockerfile 2021-06-09 12:38:33 -07:00
ddm_predictor.py UPDATE: initial states in episode_start and in cartpole-updated-simparam.yaml 2021-07-13 17:07:21 -07:00
ddm_test_validate.py LINTER 2021-04-16 17:35:37 -07:00
ddm_trainer.py UPDATE: torch -> pytorch ddm_trainer + sweep params 2021-07-12 12:57:37 -07:00
environment.yml MERGE: conflicts in environment yaml 2021-06-24 20:14:27 +00:00
gboost_models.py UPDATE: sweeping complete, and notebooks moved to NB directory 2021-04-22 17:08:11 -07:00
loaders.py fixup: Format Python code with Black 2021-04-23 00:46:17 +00:00
model_loader.py 🐛 fix: track history of data in ddm_predictor and define schema episode_step properly 2021-06-21 19:13:53 -07:00
requirements-tests.txt UPDATE: pytests with full requirements 2021-07-13 09:50:00 -07:00
requirements.txt UPDATE: env_setup param for ddm_predictor 2021-06-09 14:51:56 -07:00
skmodels.py UPDATE: sweeping complete, and notebooks moved to NB directory 2021-04-22 17:08:11 -07:00
torch_models.py UPDATE: add path names to model.load_numpy 2021-04-28 09:07:58 -07:00

README.md

Training Data-Driven or Surrogate Simulators

This repository provides a template for training data-driven simulators that can then be leveraged for training brains (reinforcement learning agents) with Project Bonsai.

⚠️ Disclaimer: This is not an official Microsoft product. This application is considered an experimental addition to Microsoft's Project Bonsai toolbox. Its primary goal is to reduce barriers of entry to use Project Bonsai's core Machine Teaching. Pull requests for fixes and small enhancements are welcome, but we expect this to be replaced by out-of-the-box features of Project Bonsai shortly.

Dependencies

This repository leverages Anaconda for Python virtual environments and all dependencies. Please install Anaconda or miniconda first and then run the following:

conda env update -f environment.yml
conda activate ddm

This will create and activate a new conda virtual environment named ddm based on the configuration in the environment.yml file.

Tests

To get an understanding of the package, you may want to look at the tests in tests, and the configuration files in conf. You can run the tests by simply:

pytest tests
# or
python -m pytest tests/

Usage

The scripts in this package expect that you have a dataset of CSVs or numpy arrays. If you are using a CSV, you should ensure that:

  • The CSV has a header with unique column names describing your inputs to the model and the outputs of the model.
  • The CSV should have a column for the episode index and another column for the iteration index.
  • The CSV should have been cleaned from any rows containing NaNs

Generating Logs from an Existing Simulator

For an example on how to generate logged datasets from a simulator using the Python SDK, take a look at the examples in the samples repository, in particular, you can use the flag --test-local True --log-iteration True to generate a CSV data that matches the schema used in this repository.

Training Your Models

The scripts in this package leverage the configuration files saved in the conf folder to load CSV files, train and save models, and interface them to the Bonsai service. There are three configuration files:

  • conf/data/$YOUR_DATA_CONFIG.yaml defines the interface to the data to train on
  • conf/model/$YOUR_MODEL_CONFIG.yaml defines the Machine Learning model's hyper-parameters
  • conf/simulator/$YOUR_SIM_CONFIG.yaml defines the simulator interface

The library comes with a default configuration set in conf/config.yaml.

python ddm_trainer.py

You can change any configuration parameter by specifying the configuration file you would like to change and its new path, i.e.,

python ddm_trainer.py data=cartpole_st_at simulator=gboost_cartpole.yaml

which will use the configuration files in conf/data/cartpole_st_at.yaml and conf/simulator/gboost_cartpole.yaml.

You can also override the parameters of the configuration file by specifying their name:

python ddm_trainer.py data.path=csv_data/cartpole_at_st.csv data.iteration_order=1
python ddm_trainer.py data.path=csv_data/cartpole_at_st.csv model=xgboost 

The script automatically saves your model to the path specified by model.saver.filename. An outputs directory is also saved with your configuration file and logs.

Hyperparameter Tuning

You can also do some hyperparameter tuning by setting sweep.run to True in your conf.model.yaml file and specifying the parameters to sweep over and their distributions in the params argument:

sweep:
  run: True
  search_algorithm: random
  num_trials: 3
  scoring_func: r2
  params:
    estimator__max_depth: [1, 3, 5, 10]
    estimator__gamma: [0, 0.5, 1, 5]
    estimator__subsample: [0.1, 0.5, 1]

The sweeping function uses tune-sklearn. Valid choices for search_algorithm are: bayesian, random, bohb, and hyperopt.

Building Your Simulators

The schema for your simulator resides in conf/simulator. After defining your states, actions, and configs, you can run the simulator as follows:

python ddm_predictor simulator=$YOUR_SIM_CONFIG.yaml

If you would like to test your simulator before connecting to the platform, you can use a random policy:

python ddm_predictor.py simulator=$YOUR_SIM_CONFIG.yaml simulator.policy=random

Build Simulator Package

az acr build --image <IMAGE_NAME>:<IMAGE_VERSION> --file Dockerfile --registry <ACR_REGISTRY> .

Data Evaluation

Use the release version of the jupyter notebook to assist you with qualifying your data for creation of a simulator using supervised learning. The notebook is split up into three parts. The notebook uses the nbgrader package, where the user should click the Validate button to determine if all tests have been passed. You will be responsible for loading the data, running cells to see if you successfully pass tests, and manipulating the data in Python if the notebook finds things like NaNs and outliers. It will ask for desired operating limits of the model you wish to create and compare that against what is available in your provided datasets. Asssuming you pass the tests for data relevance, your data will be exported to a single csv named approved_data.csv which is ready to be ingested by the datadrivenmodel tool.

  • Data Relevance
  • Sparsity
  • Data Distribution Confidence
jupyter notebook release/presales_evaluation/presales_evaluation.ipynb

Once you have successfuly qualified your data using the Validate button, it is recommended to export it as a PDF to share the results without requiring access to the data.

Contribute Code

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Telemetry

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft's privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.