Medial Code Documentation
Loading...
Searching...
No Matches
micNet2.h
1//==============================================================
2// micNet2
3//==============================================================
4// simple deep learning network implementation.
5// This variant is the second phase - writing things more ordered and flexible + adding lots of new options
6//
7
8#ifndef __micNet__H__
9#define __minNet__H__
10
11#include <vector>
12#include <MedMat/MedMat/MedMat.h>
13
14using namespace std;
15
16enum NodesTypes {Node_Input=0, Node_LeakyReLU, NodeSoftMax, Node_Normalization, Node_Regression, Node_MaxOut, Node_Chooser};
17
18// next class is used when reading a network structure from the user,
19// and contains the full information about the type of each node , its parameters, and the graph structure
20// format to read a node-info:
21// <id=[int]|type=[LR,SoftMax,Norm,Regression,MaxOut,Chooser,Noise]|n_hidden=[int]|sources=[int:int:...]|lr=[float]|drop=[float]|...
22// ...keep|A=[float]|B=[float]|lambda=[float]|momentum=[float]|decay=[float]|max_width=[int]|noise_std=[float]|...
23// ...wgt_std=[float]|train=[int]>
24class NodeInfo {
25public:
26
27 // inputs
28
29 int id; // id 0 is always special and is always the input layer (x)
30 int keep_node; // if 1 will keep current node as is, however one can still change lr,drop,momentum,decay
31 string type; // "Input","LeakyReLU","SoftMax","Normalization","Regression","MaxOut","Chooser"
32 int n_hidden; // number of hidden layers.
33 vector<int> sources; // input nodes, given in order.
34 float learn_rate; // learn_rate
35 float momentum; // momentum for this node
36 float rate_decay; // learn rate decay for this node.
37 float drop_in; // dropout on input
38 float A, B; // variables for LeakyReLU
39 float lambda; // lambda for L2 Regularization whenever relevant
40 int max_width; // width for max out layers
41 float noise_std; // noise level to add to input in training stages
42 float wgt_std; // std for initialization, if == 0 calculated auto as 2/sqrt(n_in)
43 int to_train; // if 1 (default) weights will be learned, if 0 current weights stay
44
45 // calculated from inputs
46 vector<int> sinks; // output nodes for this node
47 vector<pair<int, int>> sources_idx; // indexes of sources into the input matrix
48 vector<pair<int, int>> sinks_idx; // matching indexes in sink matrix
49 float drop_out;
50 int in_dim, out_dim; // non bias dimensions for input and output
51
52 // init
53 NodeInfo() {
54 id=-1; keep_node=0; type=""; n_hidden=0; learn_rate = -1; momentum = -1; rate_decay = -1; drop_in = -1;
55 drop_out = -1; A = -1; B = -1; lambda = -1; max_width = 0; noise_std = -1; wgt_std = -1; to_train = 1;
56 }
57
58 void init_from_string(const string &in_str);
59
60};
61
62//
63// general base class for a node
64// has the basic structures that all nodes share
65// each specific node type should expand the missing featues.
66// each node must maintain the basic functions such as : init , forward, backward
67// and maintaining all that is needed in order that is needed for next nodes to forward (batch_out) and previous nodes to backprop (...)
68//
70public:
71 int type;
72 NodeInfo ni;
73
74 int mode; // either NET_MODE_TRAIN or NET_MODE_TEST
75
76 int n_in, k_out; // dimensions of input without bias: n x k
77 MedMat<float> batch_in; // matrix of nb x (n + 1) : batch_in(i,n) is always 1 to work with the bias term
78 //MedMat<float> *batch_in; // pointer to batch_in, as sometimes the batch_out of previous node is the batch_in, and this saves copying
79 MedMat<float> batch_out; // matrix of nb x (k + 1) : batch_out(i,k) is always 1 to work with the next bias term
80 MedMat<float> wgt; // size: (n + 1) x (k + 1) : wgt(n,j) is the bias for neuron j, wgt(i,k) is 0, wgt(n,k) is 1 to transfer the 1 bias carrier.
81 MedMat<float> grad_w; // size: (n + 1) x (k + 1) : gradient for wgt. wgt(i,k) is always 0.
82 MedMat<float> F; // size: nb x (k + 1) :: dL/dZ :: recursion F = delta * wgt^T
83 MedMat<float> G; // size: nb x (k + 1) :: dZ/dS :: the gradient of the actual activation function
84 MedMat<float> delta; // size: nb x (k + 1) :: recursion : delta = F(next) @ G
85 MedMat<float> S; // size: nb x (k + 1) :: linear response before non linear activation :: S = batch_in * wgt
86
87 int get_S_flag; // to be set by each node type , to sign Forward if to get S or not
88 int Forward(); // general implementation that will use the forward of the specific node when needed
89 int Backward(); // general implementation that will use the backward of the specific node when needed
90
91 virtual int init(NodeInfo _ni) = 0;
92 virtual int forward() { return 0; }
93 virtual int backward() { return 0; }
94};
95
96//
97// LeakyRelu Node:
98// The LeakyReLU activation function is :
99//
100// { A ... if <w,x> >= 0
101// f(x) = {
102// { B ... if <w,x> < 0
103//
104// Typically A=1 B=0 or 0.01 or 0.1
105//
106
108 micNodeLeakyReLU() { type = Node_LeakyReLU; get_S_flag = 1; }
109
110 int forward();
111};
112
113#endif
Definition MedMat.h:63
Definition micNet2.h:24
Definition micNet2.h:69
Definition micNet2.h:107
Definition StdDeque.h:58