56 arma::Mat<DNN_Dtype>
Y;
57 arma::Mat<DNN_Dtype>
Y1;
59 arma::Cube<DNN_Dtype>
W;
60 arma::Mat<DNN_Dtype>
B;
113 Y. zeros(N_right, N_batch);
114 Y1. zeros(N_right, 1);
115 Dleft.zeros(N_left, N_batch);
126 std::string n_out_s=
"["+std::to_string(lay.
N_rows_right)+
","+
130 std::string opt_s=
"";
134 std::string param_s=
"";
137 param_s = std::to_string(p);
141 init.copyfmt(std::cout);
143 os << std::left <<
" " 145 << std::setw(23) << lay.
id 146 << std::setw(30) << opt_s
147 << std::setw(12) << n_out_s
148 << std::setw(10) << param_s;
150 std::cout.copyfmt(init);
186 std::cout <<
"====================== " << std::endl;
187 std::cout <<
"Type: " << type << std::endl;
220 virtual arma::Mat<DNN_Dtype>
get_B(
void)
227 virtual arma::Cube<DNN_Dtype>
get_W(
void)
234 virtual void set_B(arma::Mat<DNN_Dtype>& b)
241 virtual void set_W(arma::Cube<DNN_Dtype>& w)
276 virtual void set_id(
const std::string str)
333 virtual void set_Y1(arma::Mat<DNN_Dtype>& y)
340 virtual void set_Y(arma::Mat<DNN_Dtype>& y)
398 opt_alg = &alg_class;
427 virtual arma::Mat<DNN_Dtype>
get_Y1(
void)
448 virtual arma::Mat<DNN_Dtype>
get_Y(
void)
493 return Dleft.memptr();
501 return Dleft.colptr(n);
516 W.save(
"dnn_w_" + name, arma::arma_ascii);
517 B.save(
"dnn_b_" + name, arma::arma_ascii);
520 W.save(
"dnn_w_" + name, arma::arma_binary);
521 B.save(
"dnn_b_" + name, arma::arma_binary);
524 std::cout <<
"Parameter format not implemented yet!" << std::endl;
541 ret = W.load(
"dnn_w_" + name, arma::auto_detect) &&
542 B.load(
"dnn_b_" + name, arma::auto_detect);
545 ret = W.load(
"dnn_w_" + name, arma::arma_ascii) &&
546 B.load(
"dnn_b_" + name, arma::arma_ascii);
549 ret = W.load(
"dnn_w_" + name, arma::arma_binary) &&
550 B.load(
"dnn_b_" + name, arma::arma_binary);
553 std::cout <<
"Parameter format not implemented yet!" << std::endl;
571 template <
typename T>
583 w*= (
DNN_Dtype)std::sqrt(2.0/(fan_in+fan_out));
589 for(arma::uword k=0; k<w.n_elem; k++ )
591 w(k) = (
DNN_Dtype)(sin(k*45191.0)/std::sqrt(fan_in+fan_out));
602 return 1/(1+std::exp(-x));
608 arma::Mat<DNN_Dtype>
sigmoid(
const arma::Mat<DNN_Dtype>& x)
610 return 1/(1+arma::trunc_exp(-x));
616 arma::Mat<DNN_Dtype>
softmax(
const arma::Mat<DNN_Dtype>& x)
619 arma::Mat<DNN_Dtype> y = arma::trunc_exp(x.each_row()-arma::max(x,0));
620 return y.each_row()/arma::sum(y,0);
virtual void enable_bias(void)
Enable bias term in layer.
virtual void set_ix(const arma::uword n)
Set layer index.
layer(void)
Layer constructor.
opt * opt_alg
Pointer to optimizer.
std::string type
Layer type string.
arma::Mat< DNN_Dtype > Y
Output buffer mini batch [N_right,N_batch].
virtual void set_B(arma::Mat< DNN_Dtype > &b)
Set bias.
virtual std::string get_id(void)
Get layer id string.
virtual arma::Mat< DNN_Dtype > get_B(void)
Get bias.
virtual void set_Y1(arma::Mat< DNN_Dtype > &y)
Set output buffer.
arma::uword N_rows_left
Input rows.
std::string id
Layer id string.
layer * right
Pointer to next layer.
virtual arma::Mat< DNN_Dtype > get_Dleft()
Get error buffer - mini batch.
virtual arma::uword get_nrof_params(void)
Get number of trainable parameters.
virtual arma::Mat< DNN_Dtype > * get_Y1_ptr(void)
Get output buffer pointer.
arma::uword N_channels_right
Output channels, number of filters.
arma::Mat< DNN_Dtype > Dleft
Error buffer [N_left,N_batch].
virtual void set_id(const std::string str)
Set layer id string.
virtual arma::Cube< DNN_Dtype > get_W(void)
Get weights.
friend std::ostream & operator<<(std::ostream &os, layer &lay)
operator <<
arma::Mat< DNN_Dtype > sigmoid(const arma::Mat< DNN_Dtype > &x)
Sigmoid function - matrix.
arma::uword N_cols_left
Input cols.
arma::Mat< DNN_Dtype > Y1
Output buffer [N_right,1].
virtual void set_Y(arma::Mat< DNN_Dtype > &y)
Set output buffer - mini batch.
virtual void set_right(layer *rptr)
Set pointer to right layer.
virtual arma::uword get_nrof_inputs(void)
Get total number of layer inputs.
void init_weights(T &w, arma::uword fan_in, arma::uword fan_out, INIT_W_ALG alg, INIT_W_DIST dist)
Initiate weights.
virtual DNN_Dtype * get_Y_colptr(const arma::uword n)
Get output buffer memory column pointer - mini batch.
virtual arma::uword get_nrof_outputs(void)
Get total number of layer outputs.
PHASE phase
Active state/phase.
virtual arma::Mat< DNN_Dtype > get_Y1(void)
Get output buffer.
virtual void init(void)
Initialize layer.
arma::Mat< DNN_Dtype > B
Bias.
virtual arma::Mat< DNN_Dtype > get_Y(void)
Get output buffer - mini batch.
virtual arma::Mat< DNN_Dtype > * get_Dleft_ptr(void)
Get error buffer pointer - mini batch.
virtual arma::uword get_batch_size(void)
Get mini batch size.
virtual arma::uword get_nrof_channels(void)
Get output buffer channel/layer size.
virtual void update(void)
Parameter update.
DNN_Dtype sigmoid(const DNN_Dtype x)
Sigmoid function - scalar.
virtual void backprop(void)
Back propagation.
virtual void disp(void)
Display info about layer.
float DNN_Dtype
Data type used in the network (float or double)
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.
virtual void set_channels_in(arma::uword l)
Set input buffer channel/layer size.
virtual arma::uword get_ix(void)
Get layer index.
virtual opt * get_opt_alg(void)
Get pointer to optimizer class.
virtual arma::uword get_nrof_cols(void)
Get output buffer column size.
virtual void disable_training(void)
Disable training of layer.
virtual void load_layer_param(std::string name, PARAM_FORMAT f=PARAM_FORMAT::AUTO)
Load layer weight and bias.
arma::Cube< DNN_Dtype > W
Weights.
virtual void set_W(arma::Cube< DNN_Dtype > &w)
Set weights.
arma::uword N_batch
Mini batch size.
bool train_par
Enable training.
arma::uword layer_ix
Layer index [0..].
arma::uword N_channels_left
Input channels, number of filters.
virtual std::string get_algorithm(void)
Get the optimizer algorithm information.
virtual void set_rows_in(arma::uword r)
Set input buffer row size.
virtual DNN_Dtype * get_Dleft_memptr(void)
Get error buffer memory pointer - mini batch.
virtual void disable_bias(void)
Disable bias term in layer.
virtual void set_Dleft(arma::Mat< DNN_Dtype > &d)
Set error output buffer - mini batch.
virtual arma::uword get_nrof_rows(void)
Get output buffer row size.
virtual void set_cols_in(arma::uword c)
Set input buffer column size.
virtual void set_batch_size(const arma::uword n)
Set mini batch size.
virtual std::string get_type(void)
Get layer type string.
virtual void set_opt_alg(opt &alg_class)
Set pointer to optimizer class.
arma::uword N_right
Total size right.
virtual DNN_Dtype * get_Y1_memptr(void)
Get output buffer memory pointer.
layer * left
Pointer to previous layer.
arma::uword N_cols_right
Output cols.
virtual void save_layer_param(std::string name, PARAM_FORMAT f=PARAM_FORMAT::ASCII)
Save layer weight and bias.
virtual DNN_Dtype * get_Y_memptr(void)
Get output buffer memory pointer - mini batch.
virtual void set_left(layer *lptr)
Set pointer to left layer.
virtual void set_type(const std::string str)
Set layer type string.
virtual void enable_training(void)
Enable training of layer.
virtual void prop_mb(void)
Forward propagation - mini batch.
virtual arma::Mat< DNN_Dtype > * get_Y_ptr(void)
Get output buffer pointer - mini batch.
virtual void set_phase(PHASE p)
Set phase/state.
arma::Mat< DNN_Dtype > softmax(const arma::Mat< DNN_Dtype > &x)
Softmax function - matrix.
virtual DNN_Dtype * get_Dleft_colptr(const arma::uword n)
Get error buffer memory column pointer - mini batch.
virtual void prop(void)
Forward propagation.
bool add_bias
Enable bias.