當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。