NoiseInjector¶
Description¶
This module implements the function of introducing noise into the data tensor.
This operation can be used both for data augmentation, as well as to increase the noise immunity of the model when training on prepared intentionally noisy data.
Additional sources¶
Initializing¶
def __init__(self, mode="add", noisetype="uniform", params=(0.0, 1.0), rng=globalRng, inplace=False, slicing=None,
name=None):
Parameters
Parameter | Allowed types | Description | Default |
---|---|---|---|
mode | str | Noise adding mode | "add" |
noisetype | str | Noise type | "uniform" |
params | tuple | Noise model parameters | (0.0, 1.0) |
rng | str | Random number generator object (backend-dependent) | globalRng |
inplace | bool | If True, then the output tensor will be written in the place of the input tensor in memory. | False |
slicing | slice | To which slice the operation is applied | None |
name | str | Layer name | None |
Explanations
mode
- noise adding mode. It can take values "add" and "mul";
noisetype
- type of added noise. It can take values "gaussian" and "uniform";
rng
- random number generator, which depends on the platform where the calculations are performed, i.e. the generator is backend-dependent: for example, when using Nvidia-GPU and CUDA, the generator is formed using the curand library;
params
- params
- tuple of noise parameters: (a, b)
for "uniform" and (mean, sigma)
for "gaussian";
inplace
- flag showing whether additional memory resources should be allocated for the result. If True, then the output tensor will be written in the place of the input tensor in memory, which can negatively affect the network, if the input tensor takes part in calculations on other branches of the graph;
slicing
- slice is set through the Python built-in slice object. Please have in mind that the slice indexes are set for the flattened data tensor;
Examples¶
Necessary imports.
>>> import numpy as np
>>> from PuzzleLib.Backend import gpuarray
>>> from PuzzleLib.Modules import NoiseInjector
Info
gpuarray
is required to properly place the tensor in the GPU
Take the normally distributed noise and the summing noise values mode with data values:
>>> data = gpuarray.to_gpu(np.random.randint(0, 9, (1, 3, 4, 4)).astype(np.float32))
>>> print(data)
[[[[6. 6. 1. 2. 6.]
[3. 6. 8. 2. 0.]
[0. 5. 4. 1. 6.]
[5. 6. 1. 3. 6.]
[4. 1. 8. 0. 0.]]
[[6. 5. 6. 2. 5.]
[8. 6. 5. 1. 8.]
[1. 7. 4. 2. 6.]
[1. 5. 0. 4. 0.]
[0. 8. 4. 6. 8.]]
[[6. 3. 8. 4. 0.]
[7. 6. 4. 8. 3.]
[5. 1. 4. 1. 1.]
[0. 8. 2. 7. 2.]
[1. 6. 6. 3. 3.]]]]
>>> injector = NoiseInjector(mode="add", noisetype="gaussian", params=(0.0, 10.0))
>>> injector(data)
[[[[ 18.497967 -1.3347244 8.175358 15.437071 -17.020744 ]
[ 11.160535 3.7274754 11.790724 -9.220343 -6.3753014 ]
[ 10.79629 7.2404356 -4.88393 -1.1487608 -4.7226696 ]
[ 5.569405 -2.0390673 -3.3565602 7.963166 23.643564 ]
[ 9.449907 -3.11933 32.06887 -13.164926 3.550262 ]]
[[ 22.858295 13.527686 38.052925 -4.5417457 8.245185 ]
[ 14.954709 9.224528 13.953817 -12.6050415 11.85663 ]
[ 7.456253 6.77518 20.842493 -3.251524 12.892044 ]
[ -2.5932856 -3.0917072 -6.9652996 -5.3049097 -8.3553 ]
[ -4.171719 4.9545865 -2.510837 17.864025 10.528694 ]]
[[ 3.267301 -3.414236 1.7571855 10.906286 -8.430187 ]
[ 9.859923 -2.7999115 -5.892701 0.3149557 6.6246815 ]
[ 16.78846 5.6055384 7.293224 -4.111868 -3.1974878 ]
[ 16.031277 25.708433 11.264681 13.54818 -3.769538 ]
[ -0.14206207 11.421783 5.7146792 3.774578 3.6769433 ]]]]
Let us change the distribution to uniform and choose the mode of multiplying noise values by data values; this time the noise will be introduced through slicing:
>>> injector = NoiseInjector(mode="mul", noisetype="uniform", params=(0.0, 10.0), slicing = slice(2 * 4 * 5, 3 * 5 * 5))
>>> injector(data)
[[[[ 6. 6. 1. 2. 6. ]
[ 3. 6. 8. 2. 0. ]
[ 0. 5. 4. 1. 6. ]
[ 5. 6. 1. 3. 6. ]
[ 4. 1. 8. 0. 0. ]]
[[ 6. 5. 6. 2. 5. ]
[ 8. 6. 5. 1. 8. ]
[ 1. 7. 4. 2. 6. ]
[ 1. 5. 0. 4. 0. ]
[ 0. 8. 4. 6. 8. ]]
[[14.204256 7.9821568 30.882828 33.621254 0. ]
[43.150726 55.367523 4.522502 49.947643 3.107901 ]
[35.815884 8.865577 23.082764 1.0058062 5.334869 ]
[ 0. 22.528423 16.319548 31.362062 15.041352 ]
[ 7.672563 19.278425 40.095898 17.783966 8.885609 ]]]]