23 #ifndef SRC_MLNN_CONVOLUTION_HPP_
24 #define SRC_MLNN_CONVOLUTION_HPP_
28 #include<types/MatrixTypes.hpp>
29 #include<types/MatrixArray.hpp>
33 namespace convolution {
40 template <
typename eT=
float>
54 Convolution(
size_t input_height_,
size_t input_width_,
size_t input_channels_,
size_t number_of_filters_,
size_t filter_size_,
size_t stride_, std::string name_ =
"Convolution") :
55 Layer<eT>::
Layer(input_height_, input_width_, input_channels_,
57 1, 1, number_of_filters_,
66 while(height_rest >= (
int)
stride){
71 if (height_rest != 0) {
72 LOG(LERROR) <<
" Filter width and stride does not fit image height";
80 while(width_rest >= (
int)
stride){
85 if (height_rest != 0) {
86 LOG(LINFO) <<
"Filter height and stride does not fit image height";
103 eT range = sqrt(6.0 / range_init);
112 p[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)]->rand(-range, range);
116 g.add (
"W"+std::to_string(fi)+
"x"+std::to_string(ic), 1, filter_size*filter_size);
122 p.add (
"b", output_depth, 1);
125 g.add (
"b", output_depth, 1);
138 m.add (
"yc"+std::to_string(fi), output_height,
output_width);
145 m.add (
"ixrf"+std::to_string(ry)+
"x"+std::to_string(rx), output_height,
output_width);
150 m.add (
"fs", input_depth*output_depth, input_depth*output_depth);
153 Layer<eT>::template setOptimization<mic::neural_nets::optimization::GradientDescent<eT> > ();
166 std::ostringstream os_;
172 os_<<
" * input_width = " <<
input_width <<std::endl;
173 os_<<
" * input_channels = " <<
input_depth <<std::endl;
174 os_<<
" * filter_size = " <<
filter_size <<std::endl;
175 os_<<
" * stride = " <<
stride <<std::endl;
189 mic::types::MatrixPtr<eT> batch_x =
s[
'x'];
194 mic::types::MatrixPtr<eT> batch_y =
s[
'y'];
202 mic::types::MatrixPtr<eT> y_sample =
m[
"ys"];
207 mic::types::MatrixPtr<eT> y_channel =
m[
"yc"+std::to_string(fi)];
211 y_channel->setValue((*
p[
"b"])[fi]);
215 mic::types::MatrixPtr<eT> sample =
m[
"xs"];
216 (*sample) = batch_x->col(ib);
222 mic::types::MatrixPtr<eT> ichannel =
m[
"xc"];
238 mic::types::MatrixPtr<eT> field =
m[
"xrf"+std::to_string(ry)+
"x"+std::to_string(rx)];
252 mic::types::MatrixPtr<eT> y_channel =
m[
"yc"+std::to_string(fi)];
257 mic::types::MatrixPtr<eT> W =
p[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
264 mic::types::MatrixPtr<eT> xrf =
m[
"xrf"+std::to_string(ry)+
"x"+std::to_string(rx)];
270 (*y_channel)(ry, rx) += ((*W)*(*xrf))(0);
278 y_sample->block(fi*output_height*output_width, 0, output_height*output_width, 1)
285 batch_y->col(ib) = (*y_sample);
297 mic::types::MatrixPtr<eT> batch_dy =
g[
'y'];
337 mic::types::MatrixPtr<eT> batch_dy =
g[
'y'];
338 mic::types::MatrixPtr<eT> batch_x =
s[
'x'];
341 mic::types::MatrixPtr<eT> batch_dx =
g[
'x'];
352 mic::types::MatrixPtr<eT> gys =
m[
"ys"];
353 (*gys) = batch_dy->col(ib);
366 mic::types::MatrixPtr<eT> gxs =
m[
"xs"];
374 mic::types::MatrixPtr<eT> gxc =
m[
"xc"];
386 mic::types::MatrixPtr<eT> W =
p[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
390 mic::types::MatrixPtr<eT> gyc =
m[
"yc"];
406 size_t isby = oy *
stride;
407 size_t isbx = ox *
stride;
414 eT conv = (*W)(iy,ix)*(*gyc)(oy,ox);
440 batch_dx->col(ib) = (*gxs);
454 mic::types::MatrixPtr<eT> batch_dy =
g[
'y'];
455 mic::types::MatrixPtr<eT> batch_x =
s[
'x'];
462 g[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)]->setZero();
471 mic::types::MatrixPtr<eT> gys =
m[
"ys"];
472 (*gys) = batch_dy->col(ib);
475 mic::types::MatrixPtr<eT> xs =
m[
"xs"];
476 (*xs) = batch_x->col(ib);
482 mic::types::MatrixPtr<eT> x_channel =
m[
"xc"];
495 mic::types::MatrixPtr<eT> ixrf =
m[
"ixrf"+std::to_string(fy)+
"x"+std::to_string(fx)];
507 (*ixrf)(iy, ix) = (*x_channel)(fy+iy*
stride,fx+ix*
stride);
521 mic::types::MatrixPtr<eT> gyc =
m[
"yc"+std::to_string(fi)];
526 mic::types::MatrixPtr<eT> dW =
g[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
528 dW->resize(filter_size, filter_size);
534 mic::types::MatrixPtr<eT> ixrf =
m[
"ixrf"+std::to_string(ry)+
"x"+std::to_string(rx)];
540 (*dW)(ry, rx) += ((*ixrf)*(*gyc))(0);
560 mic::types::MatrixPtr<eT> db =
g[
'b'];
562 mic::types::MatrixPtr<eT> batch_dy =
g[
'y'];
569 (*db)[fi] = channel_bach_sum;
590 void update(eT alpha_, eT decay_ = 0.0f) {
592 std::map<std::string, size_t> keys =
p.keys();
594 for (
auto& i: keys) {
597 opt[i.first]->update(
p[i.first],
g[i.first], 1.0*alpha_, decay_);
624 mic::types::MatrixPtr<eT> W =
p[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
627 mic::types::MatrixPtr<eT> row =
w_activations[fi*input_depth + ic];
630 row->resize(filter_size, filter_size);
656 mic::types::MatrixPtr<eT> W =
g[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
659 mic::types::MatrixPtr<eT> row =
dw_activations[fi*input_depth + ic];
687 mic::types::MatrixPtr<eT> xrf =
m[
"xrf"+std::to_string(ry)+
"x"+std::to_string(rx)];
718 mic::types::MatrixPtr<eT> ixrf =
m[
"ixrf"+std::to_string(fy)+
"x"+std::to_string(fx)];
740 mic::types::MatrixPtr<eT> fs =
m[
"fs"];
749 mic::types::MatrixPtr<eT> iW =
p[
"W"+std::to_string(fi)+
"x"+std::to_string(ic)];
751 size_t i = fi*input_depth + ic;
753 for (
size_t fj=0; fj < fi; fj++) {
757 mic::types::MatrixPtr<eT> jW =
p[
"W"+std::to_string(fj)+
"x"+std::to_string(jc)];
759 size_t j = fj*input_depth + jc;
779 eT dot = 0.0, denom_a = 0.0, denom_b = 0.0 ;
780 for(
size_t i = 0; i < length_; ++i) {
782 denom_a += A[i] * A[i] ;
783 denom_b += B[i] * B[i] ;
785 return dot / (sqrt(denom_a) * sqrt(denom_b)) ;
void update(eT alpha_, eT decay_=0.0f)
Class representing a convolution layer, with "valid padding" and variable stride. ...
std::vector< mic::types::MatrixPtr< eT > > xrf_activations
Vector containing receptive fields.
size_t inputSize()
Returns size (length) of inputs.
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getWeightGradientActivations()
size_t input_depth
Number of channels of the input (e.g. 3 for RGB images).
void backpropagade_dy_to_dW()
size_t batch_size
Size (length) of (mini)batch.
void forward(bool test=false)
size_t outputSize()
Returns size (length) of outputs.
const std::string type() const
size_t stride
Stride (assuming equal vertical and horizontal strides).
size_t filter_size
Size of filters (assuming square filters). Filter_size^2 = length of the output vector.
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getWeightActivations()
std::vector< mic::types::MatrixPtr< eT > > dw_activations
Vector containing activations of gradients of weights (dW).
std::vector< mic::types::MatrixPtr< eT > > irf_activations
Vector containing inverse receptive fields.
std::vector< mic::types::MatrixPtr< eT > > w_activations
Vector containing activations of weights/filters.
mic::types::MatrixPtr< eT > getFilterSimilarityMatrix()
virtual std::string streamLayerParameters()
size_t input_height
Height of the input (e.g. 28 for MNIST).
void lazyAllocateMatrixVector(std::vector< std::shared_ptr< mic::types::Matrix< eT > > > &vector_, size_t vector_size_, size_t matrix_height_, size_t matrix_width_)
Convolution(size_t input_height_, size_t input_width_, size_t input_channels_, size_t number_of_filters_, size_t filter_size_, size_t stride_, std::string name_="Convolution")
Class representing a multi-layer neural network.
size_t output_depth
Number of filters = number of output channels.
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getInverseReceptiveFields()
mic::neural_nets::optimization::OptimizationArray< eT > opt
Array of optimization functions.
void backpropagade_dy_to_dx()
LayerTypes
Enumeration of possible layer types.
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getReceptiveFields()
void backpropagade_dy_to_db()
mic::types::MatrixArray< eT > s
States - contains input [x] and output [y] matrices.
mic::types::MatrixArray< eT > g
Gradients - contains input [x] and output [y] matrices.
size_t input_width
Width of the input (e.g. 28 for MNIST).
size_t output_height
Number of receptive fields in a single channel - vertical direction.
eT cosineSimilarity(eT *A, eT *B, size_t length_)
size_t output_width
Number of receptive fields in a single channel - horizontal direction.
Contains a template class representing a layer.
mic::types::MatrixArray< eT > m
Memory - a list of temporal parameters, to be used by the derived classes.
mic::types::MatrixArray< eT > p
Parameters - parameters of the layer, to be used by the derived classes.