python类result_type()的实例源码

forward.py 文件源码 项目:cebl 作者: idfah 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def __init__(self, x, g, nHidden=10, transFunc=transfer.lecun,
                 weightInitFunc=pinit.lecun, penalty=None, elastic=1.0,
                 optimFunc=optim.scg, **kwargs):
        x = np.asarray(x)
        g = np.asarray(g)
        self.dtype = np.result_type(x.dtype, g.dtype)

        self.flattenOut = False if g.ndim > 1 else True

        Regression.__init__(self, util.colmat(x).shape[1],
                            util.colmat(g).shape[1])
        optim.Optable.__init__(self)

        self.nHidden = nHidden if util.isiterable(nHidden) else (nHidden,)
        self.nHLayers = len(self.nHidden)

        self.layerDims = [(self.nIn+1, self.nHidden[0])]
        for l in xrange(1, self.nHLayers):
            self.layerDims.append((self.nHidden[l-1]+1, self.nHidden[l]))
        self.layerDims.append((self.nHidden[-1]+1, self.nOut))

        self.transFunc = transFunc if util.isiterable(transFunc) \
                else (transFunc,) * self.nHLayers
        assert len(self.transFunc) == self.nHLayers

        views = util.packedViews(self.layerDims, dtype=self.dtype)
        self.pw  = views[0]
        self.hws = views[1:-1]
        self.vw  = views[-1]

        if not util.isiterable(weightInitFunc): 
            weightInitFunc = (weightInitFunc,) * (self.nHLayers+1)
        assert len(weightInitFunc) == (len(self.hws) + 1)

        # initialize weights
        for hw, wif in zip(self.hws, weightInitFunc):
            hw[...] = wif(hw.shape).astype(self.dtype, copy=False)
        self.vw[...] = weightInitFunc[-1](self.vw.shape).astype(self.dtype, copy=False)

        self.penalty = penalty
        if self.penalty is not None:
            if not util.isiterable(self.penalty):
                self.penalty = (self.penalty,) * (self.nHLayers+1)
        assert (self.penalty is None) or (len(self.penalty) == (len(self.hws) + 1))

        self.elastic = elastic if util.isiterable(elastic) \
                else (elastic,) * (self.nHLayers+1)
        assert (len(self.elastic) == (len(self.hws) + 1))

        # train the network
        if optimFunc is not None:
            self.train(x, g, optimFunc, **kwargs)
align.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def _reconstruct_object(typ, obj, axes, dtype):
    """Reconstruct an object given its type, raw value, and possibly empty
    (None) axes.

    Parameters
    ----------
    typ : object
        A type
    obj : object
        The value to use in the type constructor
    axes : dict
        The axes to use to construct the resulting pandas object

    Returns
    -------
    ret : typ
        An object of type ``typ`` with the value `obj` and possible axes
        `axes`.
    """
    try:
        typ = typ.type
    except AttributeError:
        pass

    res_t = np.result_type(obj.dtype, dtype)

    if (not isinstance(typ, partial) and
            issubclass(typ, pd.core.generic.PandasObject)):
        return typ(obj, dtype=res_t, **axes)

    # special case for pathological things like ~True/~False
    if hasattr(res_t, 'type') and typ == np.bool_ and res_t != np.bool_:
        ret_value = res_t.type(obj)
    else:
        ret_value = typ(obj).astype(res_t)
        # The condition is to distinguish 0-dim array (returned in case of
        # scalar) and 1 element array
        # e.g. np.array(0) and np.array([0])
        if len(obj.shape) == 1 and len(obj) == 1:
            if not isinstance(ret_value, np.ndarray):
                ret_value = np.array([ret_value]).astype(res_t)

    return ret_value
core.py 文件源码 项目:sparse 作者: mrocklin 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def _get_expanded_coords_data(coords, data, params, broadcast_shape):
        """
        Expand coordinates/data to broadcast_shape. Does most of the heavy lifting for broadcast_to.
        Produces sorted output for sorted inputs.

        Parameters
        ----------
        coords : np.ndarray
            The coordinates to expand.
        data : np.ndarray
            The data corresponding to the coordinates.
        params : list
            The broadcast parameters.
        broadcast_shape : tuple[int]
            The shape to broadcast to.
        Returns
        -------
        expanded_coords : np.ndarray
            List of 1-D arrays. Each item in the list has one dimension of coordinates.
        expanded_data : np.ndarray
            The data corresponding to expanded_coords.
        """
        first_dim = -1
        expand_shapes = []
        for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape):
            if p and first_dim == -1:
                expand_shapes.append(coords.shape[1])
                first_dim = d

            if not p:
                expand_shapes.append(l)

        all_idx = COO._cartesian_product(*(np.arange(d, dtype=np.min_scalar_type(d - 1)) for d in expand_shapes))
        dt = np.result_type(*(np.min_scalar_type(l - 1) for l in broadcast_shape))

        false_dim = 0
        dim = 0

        expanded_coords = np.empty((len(broadcast_shape), all_idx.shape[1]), dtype=dt)
        expanded_data = data[all_idx[first_dim]]

        for d, p, l in zip(range(len(broadcast_shape)), params, broadcast_shape):
            if p:
                expanded_coords[d] = coords[dim, all_idx[first_dim]]
            else:
                expanded_coords[d] = all_idx[false_dim + (d > first_dim)]
                false_dim += 1

            if p is not None:
                dim += 1

        return np.asarray(expanded_coords), np.asarray(expanded_data)

    # (c) senderle
    # Taken from https://stackoverflow.com/a/11146645/774273
    # License: https://creativecommons.org/licenses/by-sa/3.0/


问题


面经


文章

微信
公众号

扫码关注公众号