（論文）Multi-task Deep Reinforcement Learning with PopArt（2018） 

# Source
- [Multi-task Deep Reinforcement Learning with PopArt](https://arxiv.org/abs/1809.04474)
- [Learning values across many orders of magnitude](https://arxiv.org/abs/1602.07714)
- [IMPALA: Scalable Distributed Deep-RL with Importance Weighted Actor-Learner Architectures](https://arxiv.org/abs/1802.01561)
- [Preserving Outputs Precisely while Adaptively Rescaling Targets](https://deepmind.com/blog/preserving-outputs-precisely-while-adaptively-rescaling-targets/)
- [Reinforcement Learning with Unsupervised Auxiliary Tasks](https://arxiv.org/abs/1611.05397)
- [What’s New in Deep Learning Research: How DeepMind Builds Multi-Task Reinforcement Learning Algorithms That Don’t Get Distracted](https://towardsdatascience.com/whats-new-in-deep-learning-research-how-deep-builds-multi-task-reinforcement-learning-algorithms-f0d868684c45)

# Introduction
There have been numerous successful cases where a reinforcement agent being able to perform at super human level at various games.  However, while the agent retains the same network structure throughout, it needs to be expertly trained on the specific game they are currently trying to conquer.  Despite the progress DeepMind has made with [IMPALA](https://arxiv.org/abs/1802.01561), where the aim is to create an agent that can multi-task, the final performance can only achieve a 59.7% median human normalized score across 57 Atari games, and a 49.4% mean human normalized score across 30 DeepMind Lab levels (DmLab-30).

One particular reason is that different games has a different rewarding system, causing the agent to tunnel-vision on tasks that yield higher rewards.  For example, if game A can only provided a maximum reward of +1 at each step, whereas game B offers +1000, it is without question that the model will be more inclined to solve for game B due to saliency.

To alleviate this issue, a method named [PopArt](https://arxiv.org/abs/1602.07714) was proposed, where the rewards in each game are scaled so that each game becomes of equal learning value.  With the collaboration between PopArt and IMPALA, a single agent is now capable of playing 57 Atari games with above-human median performance (110% median human normalized) and 72.8% mean human normalized score on DmLab-30.

# Implementation
The goal is to have a single agent to learn *N* different environments ![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/f60ff788-7ed7-b23e-bdd3-68c70e347266.png).  This can be formalised as a single large MDP whose state space is ![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/e57f797a-efec-1ae6-70f1-73e26adb0cb5.png) where *i* is the task index.  The task index is only used at training time, not during testing.  

The agent is an actor-critic that utilizes deep neural network for both its policy and value network.  The policy is updated using REINFORCE algorithm 
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/c2685875-cc5d-491c-aaca-636e67400991.png).  
where v<sub>θ</sub>(S<sub>t</sub>) is used as a baseline to reduce variance.  It also uses multi-step return to further reduce the variance.
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/f7e7b4e2-6f41-2860-ea6b-49a052d3a018.png)
G<sup>v</sup><sub>t</sub> and G<sup>π</sup><sub>t</sub> are the estimated return of v<sub>π</sub>(S<sub>t</sub>) and Q<sub>π</sub>(S<sub>t</sub>A<sub>t</sub>) respectively.

The agent follows the IMPALA distributed structure (several actors distributed across different environments, with a single GPU updating all rollouts with a deep convolutional ResNet followed by a LSTM recurrent layer), and implements importance sampling to correct each error term in n-step return.  This causes the network to become unbiased but has high variance as a downside.  To reduce variance, most of the importance sampling ratio is clipped.  This lead to the v-trace return:
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/a7e14ee3-ef1e-56cf-a308-7368e0c7ed63.png)
Following this, the returns used by the value and policy updates are:
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/f2b74ccb-d7e0-9e39-5431-e58289080121.png)

The network uses PopArt normalisation to derive a scale invariant algorithm for the actor-critic agent.  In order to normalize both baseline and policy gradient updates, the value function v<sub>µ,σ,θ</sub>(S) is parameterised as the linear transformation of a suitably normalised value prediction n<sub>θ</sub>(S). The output from the deep neural net is the normalised value prediction:
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/172508f1-2166-db66-3f03-3a147967b565.png)
µ and σ are updated with (6) in order to track mean and standard deviation of the values.
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/118b9ce7-6968-b72f-06ab-b7a42b787395.png)
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/873cf604-c643-30bc-a08d-8e3df0f6c3cc.png)
The fixed decay rate β determines the horizon used to compute the statistics. n<sub>θ</sub>(S), µ and σ are used to normalise the loss in both value and policy network of the actor critic.  This results in the scale-invariant updates:
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/74b60894-1ece-ef1d-eb10-c074cae7bc92.png)
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/eebb35e8-e5d8-532f-172a-53324d5b53d2.png)
The normalised targets for values are non-stationary as they depend on statistics µ and σ.  PopArt normalisation prevents this by updating the last layer of the normalised value network to preserve unnormalised value estimates v<sub>µ,σ,θ</sub>, under any change in the statistics µ -> µ' and σ -> σ':
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/b23a8149-26f7-5a94-82dc-b6bc0ce9278c.png)

A single pair of normalisation statistics is not enough for multitask RL settings.  Let D<sub>i</sub> be an environment in some finite number i, and π(S|A) be a task-agnostic (it doesn't know which task it will get) policy that takes a state S from any of the environment D<sub>i</sub>, and maps it to a probability distribution onto the shared action space A.  The multi-task value function v(S) has N outputs, one for each task.  v can be parameterised the same as before using Equation 5, except the statistics µ and σ are now vectors, and the same holds true for the value function, which becomes ![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/c75dba75-aec0-aa77-54bc-25c8ee349031.png)
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/8174c586-1f92-6704-7277-26c72bca2c10.png)
where W and b are the weights and bias for the last fully connected layer in n<sub>θ</sub>(s).  The scale invariant updates from Equation 7 and 8 now becomes:
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/c1863bca-7a30-cab8-52d1-90033528c08e.png)
where the targets ![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/39ad63f6-d7c0-4080-fc2e-4584b0f627c1.png) uses the value estimates for environment D<sub>i</sub> for bootstrapping.

W and b are updated in the same way when updating µ and σ
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/92ad33d8-ed25-c2d1-434b-babfcf6159f3.png)
where w<sub>i</sub> is the i<sup>th</sup> row of matrix W, and µ<sub>i</sub>, σ<sub>i</sub>, b<sub>i</sub> are the  i<sup>th</sup> elements of the corresponding parameter vectors.  When updating, only the values, not the policy, are conditioned on the task index, so that the agent can perform without prior knowledge of the selected task.  This is because values are only used to reduce the variance of the policy updates during training, not used for action selection.

To summarize, the gradients do not propagate into mean and std, as they are exclusively updated as in Equation 6.  The weights W of the last layer of the value function are updated according to Equation 11 and 13.  Note that the actor critic updates are applied first (11) before the update of the statistics (6), and finally followed by (13).

# Result
The experiments are performed on Atari-57 and DmLab-30.  In addition, an extra environment of Atari-57 without reward clipping is also introduced.  Atari has a particularly diverse reward structure, while DmLab holds diverse tasks.  The IMPALA baseline shown in this paper is also stronger than those reported in the original paper, as the agent has been equipped with a larger action set (e.g. giving it finer control when rotating).
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/9faa36c6-1224-561a-0380-c1e4f1e1a1d0.png)

![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/d876f30a-e376-5619-0553-2f8d81c5a0bf.png)
Note that the performance is the median performance of a single agent across all games, rather than the median of a set of individually trained agents.

The PopArt-IMPALA was further extended to incooperate pixel control in order to improve data efficiency.
![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/e4c45ad2-1967-f454-cc02-2f1711384d57.png)
It can be seen in Figure 5 that pixel control augmented PopArt-IMPALA with only a fraction of the data.  On top of this, since both PopArt and pixel control requires very small additional computational cost, the cost of training IMPALA agents is greatly reduced.  Furthermore, any other orthogonal advances in deep RL can be combined to further improve performance, similar to those done by Rainbow.

![image.png](https://qiita-image-store.s3.amazonaws.com/0/264921/d620ed35-a18f-76f2-2cac-0e1dd468541b.png)

# Conclusion
The PopArt's adaptive normalisation has proven that it can allow a single agent to train across multiple tasks.  On top of the performance gain,  the data efficiency has also been improved.  Further augmentations such as pixel control has shown to further boost data efficiency.  Result might even be improved when incooperating other methods.