python类broadcast_to()的实例源码

linear_two.py 文件源码 项目:vampyre 作者: GAMPTeam 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def get_tgt_vec(self,r):
        """
        Computes the target vector `g` in the above description
        """
        r0,r1 = r
        g0 = 1/self.rsqrt0*r0
        if self.wvar_pos:
            gout = 1/self.wsqrt*np.broadcast_to(self.b,self.shape1)
            g1 = 1/self.rsqrt1*r1            
            g = np.hstack((gout.ravel(),g0.ravel(),g1.ravel()))
        else:
            g1 = 1/self.rsqrt1*(r1-self.b)
            g = np.hstack((g0.ravel(),g1.ravel()))
        return g
softmax_cross_entropy.py 文件源码 项目:chainer-segnet 作者: pfnet-research 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def forward_cpu(self, inputs):
        x, t = inputs
        if chainer.is_debug():
            self._check_input_values(x, t)

        log_y = log_softmax._log_softmax(x, self.use_cudnn)
        if self.cache_score:
            self.y = numpy.exp(log_y)
        if self.class_weight is not None:
            if self.class_weight.shape != x.shape:
                shape = [1 if d != 1 else -1 for d in six.moves.range(x.ndim)]
                self.class_weight = numpy.broadcast_to(
                    self.class_weight.reshape(shape), x.shape)
            log_y *= self.class_weight
        log_yd = numpy.rollaxis(log_y, 1)
        log_yd = log_yd.reshape(len(log_yd), -1)
        log_p = log_yd[numpy.maximum(t.ravel(), 0), numpy.arange(t.size)]

        # deal with the case where the SoftmaxCrossEntropy is
        # unpickled from the old version
        if self.normalize:
            count = (t != self.ignore_label).sum()
        else:
            count = len(x)
        self._coeff = 1.0 / max(count, 1)

        y = (log_p * (t.ravel() != self.ignore_label)).sum(keepdims=True) \
            * (-self._coeff)
        return y.reshape(()),
softmax_cross_entropy.py 文件源码 项目:chainer-segnet 作者: pfnet-research 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def forward_gpu(self, inputs):
        cupy = cuda.cupy
        x, t = inputs
        if chainer.is_debug():
            self._check_input_values(x, t)

        log_y = log_softmax._log_softmax(x, self.use_cudnn)
        if self.cache_score:
            self.y = cupy.exp(log_y)
        if self.class_weight is not None:
            shape = [1 if d != 1 else -1 for d in six.moves.range(x.ndim)]
            log_y *= cupy.broadcast_to(
                self.class_weight.reshape(shape), x.shape)
        if self.normalize:
            coeff = cupy.maximum(1, (t != self.ignore_label).sum())
        else:
            coeff = max(1, len(t))
        self._coeff = cupy.divide(1.0, coeff, dtype=x.dtype)

        log_y = cupy.rollaxis(log_y, 1, log_y.ndim)
        ret = cuda.reduce(
            'S t, raw T log_y, int32 n_channel, raw T coeff', 'T out',
            't == -1 ? T(0) : log_y[_j * n_channel + t]',
            'a + b', 'out = a * -coeff[0]', '0', 'crossent_fwd'
        )(t, log_y.reduced_view(), log_y.shape[-1], self._coeff)
        return ret,
softmax_cross_entropy.py 文件源码 项目:chainer-segnet 作者: pfnet-research 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def backward_cpu(self, inputs, grad_outputs):
        x, t = inputs
        gloss = grad_outputs[0]
        if hasattr(self, 'y'):
            y = self.y.copy()
        else:
            y = log_softmax._log_softmax(x, self.use_cudnn)
            numpy.exp(y, out=y)
        if y.ndim == 2:
            gx = y
            gx[numpy.arange(len(t)), numpy.maximum(t, 0)] -= 1
            if self.class_weight is not None:
                c = self.class_weight[
                    numpy.arange(len(t)), numpy.maximum(t, 0)]
                gx *= numpy.broadcast_to(numpy.expand_dims(c, 1), gx.shape)
            gx *= (t != self.ignore_label).reshape((len(t), 1))
        else:
            # in the case where y.ndim is higher than 2,
            # we think that a current implementation is inefficient
            # because it yields two provisional arrays for indexing.
            n_unit = t.size // len(t)
            gx = y.reshape(y.shape[0], y.shape[1], -1)
            fst_index = numpy.arange(t.size) // n_unit
            trd_index = numpy.arange(t.size) % n_unit
            gx[fst_index, numpy.maximum(t.ravel(), 0), trd_index] -= 1
            if self.class_weight is not None:
                c = self.class_weight.reshape(gx.shape)
                c = c[fst_index, numpy.maximum(t.ravel(), 0), trd_index]
                c = c.reshape(y.shape[0], 1, -1)
                gx *= numpy.broadcast_to(c, gx.shape)
            gx *= (t != self.ignore_label).reshape((len(t), 1, -1))
            gx = gx.reshape(y.shape)
        gx *= gloss * self._coeff
        return gx, None
thresholding.py 文件源码 项目:skan 作者: jni 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def broadcast_mgrid(arrays):
    shape = tuple(map(len, arrays))
    ndim = len(shape)
    result = []
    for i, arr in enumerate(arrays, start=1):
        reshaped = np.broadcast_to(arr[[...] + [np.newaxis] * (ndim - i)],
                                   shape)
        result.append(reshaped)
    return result
csr.py 文件源码 项目:skan 作者: jni 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def numba_csgraph(csr, node_props=None):
    if node_props is None:
        node_props = np.broadcast_to(1., csr.shape[0])
        node_props.flags.writeable = True
    return CSGraph(csr.indptr, csr.indices, csr.data,
                   np.array(csr.shape, dtype=np.int32), node_props)
test_indexing.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def test_indexing_array_weird_strides(self):
        # See also gh-6221
        # the shapes used here come from the issue and create the correct
        # size for the iterator buffering size.
        x = np.ones(10)
        x2 = np.ones((10, 2))
        ind = np.arange(10)[:, None, None, None]
        ind = np.broadcast_to(ind, (10, 55, 4, 4))

        # single advanced index case
        assert_array_equal(x[ind], x[ind.copy()])
        # higher dimensional advanced index
        zind = np.zeros(4, dtype=np.intp)
        assert_array_equal(x2[ind, zind], x2[ind.copy(), zind])
stride_tricks.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def broadcast_to(array, shape, subok=False):
    """Broadcast an array to a new shape.

    Parameters
    ----------
    array : array_like
        The array to broadcast.
    shape : tuple
        The shape of the desired array.
    subok : bool, optional
        If True, then sub-classes will be passed-through, otherwise
        the returned array will be forced to be a base-class array (default).

    Returns
    -------
    broadcast : array
        A readonly view on the original array with the given shape. It is
        typically not contiguous. Furthermore, more than one element of a
        broadcasted array may refer to a single memory location.

    Raises
    ------
    ValueError
        If the array is not compatible with the new shape according to NumPy's
        broadcasting rules.

    Notes
    -----
    .. versionadded:: 1.10.0

    Examples
    --------
    >>> x = np.array([1, 2, 3])
    >>> np.broadcast_to(x, (3, 3))
    array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
    """
    return _broadcast_to(array, shape, subok=subok, readonly=True)
dfun.py 文件源码 项目:BAG_framework 作者: ucb-art 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def _fd(self, xi, idx, delta):
        """Calculate the derivative along the given index using central finite difference.

        Parameters
        ----------
        xi : array_like
            The coordinates to evaluate, with shape (..., ndim)
        idx : int
            The index to calculate the derivative on.
        delta : float
            The finite difference step size.

        Returns
        -------
        val : np.multiarray.ndarray
            The derivatives at the given coordinates.
        """
        if idx < 0 or idx >= self.ndim:
            raise ValueError('Invalid derivative index: %d' % idx)

        xi = np.asarray(xi, dtype=float)
        if xi.shape[-1] != self.ndim:
            raise ValueError("The requested sample points xi have dimension %d, "
                             "but this interpolator has dimension %d" % (xi.shape[-1], self.ndim))

        # use broadcasting to evaluate two points at once
        xtest = np.broadcast_to(xi, (2,) + xi.shape).copy()
        xtest[0, ..., idx] += delta / 2.0
        xtest[1, ..., idx] -= delta / 2.0
        val = self(xtest)
        ans = (val[0] - val[1]) / delta  # type: np.ndarray

        if ans.size == 1 and not np.isscalar(ans):
            return ans[0]
        return ans
dfun.py 文件源码 项目:BAG_framework 作者: ucb-art 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def _fd_jacobian(self, xi, delta_list):
        """Calculate the Jacobian matrix using central finite difference.

        Parameters
        ----------
        xi : array_like
            The coordinates to evaluate, with shape (..., ndim)
        delta_list : List[float]
            list of finite difference step sizes for each input.

        Returns
        -------
        val : np.multiarray.ndarray
            The Jacobian matrices at the given coordinates.
        """
        xi = np.asarray(xi, dtype=float)
        if xi.shape[-1] != self.ndim:
            raise ValueError("The requested sample points xi have dimension %d, "
                             "but this interpolator has dimension %d" % (xi.shape[-1], self.ndim))

        # use broadcasting to evaluate all points at once
        xtest = np.broadcast_to(xi, (2 * self.ndim,) + xi.shape).copy()
        for idx, delta in enumerate(delta_list):
            xtest[2 * idx, ..., idx] += delta / 2.0
            xtest[2 * idx + 1, ..., idx] -= delta / 2.0

        val = self(xtest)
        ans = np.empty(xi.shape)
        for idx, delta in enumerate(delta_list):
            ans[..., idx] = (val[2 * idx, ...] - val[2 * idx + 1, ...]) / delta
        return ans
hmm.py 文件源码 项目:NetPower_TestBed 作者: Vignesh2208 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _fix_priors_shape(self):
        # If priors are numbers, this function will make them into a
        # matrix of proper shape
        self.weights_prior = np.broadcast_to(
            self.weights_prior, (self.n_components, self.n_mix)).copy()
        self.means_prior = np.broadcast_to(
            self.means_prior,
            (self.n_components, self.n_mix, self.n_features)).copy()
        self.means_weight = np.broadcast_to(
            self.means_weight,
            (self.n_components, self.n_mix)).copy()

        if self.covariance_type == "full":
            self.covars_prior = np.broadcast_to(
                self.covars_prior,
                (self.n_components, self.n_mix,
                 self.n_features, self.n_features)).copy()
            self.covars_weight = np.broadcast_to(
                self.covars_weight, (self.n_components, self.n_mix)).copy()
        elif self.covariance_type == "tied":
            self.covars_prior = np.broadcast_to(
                self.covars_prior,
                (self.n_components, self.n_features, self.n_features)).copy()
            self.covars_weight = np.broadcast_to(
                self.covars_weight, self.n_components).copy()
        elif self.covariance_type == "diag":
            self.covars_prior = np.broadcast_to(
                self.covars_prior,
                (self.n_components, self.n_mix, self.n_features)).copy()
            self.covars_weight = np.broadcast_to(
                self.covars_weight,
                (self.n_components, self.n_mix, self.n_features)).copy()
        elif self.covariance_type == "spherical":
            self.covars_prior = np.broadcast_to(
                self.covars_prior, (self.n_components, self.n_mix)).copy()
            self.covars_weight = np.broadcast_to(
                self.covars_weight, (self.n_components, self.n_mix)).copy()
som.py 文件源码 项目:somber 作者: stephantul 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _grid_distance(self, index):
        """
        Calculate the distance grid for a single index position.

        This is pre-calculated for fast neighborhood calculations
        later on (see _calc_influence).
        """
        # Take every dimension but the first in reverse
        # then reverse that list again.
        dimensions = np.cumprod(self.map_dimensions[1::][::-1])[::-1]

        coord = []
        for idx, dim in enumerate(dimensions):
            if idx != 0:
                value = (index % dimensions[idx-1]) // dim
            else:
                value = index // dim
            coord.append(value)

        coord.append(index % self.map_dimensions[-1])

        for idx, (width, row) in enumerate(zip(self.map_dimensions, coord)):
            x = np.abs(np.arange(width) - row) ** 2
            dims = self.map_dimensions[::-1]
            if idx:
                dims = dims[:-idx]
            x = np.broadcast_to(x, dims).T
            if idx == 0:
                distance = np.copy(x)
            else:
                distance += x.T

        return distance
patterns.py 文件源码 项目:imgProcessor 作者: radjkarl 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def patCycles(s0, s1=50, return_x=False):
    arr = np.zeros(s0)
    p = 1
    t = 1
    c = 0
    x,y = [],[]
    while True:
        arr[p:p+t] = 1
        p+=t
        x.append(2*t)
        y.append(p+0.5*t)
        if c > s1:
            t+=1

        c += 2
        p+=t

        if p>s0:
            break

    arr = arr[::-1]
    arr = np.broadcast_to(arr, (s1, s0))
    if return_x:
        #cycles/px:
        x =np.interp(np.arange(s0), y, x)  
        return arr,1/x[::-1]
    else:
        return arr
compute.py 文件源码 项目:paradox 作者: ictxiangxin 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def compute_max_unpooling_nd(data, pooling, size, step, dimension: int):
    result = []
    data_prefix_shape = data.shape[:-dimension]
    kernel_prefix_shape = pooling.shape[:-dimension]
    final_shape = element_wise_shape(data_prefix_shape, kernel_prefix_shape)[0]
    data = numpy.broadcast_to(data, final_shape + data.shape[-dimension:])
    pooling = numpy.broadcast_to(pooling, final_shape + pooling.shape[-dimension:])
    if final_shape:
        for key in array_index_traversal(final_shape):
            result.append(__compute_max_unpooling_nd(data[key], pooling[key], size, step, dimension))
        return numpy.array(result).reshape(final_shape + result[0].shape)
    else:
        return __compute_max_unpooling_nd(data, pooling, size, step, dimension)
autodiff.py 文件源码 项目:Aurora 作者: upul 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def compute(self, node, input_vals, output_val, use_numpy=True):
        assert len(input_vals) == 2
        if use_numpy:
            output_val[:] = np.broadcast_to(input_vals[0], input_vals[1].shape)
        else:
            gpu_op.broadcast_to(input_vals[0], output_val)
test_gpu_operations.py 文件源码 项目:Aurora 作者: upul 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def test_broadcast_to():
    ctx = ndarray.gpu(0)
    shape = (200, 300)
    to_shape = (130, 200, 300)
    x = np.random.uniform(-1, 1, shape).astype(np.float32)
    arr_x = ndarray.array(x, ctx=ctx)
    arr_y = ndarray.empty(to_shape, ctx=ctx)
    gpu_op.broadcast_to(arr_x, arr_y)
    y = arr_y.asnumpy()
    np.testing.assert_allclose(np.broadcast_to(x, to_shape), y)
test_indexing.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_indexing_array_weird_strides(self):
        # See also gh-6221
        # the shapes used here come from the issue and create the correct
        # size for the iterator buffering size.
        x = np.ones(10)
        x2 = np.ones((10, 2))
        ind = np.arange(10)[:, None, None, None]
        ind = np.broadcast_to(ind, (10, 55, 4, 4))

        # single advanced index case
        assert_array_equal(x[ind], x[ind.copy()])
        # higher dimensional advanced index
        zind = np.zeros(4, dtype=np.intp)
        assert_array_equal(x2[ind, zind], x2[ind.copy(), zind])
numerical_check.py 文件源码 项目:autodiff 作者: bgavran 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def numerical_check(test, graph, wrt_vars, order=1):
    backprop_graphs, numeric_grads = differentiate_n_times_num(graph, wrt_vars, order=order)

    for wrt_var, graph_grad, num_grad in zip(wrt_vars, backprop_graphs, numeric_grads):
        name = "num" + str(order) + "df_wrt_" + wrt_var.name
        if graph.name == "extra_exp_op":
            name += " as input to another op!!!"
        with test.subTest(name):
            print("---------- " + name + " ----------")
            print("Backprop grad:", graph_grad())
            print("Numeric grad:", num_grad)
            broadcasted_grad = np.broadcast_to(graph_grad(), wrt_var().shape)  # not necessarily the same shape
            arrays_allclose(broadcasted_grad, num_grad)
utils.py 文件源码 项目:autodiff 作者: bgavran 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def eval_graphs(my_graph, tf_graph, my_var, tf_var, n):
    tf_grads = 0
    if tf_graph is not None:
        tf_grads = tf_graph.eval()
    my_grads = my_graph()

    print("---------- " + str(n) + "df w.r.t. " + str(my_var) + " ----------")
    print("My_val:", my_grads)
    print("Tf_val:", tf_grads)
    my_val = np.broadcast_to(my_grads, my_var.shape)
    tf_val = np.broadcast_to(tf_grads, my_var.shape)
    arrays_allclose(my_val, tf_val)
ops.py 文件源码 项目:autodiff 作者: bgavran 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def _eval(self):
        arr = [op() for op in self.operands]

        for i, val in enumerate(arr):
            if isinstance(val, numbers.Number):
                shp = [l for let in Einsum.split_dots(self.opnames[i]) for l in self.letter_to_dim.get(let, [1])]
                arr[i] = np.broadcast_to(val, shp)

        return np.einsum(self.op_str, *arr)


问题


面经


文章

微信
公众号

扫码关注公众号