MCPcopy Create free account
hub / github.com/apache/singa / sum

Function sum

python/singa/tensor.py:1044–1100  ·  view source on GitHub ↗

Sum of tensor elements over given axis Args: t: Singa.tensor The array_like tensor to be sumed axis: None or int or tuple of ints, optional Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of th

(t, axis=None, out=None)

Source from the content-addressed store, hash-verified

1042
1043
1044def sum(t, axis=None, out=None):
1045 '''Sum of tensor elements over given axis
1046
1047 Args:
1048 t: Singa.tensor
1049 The array_like tensor to be sumed
1050 axis: None or int or tuple of ints, optional
1051 Axis or axes along which a sum is performed.
1052 The default, axis=None, will sum all of the elements of the input array.
1053 If axis is negative it counts from the last to the first axis.
1054 If axis is a tuple of ints, a sum is performed on all of the axes specified
1055 in the tuple instead of a single axis or all the axes as before.
1056 out:Singa.tensor optional
1057 Alternative output array in which to place the result.
1058 It must have the same shape as the expected output,
1059 but the type of the output values will be cast if necessary.
1060
1061 Returns:
1062 A tensor with the same shape as t, with the specified axis removed.
1063 If a is a 0-d array, or if axis is None, a scalar is returned.
1064 If an output array is specified, a reference to out is returned
1065 '''
1066
1067 t_shape = t.shape
1068 t_ndim = t.ndim()
1069
1070 if axis is None:
1071 one = Tensor(t.shape, t.device)
1072 one.set_value(1.0)
1073 ret = tensordot(t, one, t_ndim)
1074
1075 if isinstance(axis, int):
1076 if axis < 0:
1077 axis += t_ndim
1078
1079 axis_shape = t_shape[axis]
1080 axis_shape = int(axis_shape)
1081 one = Tensor(shape=(axis_shape,), device=t.device)
1082 one.set_value(1.0)
1083 ret = tensordot(t, one, axes=([axis], [0]))
1084
1085 if isinstance(axis, tuple):
1086 l_axis = list(axis)
1087 axis_shape = [t_shape[x] for x in axis]
1088 axisshape = tuple(axis_shape)
1089 one = Tensor(axisshape, t.device)
1090 one.set_value(1.0)
1091 one_axis = [x for x in range(one.ndim())]
1092 ret = tensordot(t, one, (l_axis, one_axis))
1093
1094 if out is not None:
1095 if out.shape != ret.shape:
1096 raise ValueError('dimensions do not match')
1097 out[:] = ret
1098 return out
1099 else:
1100 return ret
1101

Callers 3

repeatMethod · 0.70
einsumFunction · 0.70
Opencl>Method · 0.50

Calls 5

ndimMethod · 0.95
set_valueMethod · 0.95
TensorClass · 0.70
tensordotFunction · 0.70
tupleFunction · 0.50

Tested by

no test coverage detected