38 arma::Cube<DNN_Dtype>
C;
39 arma::Mat<DNN_Dtype>
Cbp;
50 const arma::uword n_kernel,
51 const arma::uword n_pad = 0,
52 const arma::uword n_stride = 1 ):
layer()
55 N_filter_size = n_kernel;
73 id =
type+
"("+std::to_string(N_filter_size)+
"x"+std::to_string(N_filter_size)+
","+
74 std::to_string(
N_channels_left)+
"*"+std::to_string(N_channels_right)+
")";
78 B. set_size(N_channels_right,1);
82 N_filter_size*N_filter_size*N_channels_right,
106 arma::Mat<DNN_Dtype>
rotate_180(
const arma::Mat<DNN_Dtype>& M)
108 arma::Mat<DNN_Dtype> U(M);
109 return arma::fliplr(arma::flipud(U));
119 arma::Cube<DNN_Dtype>
rotate_180(
const arma::Cube<DNN_Dtype>& C)
121 arma::Cube<DNN_Dtype> U(C);
122 for (arma::uword n=0; n<C.n_slices; n++)
124 U.slice(n) = arma::fliplr(arma::flipud(C.slice(n)));
140 template <
typename DNN_Dtype>
142 const arma::uword XRows,
const arma::uword XCols,
const arma::uword XDepth,
143 const arma::uword KRows,
const arma::uword KCols,
144 const arma::uword SRows,
const arma::uword SCols)
146 const arma::uword XSize = XRows*XCols;
147 const arma::uword XC = XCols-KCols;
148 const arma::uword XR = XRows-KRows;
149 for (arma::uword x_col=0; x_col<=XC; x_col+=SCols)
151 for(arma::uword x_row=0; x_row<=XR; x_row+=SRows )
155 const arma::uword lay_offs =
layer*XSize;
156 for(arma::uword k_col=0; k_col<KCols; k_col++ )
158 const arma::uword col_offs = x_col+k_col;
159 const arma::uword l_c_offs = lay_offs+col_offs*XRows;
160 for(arma::uword k_row=0; k_row<KRows; k_row++ )
162 *(Dptr++)=Xptr[l_c_offs+ x_row + k_row];
182 template <
typename DNN_Dtype>
184 const arma::uword XRows,
const arma::uword XCols,
const arma::uword XDepth,
185 const arma::uword KRows,
const arma::uword KCols,
186 const arma::uword PRows,
const arma::uword PCols,
187 const arma::uword SRows,
const arma::uword SCols)
189 const arma::uword XSize = XRows * XCols;
190 const arma::sword XC = XCols - KCols + PCols;
191 const arma::sword XR = XRows - KRows + PRows;
192 for (arma::sword x_col = -1*PCols; x_col <= XC; x_col += SCols)
194 for (arma::sword x_row = -1*PRows; x_row <= XR; x_row += SRows)
198 const arma::uword lay_offs =
layer * XSize;
199 for (arma::uword k_col = 0; k_col < KCols; k_col++)
201 const arma::sword col_offs = x_col + k_col;
202 const bool c_ok =
static_cast<unsigned int>(col_offs) < XCols;
205 const arma::uword l_c_offs = lay_offs + col_offs * XRows;
206 for (arma::uword k_row = 0; k_row < KRows; k_row++)
208 const arma::sword row_offs = x_row + k_row;
209 const bool r_ok =
static_cast<unsigned int>(row_offs) < XRows;
212 *(Dptr++) = Xptr[l_c_offs + row_offs];
222 for (arma::uword k_row = 0; k_row < KRows; k_row++)
245 template <
typename DNN_Dtype>
247 const unsigned int XRows,
const unsigned int XCols,
const unsigned int XDepth,
248 const unsigned int KRows,
const unsigned int KCols,
249 const unsigned int PRows,
const unsigned int PCols,
250 const unsigned int SRows,
const unsigned int SCols)
252 const unsigned int XSize = XRows*XCols;
253 const int XC = XCols-KCols+PCols;
254 const int XR = XRows-KRows+PRows;
257 const unsigned int lay_offs =
layer*XSize;
258 for(
unsigned int k_col=0; k_col<KCols; k_col++ )
260 for(
unsigned int k_row=0; k_row<KRows; k_row++ )
262 for (
int x_col=-PCols; x_col<=XC; x_col+=SCols)
264 const int col_offs = x_col+k_col;
265 const int l_c_offs = lay_offs+col_offs*XRows;
266 for(
int x_row=-PRows; x_row<=XR; x_row+=SRows )
268 const int row_offs = x_row+k_row;
269 if((row_offs) >= 0 && static_cast<unsigned int>(row_offs) < XRows &&
270 (col_offs) >= 0 && static_cast<unsigned int>(col_offs) < XCols)
272 *(Dptr++)=Xptr[l_c_offs+row_offs];
294 std::cout <<
"Padding: " << N_pad << std::endl;
295 std::cout <<
"Stride: " << N_stride << std::endl;
321 im2col(C.slice_memptr(0),
left->
get_Y1_memptr(),
N_rows_left,
N_cols_left,
N_channels_left,
N_filter_size,
N_filter_size,
N_pad,
N_pad,
N_stride,
N_stride);
327 yy = ((C.slice(0)).t())*gg;
329 Y1 = arma::vectorise(yy);
342 for (arma::uword n=0; n<
N_batch; n++)
346 im2col(C.slice_memptr(n),
left->
get_Y_colptr(n),
N_rows_left,
N_cols_left,
N_channels_left,
N_filter_size,
N_filter_size,
N_pad,
N_pad,
N_stride,
N_stride);
352 yy=((C.slice(n)).t())*gg;
354 Y.col(n) = arma::vectorise(yy);
371 for (arma::uword n=0; n<
N_batch; n++)
373 im2col(C_ptr,
right->
get_Dleft_colptr(n),
N_rows_right,
N_cols_right,
N_channels_right,
N_filter_size,
N_filter_size, N_filter_size - 1, N_filter_size - 1,
N_stride,
N_stride);
375 Dleft.col(n) = arma::vectorise(ii);
392 for (arma::uword n=0; n<
N_batch; n++)
395 dG += C.slice(n) * d_o;
401 arma::Cube<DNN_Dtype> g(
W.memptr(),
W.n_elem,1,1,
false,
true);
402 arma::Cube<DNN_Dtype> dg(dG.memptr(),dG.n_elem,1,1,
false,
true);
410 arma::Mat<DNN_Dtype> m={1.0};
425 arma::uword R =
W.n_rows;
426 arma::uword C =
W.n_cols;
427 arma::uword M =
W.n_slices;
428 arma::uword IC =
static_cast<arma::uword
>(ceil(M/(1.0*IR)));
429 arma::Mat<DNN_Dtype> II(IR*R,IC*C,arma::fill::zeros);
431 for(arma::uword m=0; m<M; m++)
433 arma::Mat<DNN_Dtype> I =
W.slice(m);
435 II(r,c,arma::size(R,C)) = I;
447 void plotWeights(sp::gplot& gp,
const arma::uword PR)
449 std::ostringstream tmp_s;
450 arma::uword M =
W.n_slices;
451 arma::uword PC = ceil(M/(1.0*PR));
453 gp.send2gp(
"unset key");
454 gp.send2gp(
"set format xy \"\" ");
455 DNN_Dtype p=arma::max(arma::abs(arma::vectorise(
W)));
456 tmp_s <<
"set cbrange [ " << -p <<
":" << p <<
"]";
457 gp.send2gp(tmp_s.str().c_str());
461 gp.send2gp(
"unset colorbox");
462 tmp_s <<
"set multiplot layout " << PR <<
"," << PC <<
" margins 0.01,0.99,0.01,0.99 spacing 0.01,0.01";
466 tmp_s <<
"set multiplot layout " << PR <<
"," << PC;
468 gp.send2gp(tmp_s.str().c_str());
470 for(arma::uword m=0; m<M; m++)
473 gp.image(arma::conv_to<arma::mat>::from(Im));
475 gp.send2gp(
"unset multiplot");
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].
void prop(void)
Forward propagation though layer.
arma::uword N_rows_left
Input rows.
arma::uword get_nrof_params(void)
Get info about number of trainable parameters in layer.
layer * right
Pointer to next layer.
arma::Mat< DNN_Dtype > Cbp
Input expanded by im2col.
arma::uword N_channels_right
Output channels, number of filters.
void backprop(void)
Backpropagation of mini batch propagation though layer.
arma::uword N_stride
Stride.
arma::Mat< DNN_Dtype > Dleft
Error buffer [N_left,N_batch].
arma::uword N_cols_left
Input cols.
arma::Cube< DNN_Dtype > C
Input expanded by im2col.
arma::Mat< DNN_Dtype > Y1
Output buffer [N_right,1].
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 void init(void)
Initialize layer.
arma::Mat< DNN_Dtype > B
Bias.
arma::uword N_pad
Padding.
void prop_mb(void)
Forward mini batch propagation though layer.
virtual void disp(void)
Display info about layer.
float DNN_Dtype
Data type used in the network (float or double)
void im2col(DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const arma::uword XRows, const arma::uword XCols, const arma::uword XDepth, const arma::uword KRows, const arma::uword KCols, const arma::uword PRows, const arma::uword PCols, const arma::uword SRows, const arma::uword SCols)
Im2col with zero padding.
arma::uword N_rows_right
Output rows.
virtual void upd_buf_size(arma::uword nmb)
Update layer buffer sizes.
void disp(void)
Display info about layer.
virtual void apply(arma::Cube< DNN_Dtype > &W, arma::Mat< DNN_Dtype > &B, const arma::Cube< DNN_Dtype > &Wgrad, const arma::Mat< DNN_Dtype > &Bgrad)=0
Apply the optimizer to the layer parameters.
arma::Mat< DNN_Dtype > weights2img(arma::uword IR)
Generate an image of the weights.
arma::Cube< DNN_Dtype > W
Weights.
arma::uword N_batch
Mini batch size.
bool train_par
Enable training.
void upd_buf_size(arma::uword nmb)
Updates the buffer sizes.
arma::Cube< DNN_Dtype > rotate_180(const arma::Cube< DNN_Dtype > &C)
Rotates each matrix in a cube 180 degree.
arma::uword N_filter_size
Conv. filter size.
arma::Mat< DNN_Dtype > rotate_180(const arma::Mat< DNN_Dtype > &M)
Rotates a matrix 180 degree.
void im2row(DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const unsigned int XRows, const unsigned int XCols, const unsigned int XDepth, const unsigned int KRows, const unsigned int KCols, const unsigned int PRows, const unsigned int PCols, const unsigned int SRows, const unsigned int SCols)
Im2row with zero padding.
arma::uword N_channels_left
Input channels, number of filters.
virtual std::string get_algorithm(void)
Get the optimizer algorithm information.
layer_conv(const arma::uword n_channels, const arma::uword n_kernel, const arma::uword n_pad=0, const arma::uword n_stride=1)
Convolution layer constructor.
arma::uword N_right
Total size right.
void im2col_nzp(DNN_Dtype *Dptr, const DNN_Dtype *Xptr, const arma::uword XRows, const arma::uword XCols, const arma::uword XDepth, const arma::uword KRows, const arma::uword KCols, const arma::uword SRows, const arma::uword SCols)
Im2col without zero padding.
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.
void init(void)
Initialization of layer.
A convolution layer class.
virtual DNN_Dtype * get_Dleft_colptr(const arma::uword n)
Get error buffer memory column pointer - mini batch.
bool add_bias
Enable bias.
void update(void)
Updates the trainable parameters.