[英]OpenAI Gym - How to create one-hot observation space?
Aside from openAI's doc , I hadn't been able to find a more detailed documentation. 除了openAI的文档外 ,我无法找到更详细的文档。
I need to know the correct way to create: 我需要知道正确的创建方法:
An action space which has 1..n
possible actions. 具有
1..n
可能动作的动作空间。 (currently using Discrete action space) (当前使用离散操作空间)
An observation space that has 2^n
states - A state for every possible combination of actions that has been taken. 具有
2^n
个状态的观察空间-每个已采取动作的可能组合的状态。 I would like a one-hot representation of the action vector - 1 for action was already taken
, 0 for action still hadn't been taken
我想要动作向量的一个热表示-
action was already taken
1项action was already taken
, action still hadn't been taken
action was already taken
0项action still hadn't been taken
How do I do that with openAI's Gym? 我如何使用openAI的Gym做到这一点?
Thanks 谢谢
None of the gym.Spaces
provided by the gym
package at the time of writing can be used to mirror a one hot encoding representation. gym
gym.Spaces
。 gym
包在撰写本文时提供的空间可用于镜像一个热编码表示。
Luckily for us, we can define our own space by creating a child class of gym.Spaces
. 对我们来说幸运的是,我们可以通过创建一个
gym.Spaces
子类来定义自己的空间。
I have made such a class, which may be what you need: 我做了这样的一堂课,可能是您需要的:
import gym
import numpy as np
class OneHotEncoding(gym.Space):
"""
{0,...,1,...,0}
Example usage:
self.observation_space = OneHotEncoding(size=4)
"""
def __init__(self, size=None):
assert isinstance(size, int) and size > 0
self.size = size
gym.Space.__init__(self, (), np.int64)
def sample(self):
one_hot_vector = np.zeros(self.size)
one_hot_vector[np.random.randint(self.size)] = 1
return one_hot_vector
def contains(self, x):
if isinstance(x, (list, tuple, np.ndarray)):
number_of_zeros = list(x).contains(0)
number_of_ones = list(x).contains(1)
return (number_of_zeros == (self.size - 1)) and (number_of_ones == 1)
else:
return False
def __repr__(self):
return "OneHotEncoding(%d)" % self.size
def __eq__(self, other):
return self.size == other.size
You can use it thus: 您可以这样使用它:
-> space = OneHotEncoding(size=3)
-> space.sample()
array([0., 1., 0.])
-> space.sample()
array([1., 0., 0.])
-> space.sample()
array([0., 0., 1.])
Hope I could help 希望我能帮上忙
The "multi one hot" space you ask for is implemented already 您要求的“多热点”空间已经实现
https://github.com/openai/gym/blob/master/gym/spaces/multi_binary.py https://github.com/openai/gym/blob/master/gym/spaces/multi_binary.py
import gym
# create a MultiBinary Space
# by passing n=10, each sample will contain 10 elements
mb = gym.spaces.MultiBinary(n=10)
mb.sample()
# array([1, 0, 1, 0, 0, 0, 0, 0, 0, 1], dtype=int8)
If you wanted to implement your own to ensure that there were no more than some number x of positive elements when calling sample
, you could choose x indices randomly from n options and then flip those indices in an array of all 0
and then return that. 如果要实现自己的方法以确保在调用
sample
时不超过x个正元素,则可以从n个选项中随机选择x个索引,然后将这些索引翻转为全0
的数组,然后返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.