How to use normalizations in inference?
See original GitHub issueImportant Note: We do not do technical support, nor consulting and don’t answer personal questions per email. Please post your question on the RL Discord, Reddit or Stack Overflow in that case.
📚 Documentation
A clear and concise description of what should be improved in the documentation.
Checklist
- I have read the documentation (required)
- I have checked that there is no similar issue in the repo (required)
We have used your VecNormalize wrapper and like it a lot, but we are wondering how to use the final normalization in setups that only do inference.
We are exporting them as noted in the documentation:
stats_path = os.path.join("archive/" + log_name_, "vec_normalize.pkl")
env.save(stats_path)
And load them accordingly:
env = DummyVecEnv([OurVeryFancyEnv])
env = VecNormalize.load(stats_path, env)
env.training = False
env.norm_reward = False
model_test = PPO.load("archive/" + log_name_ + "/best_model.zip",env)
Is it truly neccessary to wrap our env in a vectorized environment to load/apply the normalizations for inference? Is the output documented somewhere, can we apply them “manually” on the observations?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Speed-up inference with Batch Normalization Folding
Normalize the batch by first subtracting its mean μ, then dividing it by its standard deviation σ. Further scale by a factor γ...
Read more >How to normalize a dataset considering unsee samples at ...
My question is in regard to the best practices for normalizing such kind of dataset for training and inferencing.
Read more >Batch and Layer Normalization - Pinecone
As layer normalization is done along the length of input to a specific layer, the same set of operations can be used at...
Read more >Best practice for inference (production) - normalize and resize?
I'm curious to know what the best practices are related to production usage of models? Specifically, I had assumed that for production time ......
Read more >Understanding Batch Normalization | by Krishna D N - Medium
Normalization of any data is about finding the mean and variance of the data and normalizing the data so that the the data...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve had the same issue as @Trolldemorted and implemented a somewhat hacky utility wrapper to solve it.
I implemented this wrapper, because
VecEnv
automatically resets environments at the end of an episode. While this behaviour is very nice in training, in production it became a bit of a nuisance. (I am using RL for optimisation and want retain the state achieved by the end of an episode.) Applying the normalisation to a normalgym.Env
solves this problem nicely.I’m not sure if my situation is niche or if there is a better way to disable the automatic reset in
VecEnvs
, but maybe it would make sense to include a clean version of this utility wrapper in Stable Baselines.Note: I’m not sure how all of this will be affected by the changes to Gym beyond v0.21 (especially
VectorEnv
).Sounds like you need to deactivate termination completely at test time (except the first one), or at least deactivate the reset of the actuator after the first episode (should be easy to do) (or deactivate the timeout if that the termination condition you don’t need at test time).