#pragma once #include "core/omp_config.h" #include "utils/vector.h" #include "utils/matrix.h" #include "modules/neural_networks/layers/Layer.h" #include "random/random.h" namespace neural_networks{ template struct Dropout_Layer : Layer{ // Store rate, we invert it as for example for dropout // of 0.1 we need a success rate of 0.9 T rate = T{0}; utils::Matrix binary_mask; utils::Matrix _inputs; utils::Matrix outputs; utils::Matrix dinputs; // Default Constructor Dropout_Layer() = default; // Constructor Dropout_Layer(const T rate){ this->rate = T{1} - rate; } void forward(const utils::Matrix& inputs){ // Save input values _inputs = inputs; // Generate binary_mask binary_mask = rng::binomial(inputs.rows(), inputs.cols(), 1, rate); // Scale binary_mask binary_mask = numerics::div(binary_mask, rate); // Apply binary mask to output values outputs = numerics::mul(binary_mask, inputs); } void backward(const utils::Matrix& dvalues){ dinputs = numerics::mul(dvalues, binary_mask); } }; } // end namespace neural_networks