本文整理汇总了Python中numpy.flatten方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.flatten方法的具体用法?Python numpy.flatten怎么用?Python numpy.flatten使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.flatten方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: flatten_space
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import flatten [as 别名]
def flatten_space(tuple_space):
"""Flattens a Tuple of like-spaces into a single bigger space of the appropriate type.
The spaces do not have to have the same shape, but do need to be of compatible types.
For example, we can flatten a (Box(10), Box(5)) into Box(15) or a (Discrete(2), Discrete(2))
into a MultiDiscrete([2, 2]), but cannot flatten a (Box(10), Discrete(2))."""
unique_types = set(type(space) for space in tuple_space.spaces)
if len(unique_types) > 1:
raise TypeError(f"Cannot flatten a space with more than one type: {unique_types}")
uniq_type = unique_types.pop()
if isinstance(uniq_type, gym.spaces.Discrete):
flat_space = gym.spaces.MultiDiscrete([space.n for space in tuple_space.spaces])
flatten = unflatten = lambda x: x
elif isinstance(uniq_type, gym.spaces.MultiDiscrete):
flat_space = gym.spaces.MultiDiscrete([space.nvec for space in tuple_space.spaces])
flatten = unflatten = lambda x: x
elif isinstance(uniq_type, gym.spaces.Box):
low = np.concatenate(*[space.low for space in tuple_space.spaces], axis=0)
high = np.concatenate(*[space.high for space in tuple_space.spaces], axis=0)
flat_space = gym.spaces.Box(low=low, high=high)
def flatten(x):
return np.flatten(x)
def unflatten(x):
sizes = [np.prod(space.shape) for space in tuple_space.spaces]
start = np.cumsum(sizes)
end = start[1:] + len(x)
orig = [
np.reshape(x[s:e], space.shape)
for s, e, space in zip(start, end, tuple_space.spaces)
]
return orig
else:
raise NotImplementedError("Unsupported type: f{type}")
return flat_space, flatten, unflatten
示例2: eval
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import flatten [as 别名]
def eval(self,x):
# 补零后的宽度ww和高度hh
ww = self.h - self.k_x + 2 * self.p_x + 1
hh = self.h - self.k_y + 2 * self.p_y + 1
ret = np.array([[[np.ravel(xx[:, a:a + self.k_x, b:b + self.k_y]) for b in range(0, hh, self.s_y)]
for a in range(0, ww, self.s_x)] for xx in x])
#ret = np.array([[[np.ravel(xx[:,a:a +self.k_x, b:b, self.k_y]) for b in range(0,hh,self.s_y)] for a in range(0,ww,self.s_x)] for xx in x])# here using np.ravel rather than np.flatten to save memory
return ret