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


Python ndarray.sum方法代码示例

本文整理汇总了Python中mxnet.ndarray.sum方法的典型用法代码示例。如果您正苦于以下问题:Python ndarray.sum方法的具体用法?Python ndarray.sum怎么用?Python ndarray.sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mxnet.ndarray的用法示例。


在下文中一共展示了ndarray.sum方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: unsorted_1d_segment_sum

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def unsorted_1d_segment_sum(input, seg_id, n_segs, dim):
    # TODO: support other dimensions
    assert dim == 0, 'MXNet only supports segment sum on first dimension'

    # Use SPMV to simulate segment sum
    ctx = input.context
    n_inputs = input.shape[0]
    input_shape_suffix = input.shape[1:]
    input = input.reshape(n_inputs, -1)
    n_range = nd.arange(n_inputs, dtype='int64').as_in_context(input.context)
    w_nnz = nd.ones(n_inputs).as_in_context(input.context)
    w_nid = nd.stack(seg_id, n_range, axis=0)
    w = nd.sparse.csr_matrix((w_nnz, (seg_id, n_range)), (n_segs, n_inputs))
    w = w.as_in_context(input.context)
    y = nd.dot(w, input)
    y = nd.reshape(y, (n_segs,) + input_shape_suffix)
    return y 
开发者ID:dmlc,项目名称:dgl,代码行数:19,代码来源:tensor.py

示例2: get_rmse_log

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def get_rmse_log(net, X_train, y_train):
    """Gets root mse between the logarithms of the prediction and the truth."""
    num_train = X_train.shape[0]
    clipped_preds = nd.clip(net(X_train), 1, float('inf'))
    return np.sqrt(2 * nd.sum(square_loss(
        nd.log(clipped_preds), nd.log(y_train))).asscalar() / num_train) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:8,代码来源:kaggle_k_fold_cross_validation.py

示例3: contrast_aug

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def contrast_aug(self, src, x):
      alpha = 1.0 + random.uniform(-x, x)
      coef = nd.array([[[0.299, 0.587, 0.114]]])
      gray = src * coef
      gray = (3.0 * (1.0 - alpha) / gray.size) * nd.sum(gray)
      src *= alpha
      src += gray
      return src 
开发者ID:deepinsight,项目名称:insightface,代码行数:10,代码来源:image_iter.py

示例4: saturation_aug

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def saturation_aug(self, src, x):
      alpha = 1.0 + random.uniform(-x, x)
      coef = nd.array([[[0.299, 0.587, 0.114]]])
      gray = src * coef
      gray = nd.sum(gray, axis=2, keepdims=True)
      gray *= (1.0 - alpha)
      src *= alpha
      src += gray
      return src 
开发者ID:deepinsight,项目名称:insightface,代码行数:11,代码来源:image_iter.py

示例5: sum

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def sum(input, dim, keepdims=False):
    return nd.sum(input, axis=dim, keepdims=keepdims) 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:tensor.py

示例6: reduce_sum

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def reduce_sum(input):
    return input.sum() 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:tensor.py

示例7: backward

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def backward(self, grad_out):
        lhs_data_nd, rhs_data_nd, out_data_nd, feat_shape, degs = self.saved_tensors
        if self.reducer == 'mean':
            grad_out = grad_out / degs
        grad_out_nd = zerocopy_to_dgl_ndarray(grad_out)
        grad_lhs = nd.empty((lhs_data_nd.shape[0],) + feat_shape,
                            ctx=grad_out.context, dtype=grad_out.dtype)
        K.backward_lhs_binary_op_reduce(
            self.reducer if self.reducer != 'mean' else 'sum',
            self.binary_op, self.graph, self.lhs, self.rhs,
            lhs_data_nd, rhs_data_nd, out_data_nd, grad_out_nd,
            zerocopy_to_dgl_ndarray_for_write(grad_lhs), self.lhs_map[1],
            self.rhs_map[1], self.out_map[1])
        grad_lhs = _reduce_grad(grad_lhs, lhs_data_nd.shape)
        grad_rhs = nd.empty((rhs_data_nd.shape[0],) + feat_shape,
                             ctx=grad_out.context, dtype=grad_out.dtype)
        K.backward_rhs_binary_op_reduce(
            self.reducer if self.reducer != 'mean' else 'sum',
            self.binary_op, self.graph, self.lhs, self.rhs,
            lhs_data_nd, rhs_data_nd, out_data_nd, grad_out_nd,
            zerocopy_to_dgl_ndarray_for_write(grad_rhs), self.lhs_map[1],
            self.rhs_map[1], self.out_map[1])
        grad_rhs = _reduce_grad(grad_rhs, rhs_data_nd.shape)
        # clear saved tensors explicitly
        self.saved_tensors = None
        return grad_lhs, grad_rhs 
开发者ID:dmlc,项目名称:dgl,代码行数:28,代码来源:tensor.py

示例8: _reduce_grad

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def _reduce_grad(grad, shape):
    """Reduce gradient on the broadcast dimension

    If there is broadcast in forward pass, gradients need to be reduced on
    broadcast dimension. This function checks the input tensor shape and
    gradient shape and perform the reduction.

    Parameters
    ----------
    grad: Tensor
        Gradient tensor
    shape: tuple
        Shape of input tensor

    Returns
    -------
    Tensor
    """
    grad_shape = grad.shape[1:]
    in_shape = shape[1:]
    if in_shape == grad_shape:
        # no need to reduce
        return grad
    num_to_squeeze = len(grad_shape) - len(in_shape)
    # pad in_shape
    in_shape = (1,) * num_to_squeeze + in_shape
    reduce_idx = np.nonzero(np.asarray(grad_shape) - np.asarray(in_shape))[0]
    reduce_idx += 1  # skip batch dim
    grad = grad.sum(axis=tuple(reduce_idx), keepdims=True)
    return grad.reshape(shape) 
开发者ID:dmlc,项目名称:dgl,代码行数:32,代码来源:tensor.py

示例9: is_no_grad

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def is_no_grad(x):
    return (x != 0).sum() == 0 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:tensor.py

示例10: reduce_sum

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def reduce_sum(x):
    return x.sum() 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:__init__.py

示例11: sum

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def sum(x, dim, keepdims=False):
    return x.sum(dim, keepdims=keepdims) 
开发者ID:dmlc,项目名称:dgl,代码行数:4,代码来源:__init__.py

示例12: edge_func

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def edge_func(self, edges):
        head = edges.src['emb']
        tail = edges.dst['emb']
        rel = edges.data['emb']
        score = head * rel * tail
        # TODO: check if there exists minus sign and if gamma should be used here(jin)
        return {'score': nd.sum(score, axis=-1)} 
开发者ID:dmlc,项目名称:dgl,代码行数:9,代码来源:score_fun.py

示例13: hybrid_forward

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def hybrid_forward(self, F, preds, label):
        label = label.astype('float32')
        dist = F.sqrt(F.sum(F.square(preds), axis=1))

        return label * F.square(dist) + (1 - label) * F.square(F.max(self._m - dist, 0)) 
开发者ID:aws-samples,项目名称:d-SNE,代码行数:7,代码来源:custom_layers.py

示例14: log_pdf

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def log_pdf(self, y):
        return nd.sum(nd.nansum(y * nd.log_softmax(self.unnormalized_mean), axis=0, exclude=True)) 
开发者ID:amzn,项目名称:xfer,代码行数:4,代码来源:obs.py

示例15: log_pdf

# 需要导入模块: from mxnet import ndarray [as 别名]
# 或者: from mxnet.ndarray import sum [as 别名]
def log_pdf(self, obs):
        self.check_observation_shapes(obs)
        raw_params_ext = self._replicate_shared_parameters()
        return sum([nd.sum(log_gaussian(obs[ii], raw_params_ext["mean"][ii], raw_params_ext["sigma"][ii]))
                    for ii in range(len(self.shapes))]) 
开发者ID:amzn,项目名称:xfer,代码行数:7,代码来源:prior.py


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