Sum¶
Description¶
This module calculates the sum of tensor elements along a given axis.
Initializing¶
def __init__(self, axis, useWeights=True, name=None):
Parameters
Parameter | Allowed types | Description | Default |
---|---|---|---|
axis | int | Axis number along which the summation is performed | - |
useWeights | bool | Inclusion flag for the weights tensor during summation | True |
name | str | Layer name | None |
Explanations
useWeights
The use of weights is possible only for three-dimensional tensors.
Examples¶
Necessary imports.
>>> import numpy as np
>>> from PuzzleLib.Backend import gpuarray
>>> from PuzzleLib.Modules import Sum
>>> batchsize, groups, size = 2, 3, 4
>>> np.random.seed(1234)
>>> data = gpuarray.to_gpu(np.random.randint(0, 9, (batchsize, groups, size)).astype(np.float32))
>>> print(data)
[[[3. 6. 5. 4.]
[8. 1. 7. 6.]
[8. 0. 5. 0.]]
[[6. 2. 0. 5.]
[2. 6. 3. 7.]
[0. 0. 3. 2.]]]
Let us initialize the module with summation along the groups
axis:
>>> summod = Sum(axis=1, useWeights=False)
>>> summod(data)
[[19. 7. 17. 10.]
[ 8. 8. 6. 14.]]
>>> print(summod.data.shape)
(2, 4)
Let us change the axis of summation:
>>> summod = Sum(axis=2, useWeights=False)
>>> summod(data)
[[18. 22. 13.]
[13. 18. 5.]]
>>> print(summod.data.shape)
(2, 3)
The weights tensor must be one dimension less than the dimension of the data:
>>> weights = gpuarray.to_gpu(np.random.randint(1, 3, (batchsize, groups)).astype(np.float32))
>>> print(weights)
[[2. 1. 2.]
[2. 2. 2.]]
>>> summod = Sum(axis=1, useWeights=True)
>>> summod([data, weights])
[[30. 13. 27. 14.]
[16. 16. 12. 28.]]
>>> print(summod.data.shape)
(2, 4)