25 #ifndef BACKPROPAGATIONNEURALNETWORK_H_
26 #define BACKPROPAGATIONNEURALNETWORK_H_
39 template <
typename eT>
50 setLoss <mic::neural_nets::loss::CrossEntropyLoss<eT> >();
65 template<
typename LossFunction>
67 loss = std::make_shared< LossFunction > (LossFunction());
76 void forward(mic::types::MatrixPtr<eT> input_data,
bool skip_dropout =
false) {
78 assert(
layers.size() != 0);
81 LOG(LDEBUG) <<
"Inputs size: " << input_data->rows() <<
"x" << input_data->cols();
82 LOG(LDEBUG) <<
"First layer input matrix size: " <<
layers[0]->s[
'x']->rows() <<
"x" <<
layers[0]->s[
'x']->cols();
86 assert((
layers[0]->s[
'x'])->rows() == input_data->rows());
96 for (
size_t i = 0; i <
layers.size()-1; i++) {
109 (*(
layers[0]->s[
'x'])) = (*input_data);
112 for (
size_t i = 0; i <
layers.size(); i++) {
113 LOG(LDEBUG) <<
"Layer [" << i <<
"] " <<
layers[i]->name() <<
": (" <<
114 layers[i]->inputSize() <<
"x" <<
layers[i]->batchSize() <<
") -> (" <<
115 layers[i]->outputSize() <<
"x" <<
layers[i]->batchSize() <<
")";
118 layers[i]->forward(skip_dropout);
132 for (
size_t i = 0; i <
layers.size()-1; i++) {
133 bool layer_ok =
true;
135 if (
layers[i]->s[
'y']->rows() !=
layers[i+1]->s[
'x']->rows()) {
136 LOG(LERROR) <<
"Layer["<<i<<
"].y differs from " <<
"Layer["<<i+1<<
"].x";
142 if (
layers[i]->g[
'y']->rows() !=
layers[i+1]->g[
'x']->rows()) {
143 LOG(LERROR) <<
"Layer["<<i<<
"].dy differs from " <<
"Layer["<<i+1<<
"].dx";
149 LOG(LINFO) <<
"Layer["<<i<<
"]: " << (*
layers[i]).streamLayerParameters();
150 LOG(LINFO) <<
"Layer["<<i+1<<
"]: " << (*
layers[i+1]).streamLayerParameters();
161 void backward(mic::types::MatrixPtr<eT> gradients_) {
163 assert(
layers.size() != 0);
165 LOG(LDEBUG) <<
"Last layer output gradient matrix size: " <<
layers.back()->g[
'y']->cols() <<
"x" <<
layers.back()->g[
'y']->rows();
166 LOG(LDEBUG) <<
"Passed target matrix size: " << gradients_->cols() <<
"x" << gradients_->rows();
169 assert((
layers.back()->g[
'y'])->cols() == gradients_->cols());
170 assert((
layers.back()->g[
'y'])->rows() == gradients_->rows());
173 (*(
layers.back()->g[
'y'])) = (*gradients_);
176 for (
int i =
layers.size() - 1; i >= 0; i--) {
191 eT
train(mic::types::MatrixPtr<eT> encoded_batch_, mic::types::MatrixPtr<eT> encoded_targets_, eT learning_rate_, eT decay_ = 0.0f) {
200 mic::types::MatrixPtr<eT> dy =
loss->calculateGradient(encoded_targets_, encoded_predictions);
206 update(learning_rate_, decay_);
209 eT loss_value =
loss->calculateMeanLoss(encoded_targets_, encoded_predictions);
222 eT
test(mic::types::MatrixPtr<eT> encoded_batch_, mic::types::MatrixPtr<eT> encoded_targets_) {
224 bool skip_dropout =
true;
226 forward(encoded_batch_, skip_dropout);
232 return loss->calculateMeanLoss(encoded_targets_, encoded_predictions);
242 eT
calculateMeanLoss(mic::types::MatrixPtr<eT> encoded_targets_, mic::types::MatrixPtr<eT> encoded_predictions_) {
244 return loss->calculateMeanLoss(encoded_targets_, encoded_predictions_);
261 std::shared_ptr<mic::neural_nets::loss::Loss<eT> >
loss;
void backward(mic::types::MatrixPtr< eT > gradients_)
bool connected
Flag denoting whether the layers are interconnected, thus no copying between inputs and outputs of th...
std::vector< std::shared_ptr< mic::mlnn::Layer< eT > > > layers
Class representing a multi-layer neural network based on backpropagation/gradient descent...
virtual ~BackpropagationNeuralNetwork()
Virtual descriptor - empty.
std::shared_ptr< mic::neural_nets::loss::Loss< eT > > loss
void update(eT alpha_, eT decay_=0.0f)
Class representing a multi-layer neural network.
mic::types::MatrixPtr< eT > getPredictions()
eT test(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_)
void resizeBatch(size_t batch_size_)
eT train(mic::types::MatrixPtr< eT > encoded_batch_, mic::types::MatrixPtr< eT > encoded_targets_, eT learning_rate_, eT decay_=0.0f)
BackpropagationNeuralNetwork(std::string name_="bp_net")
void forward(mic::types::MatrixPtr< eT > input_data, bool skip_dropout=false)
eT calculateMeanLoss(mic::types::MatrixPtr< eT > encoded_targets_, mic::types::MatrixPtr< eT > encoded_predictions_)