eb0a49591e
Implemented rng::uniform and rng::binomial for single values, vectors and matrices. implemeted dropout layers and tested it. Also fixed the validation code. Before it used y one place, now it uses y_test as it should.
58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include "core/omp_config.h"
|
|
|
|
#include "utils/vector.h"
|
|
#include "utils/matrix.h"
|
|
#include "random/random.h"
|
|
|
|
|
|
namespace neural_networks{
|
|
|
|
template <typename T>
|
|
struct Dropout_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<T> binary_mask;
|
|
|
|
utils::Matrix<T> _inputs;
|
|
utils::Matrix<T> outputs;
|
|
|
|
utils::Matrix<T> dinputs;
|
|
|
|
|
|
|
|
// Default Constructor
|
|
Dropout_Layer() = default;
|
|
|
|
// Constructor
|
|
Dropout_Layer(const T rate){
|
|
this->rate = T{1} - rate;
|
|
|
|
}
|
|
|
|
void forward(const utils::Matrix<T>& inputs){
|
|
// Save input values
|
|
_inputs = inputs;
|
|
|
|
// Generate binary_mask
|
|
binary_mask = rng::binomial<T>(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<T>& dvalues){
|
|
dinputs = numerics::mul(dvalues, binary_mask);
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace neural_networks
|