def eye(N, M=None, k=0, dtype=float32, order='C'): __order_assert(order) M = N if M is None else M x = _F.one_hot(arange(0 + k, N + k, 1), M) if dtype != float32: x = _F.cast(x, dtype) return x
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None): __order_assert(order) if isinstance(object, _F.Var): if copy: x = _Copy(object) else: x = object else: if not isinstance(object, _F._Sequence): x = _F.scalar(object, dtype) else: # get shape and dtype of sequence dst_shape, item_type = _F._list_shape_type(object) x = _F.const(object, dst_shape, dtype=item_type) # if give dtype, cast to dtype if dtype is not None and dtype != x.dtype: x = _F.cast(x, dtype) # if give ndmin, unsqueeze if ndmin > 0: dim = x.ndim if ndmin > dim: x = _F.unsqueeze(x, [i for i in range(ndmin - dim)]) return x
def randint(low, high=None, size=None, dtype=_F.int): if type(low) in (list, tuple) or type(high) in (list, tuple): raise ValueError('MNN.numpy randint just support low/high is int.') if high is None: high = low low = 0 if size is None: size = [1] return _F.cast(_F.randomuniform(size, _F.float, low, high), dtype)
def __arange_3(start, stop, step=1, dtype=None): _type = type(stop) start = _F.scalar(_type(start), dtype) limit = _F.scalar(stop, dtype) delta = _F.scalar(_type(step), dtype) x = _F.range(start, limit, delta) if dtype is not None and x.dtype != dtype: x = _F.cast(x, dtype) return x
def dot(a, b, out=None): a = array(a) b = array(b) # cast to same type a, b = _F._match_dtype(a, b) ad = a.ndim bd = b.ndim def assert_dim(x, y): if x != y: raise ValueError('shapes not aligned: ', x, '!=', y, '.') # If either a or b is 0-D (scalar), it is equivalent to multiply. if ad == 0 or bd == 0: return asscalar(multiply(a, b)) # If both a and b are 1-D arrays, it is inner product of vectors. if a.ndim == 1 and b.ndim == 1: assert_dim(a.shape[0], b.shape[0]) return sum(multiply(a, b)) # If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b. if (a.ndim > 1 and b.ndim == 1) or (a.ndim == 1 and b.ndim > 1): assert_dim(a.shape[-1], b.shape[-1]) return sum(multiply(a, b), [-1]) # matmul just support float32 origin_dtype = a.dtype a, b = _F._match_dtype(a, b, float32) # If both a and b are 2-D arrays, it is matrix multiplication. if a.ndim == 2 and b.ndim == 2: assert_dim(a.shape[1], b.shape[0]) return _F.cast(_F.matmul(a, b), origin_dtype) # If a is an N-D array and b is an M-D array (where M>=2), # it is a sum product over the last axis of a and the second-to-last axis of b. if a.ndim > 2 and b.ndim > 1: reduce_dim = a.shape[-1] assert_dim(reduce_dim, b.shape[-2]) a_shape = a.shape b_shape = b.shape a_shape.pop(-1) b_shape.pop(-2) dst_shape = a_shape + b_shape a = reshape(a, [-1, reduce_dim]) b = reshape(moveaxis(b, -2, 0), [3, -1]) res = _F.cast(_F.matmul(a, b), origin_dtype) return reshape(res, dst_shape)
def eye(N, M=None, k=0, dtype=float32, order='C'): ''' eye(N, M=None, k=0, dtype=float32, order='C') Return a 2-D var with ones on the diagonal and zeros elsewhere. Parameters ---------- N : int Number of rows in the output. M : int, optional Number of columns in the output. If None, defaults to `N`. k : int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : data-type, optional Data-type of the returned array. order : {'C', 'F'}, optional Compatible with numpy. Returns ------- I : var of shape (N,M) An var where all elements are equal to zero, except for the `k`-th diagonal, whose values are equal to one. Examples -------- >>> np.eye(2, dtype=int) var([[1, 0], [0, 1]]) >>> np.eye(3, k=1) var([[0., 1., 0.], [0., 0., 1.], [0., 0., 0.]]) ''' __order_assert(order) M = N if M is None else M x = _F.one_hot(arange(0 + k, N + k, 1), M) if dtype != float32: x = _F.cast(x, dtype) return x
def var(a, axis=None, dtype=float32, out=None, ddof=0, keepdims=False): a = _F.cast(a, dtype) return mean(abs(square(a - mean(a, axis, keepdims=True))), axis)
def mean(a, axis=None, dtype=float32, out=None, keepdims=False): a = _F.cast(a, dtype) if axis is None: return asscalar(_F.reduce_mean(ravel(a))) return _F.reduce_mean(a, axis, keepdims)
def clip(x, a_min, a_max): types = (list, tuple, type(x)) if type(a_min) in types or type(a_max) in types: raise ValueError('MNN.numpy.clip just support scalar a_min/a_max.') dtype = x.dtype return _F.cast(_F.relu6(_F.cast(x), a_min, a_max), dtype)
def array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None): ''' array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0, like=None) Create an array. Parameters ---------- object : var_like An var, any object exposing the var interface, an object whose __array__ method returns an array, or any (nested) sequence. If object is a scalar, a 0-dimensional array containing object is returned. dtype : data-type, optional The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. copy : bool, optional If true (default), then the object is copied. Otherwise, a copy will only be made if __array__ returns a copy, if obj is a nested sequence, or if a copy is needed to satisfy any of the other requirements (dtype, order, etc.). order : {'C', 'F', 'A', 'K'}, optional Compatible with numpy. subok : bool, optional Compatible with numpy. ndmin : int, optional Specifies the minimum number of dimensions that the resulting array should have. Ones will be pre-pended to the shape as needed to meet this requirement. like : var_like Compatible with numpy. Returns ------- out : var An var object satisfying the specified requirements. Examples -------- >>> np.array([1, 2, 3]) var([1, 2, 3]) >>> np.array([[1, 2], [3, 4]]) var([[1, 2], [3, 4]]) ''' __order_assert(order) if isinstance(object, _F.Var): if copy: x = _Copy(object) else: x = object else: if not isinstance(object, _F._Sequence): x = _F.scalar(object, dtype) else: # get shape and dtype of sequence dst_shape, item_type = _F._list_shape_type(object) x = _F.const(object, dst_shape, dtype=item_type) # if give dtype, cast to dtype if dtype is not None and dtype != x.dtype: x = _F.cast(x, dtype) # if give ndmin, unsqueeze if ndmin > 0: dim = x.ndim if ndmin > dim: x = _F.unsqueeze(x, [i for i in range(ndmin - dim)]) return x