Code accompanying "Learning What To Do by Simulating the Past", ICLR 2021.

Overview

Learning What To Do by Simulating the Past

This repository contains code that implements the Deep Reward Learning by Simulating the Past (Deep RSLP) algorithm introduced in the paper "Learning What To Do by Simulating the Past". This code is provided as is, and will not be maintained. Here we describe how to reproduce the experimental results reported in the paper. You can find video of policies trained with Deep RLSP here.

Citation

David Lindner, Rohin Shah, Pieter Abbeel, Anca Dragan. Learning What To Do by Simulating the Past. In International Conference on Learning Representations (ICLR), 2021.

@inproceedings{lindner2021learning,
    title={Learning What To Do by Simulating the Past},
    author={Lindner, David and Shah, Rohin and Abbeel, Pieter and Dragan, Anca},
    booktitle={International Conference on Learning Representations (ICLR)},
    year={2021},
}

Table of Contents

Set up the environment

There are two options to set up the environment to run the code: either using Docker, or setting up the environment manually using Anaconda. We recommend to use the Docker setup.

Docker

You can use Docker to set up the dependencies including MuJoCo automatically. To do this install Docker, then copy a valid MuJoCo key to docker/mjkey.txt, and execute the following commands:

docker build --tag deep-rlsp:1.0 docker
docker run -v `pwd`:/deep-rlsp/ -w /deep-rlsp/ -i -t deep-rlsp:1.0 bash
conda activate deep-rlsp

The first command sets up a container with all required dependencies including MuJoCo. The second command starts an interactive shell inside the container and the third command activates the Anaconda environment set up inside the container. You can now run all experiments inside this container. Note, that you might have to modify docker/Dockerfile to use Tensorflow with GPU support.

Manual setup

Alternatively, you can set up the same Anaconda environment manually. In this case MuJoCo has to be installed locally. If using a non-standard location, the environment variables MUJOCO_PY_MJKEY_PATH and MUJOCO_PY_MUJOCO_PATH have to be set accordingly.

To perform the manual setup, install Anaconda locally and run the following commands to set up the environment:

conda env create -f docker/environment.yml
conda activate deep-rlsp
pip install mujoco-py==2.0.2.9
pip install -e .
conda activate deep-rlsp

This sets up an Anaconda environment with the required dependencies and activates it, which can then be used to run the code.

Reproducing the experiments

Now we describe how to reproduce the experiments described in the paper. We first describe the experiments in Gridworld environments, discussed in Section 3.2, and then the experiments in MuJoCo environments, discussed in Sections 3.3 and 3.4. For each of these we describe how to run Deep RLSP, the ablations discussed in the paper, and GAIL as a baseline.

Gridworld experiments

To run the Gridworld experiments reported in Section 3.2, you first have to train an inverse dynamics model for each environment:

python scripts/train_inverse_dynamics.py --gridworlds RoomDefault-v0
python scripts/train_inverse_dynamics.py --gridworlds ApplesDefault-v0
python scripts/train_inverse_dynamics.py --gridworlds TrainDefault-v0
python scripts/train_inverse_dynamics.py --gridworlds BatteriesDefault-v0
python scripts/train_inverse_dynamics.py --gridworlds BatteriesEasy-v0
python scripts/train_inverse_dynamics.py --gridworlds RoomBad-v0

The models will be saved in tf_ckpt, and will have names such as tf_ckpt_mlp_RoomDefault-v0_20210313_160730. You might have to create the folder tf_ckpt before running the models.

You can then run the experiments with the following commands:

python src/deep_rlsp/run.py with latent_rlsp_config room_default inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_vae_RoomDefault-v0_20200930_132218
python src/deep_rlsp/run.py with latent_rlsp_config train_default inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_vae_TrainDefault-v0_20200930_132234
python src/deep_rlsp/run.py with latent_rlsp_config apples_default inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_vae_ApplesDefault-v0_20200930_132414
python src/deep_rlsp/run.py with latent_rlsp_config batteries_easy inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_mlp_BatteriesDefault-v0_20200930_123401
python src/deep_rlsp/run.py with latent_rlsp_config batteries_default inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_mlp_BatteriesDefault-v0_20200930_123401
python src/deep_rlsp/run.py with latent_rlsp_config room_bad inverse_dynamics_model_checkpoint=tf_ckpt/tf_ckpt_vae_RoomDefault-v0_20200930_132218

adapting the paths of the inverse dynamics model.

You can run the "AverageFeatures" ablation by replacing latent_rlsp_config with latent_rlsp_ablation in the commands above.

MuJoCo experiments

To reproduce our experiments in the MuJoCo simulator, that we report in Sections 3.3 and 3.4, you need to perform the following steps:

  1. Obtain an original policy
  2. Run Deep RLSP
  3. Evaluate the results
  4. Compare to baselines / ablations

We now describe each step in turn.

Obtaining an original policy

We consider two different ways of obtaining policies to immitate:

  1. Obtain policy by optimizing a given reward function
  2. Obtain policy by running Dynamics-Aware Unsupervised Discovery of Skills (DADS)

Obtain policy by optimizing a given reward function

To train a policy on the reward function of a given MuJoCo environment, use the scripts/train_sac.py script. With the following commands you can train policies on the environments we discuss in the paper and save them in the policies/ folder:

python scripts/train_sac.py InvertedPendulum-v2 policies/sac_pendulum_6e4 --timesteps 60000
python scripts/train_sac.py HalfCheetah-FW-v2 policies/sac_cheetah_fw_2e6 --timesteps 2000000
python scripts/train_sac.py HalfCheetah-BW-v2 policies/sac_cheetah_bw_2e6 --timesteps 2000000
python scripts/train_sac.py Hopper-v2 policies/sac_hopper_2e6 --timesteps 2000000

This uses the soft actor-critic algorithm to train a policy using the hyperparameters from rl-baselines-zoo. The hyperparameters are defined in src/deep_rlsp/solvers/__init__.py.

For convenience, we provide trained policies in the policies/ folder of this repository.

Obtain policy by running DADS

We run DADS using the code provided by the authors. To reproduce the our experiments, we provide rollouts sampled from the jumping and balancing skills in the folder skills/.

Run Deep RLSP

We are now ready to run the full Deep RLSP algorithm. The main file to run experiments is located at src/deep_rlsp/run_mujoco.py. The following commands reproduce the experiments discussed in the paper:

Pendulum

python src/deep_rlsp/run_mujoco.py with base pendulum n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base pendulum n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base pendulum n_sample_states=50

Cheetah running forward

python src/deep_rlsp/run_mujoco.py with base cheetah_fw n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base cheetah_fw n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base cheetah_fw n_sample_states=50

Cheetah running backward

python src/deep_rlsp/run_mujoco.py with base cheetah_bw n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base cheetah_bw n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base cheetah_bw n_sample_states=50

Hopper

python src/deep_rlsp/run_mujoco.py with base hopper n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base hopper n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base hopper n_sample_states=50

Cheetah balancing skill

python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/balancing_rollouts.pkl" n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/balancing_rollouts.pkl" n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/balancing_rollouts.pkl" n_sample_states=50

Cheetah jumping skill

python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/jumping_rollouts.pkl" n_sample_states=1
python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/jumping_rollouts.pkl" n_sample_states=10
python src/deep_rlsp/run_mujoco.py with base cheetah_skill current_state_file="skills/jumping_rollouts.pkl" n_sample_states=50

The results will be saved in the results/ folder. The trained (VAE and dynamics) models will be saved in tf_ckpt.

Evaluate the results

In the paper, we evaluate Deep RLSP in two ways:

  1. Train a new policy on the inferred reward function from Deep RLSP and evaluate this policy (as in Table 1)
  2. Evaluate the policy trained during Deep RLSP (for the balancing and jumping skills)

Train a new policy on the inferred reward function

To produce the results provided in Table 1 in the paper, we run SAC on the final reward function inferred by the Deep RLSP algorithm. To do this run the following command

python scripts/mujoco_evaluate_inferred_reward.py with experiment_folder=results/mujoco/20200528_150813_InvertedPendulum-v2_optimal

providing the subfolder of results/ that corresponds to the experiment you want to evaluate. This creates a sub-folder in results/mujoco/eval that contains the trained policy.

Then, to evaluate this policy, run

python scripts/evaluate_policy.py results/mujoco/eval/20200605_203113_20200603_220928_InvertedPendulum-v2_optimal_1/policy.zip sac InvertedPendulum-v2 --num_rollouts 100

for the corresponding policy file. This samples 100 trajectories from the policy and determines the mean and standard deviation of the policy return.

The same script can also be used to visualize the policies using the --render or --out_video arguments.

Evaluate the policy trained during Deep RLSP

The policies trained during Deep RLSP are saved in the results folder of a specific run as rlsp_policy_1.zip, rlsp_policy_2.zip, ...

To evaluate these policies, run

python scripts/evaluate_policy.py results/mujoco/20200528_150813_InvertedPendulum-v2_optimal/rlsp_policy_112.zip sac InvertedPendulum-v2 --num_rollouts 100

for the corresponding policy file. This samples 100 trajectories from the policy and determines the mean and standard deviation of the policy return.

The same script can also be used to visualize the policies using the --render or --out_video arguments.

AverageFeatures and Waypoints ablations

To ensure comparability with a limited number of random seeds, we run the ablations with the same trained VAE and dynamics models and the same input states as Deep RLSP. This can be done the following commands:

python src/deep_rlsp/ablation_AverageFeatures.py with result_folder=results/mujoco/20200528_150813_InvertedPendulum-v2_optimal
python src/deep_rlsp/ablation_Waypoints.py with result_folder=results/mujoco/20200528_150813_InvertedPendulum-v2_optimal

passing a folder containing the corresponding results of Deep RLSP as an argument. The policy returned by this baseline algorithm can be found in results/mujoco/, and they can also be visualized and evaluated using the scripts/evaluate_policy.py script.

Compare to GAIL

Running Generative Adversarial Imitation Learning (GAIL) requires the imitation library. You can install it using:

pip install imitation==0.1.1

To run GAIL, we provide demonstrations from the expert policies in the correct format in the demonstrations folder. You can create demonstration data from already trained expert policies by running:

python scripts/create_demonstrations.py policies/sac_cheetah_fw_2e6.zip demonstrations/sac_cheetah_fw_traj_len_{}_seed_{}.pkl 10 generate_seed HalfCheetah-FW-v2 1

Then you can run GAIL on the demonstration data by running:

python scripts/run_gail.py with gail half_cheetah env_name='HalfCheetah-FW-v2' rollout_path=demonstrations/sac_cheetah_fw_traj_len_1_seed_22750069.pkl log_dir=./gail_logs/gail_cheetah_fw_len_1_demoseed_22750069/

To visualize the resulting policies:

python scripts/evaluate_policy.py gail_logs/gail_cheetah_fw_len_1_demoseed_22750069/checkpoints/final/gen_policy gail HalfCheetah-FW-v2 --render --out_video=videos/gail_balancing_len_1.mp4

Code quality

We use black for code formatting, flake8 for linting, and mypy to check type hints. You can run all checks with bash code_checks.sh and unit tests with python setup.py test.

Owner
Center for Human-Compatible AI
CHAI seeks to develop the conceptual and technical wherewithal to reorient the general thrust of AI research towards provably beneficial systems.
Center for Human-Compatible AI
Models, datasets and tools for Facial keypoints detection

Template for Data Science Project This repo aims to give a robust starting point to any Data Science related project. It contains readymade tools setu

girafe.ai 1 Feb 11, 2022
Using BERT+Bi-LSTM+CRF

Chinese Medical Entity Recognition Based on BERT+Bi-LSTM+CRF Step 1 I share the dataset on my google drive, please download the whole 'CCKS_2019_Task1

Xiang WU 55 Dec 21, 2022
This code is for eCaReNet: explainable Cancer Relapse Prediction Network.

eCaReNet This code is for eCaReNet: explainable Cancer Relapse Prediction Network. (Towards Explainable End-to-End Prostate Cancer Relapse Prediction

Institute of Medical Systems Biology 2 Jul 28, 2022
Rethinking Space-Time Networks with Improved Memory Coverage for Efficient Video Object Segmentation

STCN Rethinking Space-Time Networks with Improved Memory Coverage for Efficient Video Object Segmentation Ho Kei Cheng, Yu-Wing Tai, Chi-Keung Tang [a

Rex Cheng 456 Dec 12, 2022
Python Library for learning (Structure and Parameter) and inference (Statistical and Causal) in Bayesian Networks.

pgmpy pgmpy is a python library for working with Probabilistic Graphical Models. Documentation and list of algorithms supported is at our official sit

pgmpy 2.2k Jan 03, 2023
SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data

SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data SurvITE: Learning Heterogeneous Treatment Effects from Time-to-Event Data Au

14 Nov 28, 2022
KoRean based ELECTRA pre-trained models (KR-ELECTRA) for Tensorflow and PyTorch

KoRean based ELECTRA (KR-ELECTRA) This is a release of a Korean-specific ELECTRA model with comparable or better performances developed by the Computa

12 Jun 03, 2022
Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021)

Understanding and Improving Encoder Layer Fusion in Sequence-to-Sequence Learning (ICLR 2021) Citation Please cite as: @inproceedings{liu2020understan

Sunbow Liu 22 Nov 25, 2022
[CVPR 2022] Thin-Plate Spline Motion Model for Image Animation.

[CVPR2022] Thin-Plate Spline Motion Model for Image Animation Source code of the CVPR'2022 paper "Thin-Plate Spline Motion Model for Image Animation"

yoyo-nb 1.4k Dec 30, 2022
PyTorch code for ICPR 2020 paper Future Urban Scene Generation Through Vehicle Synthesis

Future urban scene generation through vehicle synthesis This repository contains Pytorch code for the ICPR2020 paper "Future Urban Scene Generation Th

Alessandro Simoni 4 Oct 11, 2021
The PASS dataset: pretrained models and how to get the data - PASS: Pictures without humAns for Self-Supervised Pretraining

The PASS dataset: pretrained models and how to get the data - PASS: Pictures without humAns for Self-Supervised Pretraining

Yuki M. Asano 249 Dec 22, 2022
ShinRL: A Library for Evaluating RL Algorithms from Theoretical and Practical Perspectives

Status: Under development (expect bug fixes and huge updates) ShinRL: A Library for Evaluating RL Algorithms from Theoretical and Practical Perspectiv

37 Dec 28, 2022
Non-Metric Space Library (NMSLIB): An efficient similarity search library and a toolkit for evaluation of k-NN methods for generic non-metric spaces.

Non-Metric Space Library (NMSLIB) Important Notes NMSLIB is generic but fast, see the results of ANN benchmarks. A standalone implementation of our fa

2.9k Jan 04, 2023
A modified version of DeepMind's Alphafold2 to divide CPU part (MSA and template searching) and GPU part (prediction model)

ParallelFold Author: Bozitao Zhong This is a modified version of DeepMind's Alphafold2 to divide CPU part (MSA and template searching) and GPU part (p

Bozitao Zhong 77 Dec 22, 2022
Unified learning approach for egocentric hand gesture recognition and fingertip detection

Unified Gesture Recognition and Fingertip Detection A unified convolutional neural network (CNN) algorithm for both hand gesture recognition and finge

Mohammad 227 Dec 25, 2022
CaLiGraph Ontology as a Challenge for Semantic Reasoners ([email protected]'21)

CaLiGraph for Semantic Reasoning Evaluation Challenge This repository contains code and data to use CaLiGraph as a benchmark dataset in the Semantic R

Nico Heist 0 Jun 08, 2022
Application of the L2HMC algorithm to simulations in lattice QCD.

l2hmc-qcd 📊 Slides Recent talk on Training Topological Samplers for Lattice Gauge Theory from the Machine Learning for High Energy Physics, on and of

Sam Foreman 37 Dec 14, 2022
EMNLP'2021: SimCSE: Simple Contrastive Learning of Sentence Embeddings

SimCSE: Simple Contrastive Learning of Sentence Embeddings This repository contains the code and pre-trained models for our paper SimCSE: Simple Contr

Princeton Natural Language Processing 2.5k Dec 29, 2022
ARAE-Tensorflow for Discrete Sequences (Adversarially Regularized Autoencoder)

ARAE Tensorflow Code Code for the paper Adversarially Regularized Autoencoders for Generating Discrete Structures by Zhao, Kim, Zhang, Rush and LeCun

19 Nov 12, 2021
Self-Supervised Pre-Training for Transformer-Based Person Re-Identification

Self-Supervised Pre-Training for Transformer-Based Person Re-Identification [pdf] The official repository for Self-Supervised Pre-Training for Transfo

Hao Luo 116 Jan 04, 2023