当前位置: 首页>>代码示例>>Python>>正文


Python numpy.flatten方法代码示例

本文整理汇总了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 
开发者ID:HumanCompatibleAI,项目名称:adversarial-policies,代码行数:39,代码来源:multi_agent.py

示例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 
开发者ID:huxiaoman7,项目名称:PaddlePaddle_code,代码行数:10,代码来源:conv.py


注:本文中的numpy.flatten方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。