dropoutMatrixMath function
If isTraining is set to true this function sets values with the probability rate to zero.
The remaining values get scaled up tp conserve the total sum of the values.
Implementation
Tensor<Matrix> dropoutMatrixMath(
Tensor<Matrix> input, double rate, bool isTraining) {
if (isTraining == false || rate == 0.0) {
return input;
}
double scale = 1.0 / (1.0 - rate);
Random random = Random();
int rows = input.shape[0];
int cols = input.shape[1];
Matrix outputValue = [];
List<bool> flatMask = [];
for (int r = 0; r < rows; r = r + 1) {
Vector row = [];
for (int c = 0; c < cols; c = c + 1) {
if (random.nextDouble() < rate) {
row.add(0.0);
flatMask.add(false);
} else {
row.add(input.data[r * cols + c] * scale);
flatMask.add(true);
}
}
outputValue.add(row);
}
Tensor<Matrix> out = Tensor<Matrix>(outputValue);
out.creator = Node(
[input],
() {
int length = rows * cols;
for (int i = 0; i < length; i = i + 1) {
if (flatMask[i]) {
input.grad[i] = input.grad[i] + out.grad[i] * scale;
}
}
},
opName: 'dropout_matrix',
cost: rows * cols,
);
return out;
}