Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable preprocessing for a policy? #8600

Closed
houcharlie opened this issue May 26, 2020 · 5 comments · Fixed by #29011
Closed

How to disable preprocessing for a policy? #8600

houcharlie opened this issue May 26, 2020 · 5 comments · Fixed by #29011
Assignees
Labels
bug Something that is supposed to be working; but isn't enhancement Request for new feature and/or capability P3 Issue moderate in impact or severity

Comments

@houcharlie
Copy link

houcharlie commented May 26, 2020

What is your question?

I have a few learned policies as well as one hard-coded policy. I instantiated the hard-coded policy using the Policy interface. I'd like to know how to disable the observation preprocessing for this policy only; couldn't find anything for this in the docs.

For instance, in the rock-paper-scissors example, the observations are automatically converted to one-hot-encodings. https://github.com/ray-project/ray/blob/master/rllib/examples/policy/rock_paper_scissors_dummies.py

But I'd like to be able to operate directly on the states 0,1,2 for rock, paper scissors.

@houcharlie houcharlie added the question Just a question :) label May 26, 2020
@houcharlie
Copy link
Author

Answer: you can make the state space for the hard-coded policy be a gym.spaces.Box regardless of whether the state space is actually continuous or not; rllib does not perform any preprocessing for Box spaces. This is a pretty hacky solution though, would be nice to see a cleaner one.

@sven1977
Copy link
Contributor

sven1977 commented Aug 4, 2020

Got you. Yes, setting preprocessing to off shouldn't even do one-hot'ing anymore. I'll take a look.

@sven1977 sven1977 self-assigned this Aug 4, 2020
@sven1977 sven1977 added P3 Issue moderate in impact or severity rllib enhancement Request for new feature and/or capability and removed question Just a question :) labels Aug 4, 2020
@ericl ericl added this to the RLlib Bugs milestone Mar 11, 2021
@ericl ericl removed the rllib label Mar 11, 2021
@andras-kth
Copy link

andras-kth commented Sep 10, 2021

Has this been addressed in a non-hacky way?
Why is automatic preprocessing turned on by default, anyway?

I see a preprocessor_pref in TrainerConfDict, but based on the rather sparse documentation,
only 2 values are (or, may be?) supported: "deepmind" or "rllib". Would setting this to None
turn off preprocessing? I guess I better test...

Setting this to None doesn't appear to accomplish anything...

Maybe "NoPreprocessor" might help...

if type(pp).__name__ != "NoPreprocessor":

NOPE...

Also tried custom_preprocessor, but NoPreprocessor doesn't appear to be registered;
plus, there's a deprecation warning:

2021-09-10 09:25:46,996	WARNING catalog.py:695 -- DeprecationWarning: Custom preprocessors are deprecated, since they sometimes conflict with the built-in preprocessors for handling complex observation spaces. Please use wrapper classes around your environment instead of preprocessors.

Hmm... it looks like this is being worked on, but not quite ready for prime time:

@andras-kth
Copy link

andras-kth commented Sep 14, 2021

In the end, the custom_preprocessor approach worked. Eventually, deprecation may force the use of a wrapper.

Just in case anyone finds it useful, here's a snippet that transforms boolean vector observations from
the original gym.spaces.Tuple((gym.spaces.Discrete(2),)*dimension) to gym.spaces.Box(0, 1, (dimension,), dtype=int):

import gym
import numpy as np
from ray import rllib

class BooleanVectorPreprocessor(rllib.models.preprocessors.Preprocessor):
    def _init_shape(self, observation_space, options=None):
        return (len(observation_space.spaces),)

    def transform(self, observation):
        return np.array(observation)
    
    @property
    def observation_space(self):
        space = gym.spaces.Box(0, 1, self.shape, dtype='int8')
        space.original_space = self._obs_space
        return space

rllib.models.ModelCatalog.register_custom_preprocessor('boolean_vector', BooleanVectorPreprocessor)

@ArturNiederfahrenhorst ArturNiederfahrenhorst added the bug Something that is supposed to be working; but isn't label Oct 3, 2022
@ArturNiederfahrenhorst
Copy link
Contributor

Thanks for adding to this @andras-kth !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that is supposed to be working; but isn't enhancement Request for new feature and/or capability P3 Issue moderate in impact or severity
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants