本文整理匯總了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
示例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
示例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
示例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)
示例6: reduce_sum
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import sum [as 別名]
def reduce_sum(input):
return input.sum()
示例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
示例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)
示例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
示例10: reduce_sum
# 需要導入模塊: from mxnet import ndarray [as 別名]
# 或者: from mxnet.ndarray import sum [as 別名]
def reduce_sum(x):
return x.sum()
示例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)
示例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)}
示例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))
示例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))
示例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))])