# How to correctly apply observation normalization?

**URL:** <https://discuss.ray.io/t/how-to-correctly-apply-observation-normalization/8083>\
**Category:** RLlib\
**Created:** [October 31, 2022, 2:04pm UTC](https://discuss.ray.io/t/how-to-correctly-apply-observation-normalization/8083 "2022-10-31T14:04:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imxuemei](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/imxuemei/32/3295_2.png) [@imxuemei](https://discuss.ray.io/u/imxuemei)\
**Post date:** [October 31, 2022, 2:04pm UTC](https://discuss.ray.io/t/how-to-correctly-apply-observation-normalization/8083/1 "2022-10-31T14:04:52Z")

</div>

I’m using MeanStdFilter in my PPO example. It works during the training process.  
But, i’m not sure whether to use filtered observations when calling trainer.compute\_single\_action.  
In my opinion, the model was trained based on filtered data, and thus the compute\_single\_action function should also take the filtered obs as the input.  
However, in my example, the action obtained without the filtered obs performs better than the action obtained with filtered obs.  
Then, what is the correct way to apply MeanStdFilter to a trained PPO?

The config is as follows.

```auto
config = {"framework": "torch",
                   "num_workers": 2,
                   "num_gpus": 0,
                   "batch_mode": "complete_episodes",
                   "observation_filter": "MeanStdFilter",
                   # "log_level": "DEBUG",
                   # "callbacks": Callbacks4Normalization,
                   "gamma": 0.99,
                   "env_config": train_env_config, }

```

The code fragement is as follows.

```auto
filter = agent.workers.local_worker().filters.get("default_policy")
print("filter: ", filter.running_stats.n, filter.buffer.n, filter.running_stats.mean)
# filtered_obs = filter(obs, update=False)
# action = agent.compute_single_action(filtered_obs )
action = agent.compute_single_action(obs)

```

---

<div class="post-metadata">

**Author:** ![arturn](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/arturn/32/2096_2.png) [@arturn](https://discuss.ray.io/u/arturn)\
**Post date:** [November 15, 2022, 8:56am UTC](https://discuss.ray.io/t/how-to-correctly-apply-observation-normalization/8083/2 "2022-11-15T08:56:16Z")

</div>

Hi @imxuemei ,

Yes, indeed the model should do better on filtered inputs.  
If you use the Algorithm’s (formerly Agent) compute\_single\_action, this will apply the filter already. So you should not filter twice, by filtering manually.  
This is different if you use the Policy’s compute action methods, which don’t do filtering.

This logic will likely soon be replaced with more intuitive modes of using your trained policy for inference. Make sure to check out upcoming Ray versions 🙂

Cheers

---

<div class="post-metadata">

**Author:** ![imxuemei](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/imxuemei/32/3295_2.png) [@imxuemei](https://discuss.ray.io/u/imxuemei)\
**Post date:** [November 19, 2022, 1:01pm UTC](https://discuss.ray.io/t/how-to-correctly-apply-observation-normalization/8083/3 "2022-11-19T13:01:43Z")

</div>

I got it. Thanks very much!
