# Gymnasium FrozenLake - why one-hot encoding for state is required?

**URL:** <https://discuss.pytorch.org/t/gymnasium-frozenlake-why-one-hot-encoding-for-state-is-required/218342>\
**Category:** reinforcement-learning\
**Created:** [March 27, 2025, 6:31pm UTC](https://discuss.pytorch.org/t/gymnasium-frozenlake-why-one-hot-encoding-for-state-is-required/218342 "2025-03-27T18:31:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![impostor](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/impostor/32/74876_2.png) [@impostor](https://discuss.pytorch.org/u/impostor)\
**Post date:** [March 27, 2025, 6:31pm UTC](https://discuss.pytorch.org/t/gymnasium-frozenlake-why-one-hot-encoding-for-state-is-required/218342/1 "2025-03-27T18:31:05Z")

</div>

Hello, complete beginner here. I’m trying to implement Deep Q-Learning on the FrozenLake environment.

The state space is `Discrete(16)`, and the action space is `Discrete(4)`, so I initially designed a simple model:

```python
nn.Sequential(
    nn.Linear(1, 128),
    nn.Sigmoid(),
    nn.Linear(128, 4)
)

```

However, it failed. I spent nearly three days on this without success. Core of my DQL algorithm is 100% correct (I even tried copying code from working solutions).

After reviewing other implementations, I noticed that everyone used one-hot encoding for state. So instead of directly feeding 1-16 to model they feed its one-hot encoding representation. When I applied it, my model suddenly started working and successfully solved the environment.

But I don’t understand _why_ one-hot encoding is necessary. I found this comment:

[https://www.reddit.com/r/reinforcementlearning/comments/1bwmwt3/comment/ky9a2hb/?utm\_source=share&utm\_medium=web3x&utm\_name=web3xcss&utm\_term=1](https://www.reddit.com/r/reinforcementlearning/comments/1bwmwt3/comment/ky9a2hb/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1)

I kinda understand it but, I still don’t get why the model fails to “memorize”/“bruteforce” the correct action for each state without one-hot encoding. I also tried adding more hidden layers to help the network learn a better representation of the discrete states, but that didn’t work either.

Can someone explain why one-hot encoding is essential in this case?  
Is learning “gridworld” problem without one-hot encoding even possible?
