DaNNet
dnn_layer_drop.h
Go to the documentation of this file.
1 // Copyright 2019 Claes Rolen (www.rolensystems.com)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 namespace dnn
17 {
21 
28 class layer_drop: public layer
29 {
30 private:
31  arma::Mat<DNN_Dtype> drop_mask;
33 public:
40  {
41  p = prob;
42  type = "Dropout";
43  char s[100];
44  std::snprintf(s,100,"(%g)",p);
45  id = type+std::string(s);
46  };
47 
53  virtual void init(void)
54  {
55  layer::init();
56  // Default to same as previous layer
60  N_right = N_left;
61  train_par = true;
62  }
63 
70  void upd_buf_size(arma::uword nmb)
71  {
73  drop_mask.set_size(N_right, N_batch); drop_mask.zeros();
74  }
75 
81  void prop_mb(void)
82  {
83  if(phase == PHASE::TRAIN )
84  {
85  const arma::uword N_ = N_left*N_batch;
86  arma::Col<DNN_Dtype> p_mat(N_,arma::fill::randu);
87  drop_mask.zeros();
88  drop_mask.elem(arma::find(p_mat < p)).fill(1/p); // Inverted dropout
89  Y = *(left->get_Y_ptr())%drop_mask;
90  }
91  else
92  {
93  Y = *(left->get_Y_ptr());
94  }
95  }
96 
102  void backprop(void)
103  {
104  Dleft = *(right->get_Dleft_ptr())%drop_mask; // Back prop
105  }
106 
110  void disp(void)
111  {
112  layer::disp();
113  std::cout << "Dropout rate: " << p << std::endl;
114  }
115 }; // End class layer_drop
117 } // End namespace dnn
void upd_buf_size(arma::uword nmb)
Updates the buffer sizes.
std::string type
Layer type string.
layer_drop(DNN_Dtype prob)
Dropout layer constructor.
arma::Mat< DNN_Dtype > Y
Output buffer mini batch [N_right,N_batch].
DNN_Dtype p
Probability for a neuron to be present.
void disp(void)
Display info about layer.
arma::uword N_rows_left
Input rows.
layer * right
Pointer to next layer.
Dropout layer class.
arma::uword N_channels_right
Output channels, number of filters.
arma::Mat< DNN_Dtype > Dleft
Error buffer [N_left,N_batch].
Layer base class.
arma::uword N_cols_left
Input cols.
PHASE phase
Active state/phase.
virtual void init(void)
Initialize layer.
virtual void init(void)
Initialization of layer.
virtual arma::Mat< DNN_Dtype > * get_Dleft_ptr(void)
Get error buffer pointer - mini batch.
virtual void disp(void)
Display info about layer.
float DNN_Dtype
Data type used in the network (float or double)
Definition: dnn.h:28
arma::uword N_left
Total size left.
arma::uword N_rows_right
Output rows.
virtual void upd_buf_size(arma::uword nmb)
Update layer buffer sizes.
void backprop(void)
Backpropagation of mini batch propagation though layer.
arma::uword N_batch
Mini batch size.
bool train_par
Enable training.
Definition: dnn.h:22
arma::uword N_channels_left
Input channels, number of filters.
arma::uword N_right
Total size right.
arma::Mat< DNN_Dtype > drop_mask
Internal matrix for drop mask.
layer * left
Pointer to previous layer.
arma::uword N_cols_right
Output cols.
virtual arma::Mat< DNN_Dtype > * get_Y_ptr(void)
Get output buffer pointer - mini batch.
void prop_mb(void)
Forward mini batch propagation though layer.