Comments (4)
I changed code, seems it works now , please correct me if I misunderstanding, thank you~
# pip3 install torchrl
from torchrl.modules import MaskedCategorical
class MyDiscreteSACPolicy(DiscreteSACPolicy):
def forward( # type: ignore
self,
batch: Batch,
state: Optional[Union[dict, Batch, np.ndarray]] = None,
input: str = "obs",
**kwargs: Any,
) -> Batch:
obs = batch[input]
if hasattr(batch.info, 'mask'):
action_mask = getattr(batch.info, 'mask')
logits, hidden = self.actor(obs, state=state, info=batch.info)
mask = torch.tensor(action_mask)
dist = MaskedCategorical(logits=logits, mask=mask)
else:
# mask = None
logits, hidden = self.actor(obs, state=state, info=batch.info)
dist = Categorical(logits=logits)
if self._deterministic_eval and not self.training:
act = dist.logits.argmax(axis=-1)
# act = logits.argmax(axis=-1)
# print(logits, mask, act)
else:
act = dist.sample()
return Batch(act=act, state=hidden, dist=dist)
class MyEnv:
def reset(self, seed=None, options=None):
...
info = {'mask': action_mask}
obs = state_arr
return obs, info
def step(self, action):
...
info = {'mask': action_mask}
obs = state_arr
return obs, reward, terminated, truncated, info
from tianshou.
Is your question resolved by the corrected code?
from tianshou.
Is your question resolved by the corrected code?
it can work for "action mask", but I am not sure if it will impact on RL, I am not a RL expert.
from tianshou.
I'll take it as yes, in general a tutorial on action masking would make sense. I'll open a separate issue on that
from tianshou.
Related Issues (20)
- Clean up handling of output_dim
- How to randomly collect actions from the environment in train_step HOT 6
- Wrong output of forward for custom policy HOT 1
- Support MultiBinary action space for SAC or A2C HOT 2
- Clearer separation between the trainer and the algorithm and refactoring of policy classes HOT 1
- When is the reset() function being called in tictactoe? HOT 2
- Rename state_shape to obs_shape HOT 1
- logger and save_best_fn do not work for Custom Environment HOT 1
- Regarding the error related to SEED when I train in a homebrew environment HOT 6
- How to train a offline BCQ model with a custom logged data? HOT 1
- Collector sampling with multiple environment does not seem to be unbiased with n_episodes HOT 10
- Colab button not working in docs tutorials HOT 2
- My custom PettingZoo env is working with DQNPolicy but not with PPOPolicy : AttributeError: 'str' object has no attribute 'ndim' HOT 2
- Update Gymnasium to v1.0.0a1 HOT 3
- Allow number of episodes per test step to be configured in high-level API
- Improve and extend Documentation Content
- Code duplication between ReplayBuffer and ReplayBufferManager
- Remove data from state in Collector, and remove preprocess_fn there HOT 7
- how to convert Batch into ndarray/tensor HOT 5
- Support Dict observation spaces HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tianshou.