23 #ifndef SRC_MLNN_LAYER_HPP_
24 #define SRC_MLNN_LAYER_HPP_
29 #include<types/MatrixTypes.hpp>
30 #include<types/MatrixArray.hpp>
34 #include <boost/serialization/serialization.hpp>
36 #include <boost/serialization/vector.hpp>
38 #include <boost/serialization/array.hpp>
39 #include <boost/serialization/version.hpp>
41 #include <logger/Log.hpp>
45 namespace serialization {
85 template <
typename eT>
93 template <
typename eT=
float>
107 Layer(
size_t input_height_,
size_t input_width_,
size_t input_depth_,
108 size_t output_height_,
size_t output_width_,
size_t output_depth_,
109 LayerTypes layer_type_, std::string name_ =
"layer") :
160 virtual void forward(
bool test =
false) = 0;
165 mic::types::MatrixPtr<eT>
forward(mic::types::MatrixPtr<eT> x_,
bool test =
false) {
184 mic::types::MatrixPtr<eT>
backward(mic::types::MatrixPtr<eT> dy_) {
203 s[
"x"]->resize(
s[
"x"]->rows(), batch_size_);
204 g[
"x"]->resize(
g[
"x"]->rows(), batch_size_);
206 s[
"y"]->resize(
s[
"y"]->rows(), batch_size_);
207 g[
"y"]->resize(
g[
"y"]->rows(), batch_size_);
218 template<
typename loss>
219 mic::types::MatrixPtr<eT>
calculateNumericalGradient(mic::types::MatrixPtr<eT> x_, mic::types::MatrixPtr<eT> target_y_, mic::types::MatrixPtr<eT> param_, loss loss_, eT delta_) {
221 mic::types::MatrixPtr<eT> nGrad = MAKE_MATRIX_PTR(eT, param_->rows(), param_->cols());
222 for (
size_t i=0; i<(size_t)param_->size(); i++) {
224 (*param_)[i] += delta_;
226 eT
p = loss_.calculateLoss(target_y_,
forward(x_));
228 (*param_)[i] -= 2*delta_;
230 eT
m = loss_.calculateLoss(target_y_,
forward(x_));
233 (*nGrad)[i] = (p-
m)/(2*delta_);
235 (*param_)[i] += delta_;
252 virtual void update(eT alpha_, eT decay_ = 0.0f) = 0;
270 inline const std::string
name()
const {
277 mic::types::MatrixPtr<eT>
getParam(std::string name_) {
284 mic::types::MatrixPtr<eT>
getState(std::string name_) {
298 void setState(std::string name_, mic::types::MatrixPtr<eT> mat_ptr_) {
299 (*
s[name_]) = (*mat_ptr_);
306 template<
typename omT>
312 for (
auto& i:
p.keys()) {
315 std::make_shared< omT > (omT ( (
p[i.second])->rows(), (
p[i.second])->cols() ))
323 const std::string
type()
const {
334 return "Convolution";
346 return "SparseLinear";
348 return "HebbianLinear";
350 return "BinaryCorrelator";
364 std::ostringstream os_;
370 os_<<
" * input_width = " <<
input_width <<std::endl;
371 os_<<
" * input_channels = " <<
input_depth <<std::endl;
390 os_ <<
" [" << obj_.
s.name() <<
"]:\n";
391 for (
auto& i: obj_.
s.keys()) {
393 os_ <<
" [" << i.first <<
"]: ";
394 os_ << (obj_.
s[i.second])->cols() <<
"x" << (obj_.
s[i.second])->rows() << std::endl;
398 os_ <<
" [" << obj_.
g.name() <<
"]:\n";
399 for (
auto& i: obj_.
g.keys()) {
401 os_ <<
" [" << i.first <<
"]: ";
402 os_ << (obj_.
g[i.second])->cols() <<
"x" << (obj_.
g[i.second])->rows() << std::endl;
406 os_ <<
" [" << obj_.
p.name() <<
"]:\n";
407 for (
auto& i: obj_.
p.keys()) {
409 os_ <<
" [" << i.first <<
"]: ";
410 os_ << (obj_.
p[i.second])->cols() <<
"x" << (obj_.
p[i.second])->rows() << std::endl;
414 os_ <<
" [" << obj_.
m.name() <<
"]:\n";
415 for (
auto& i: obj_.
m.keys()) {
417 os_ <<
" [" << i.first <<
"]: ";
418 os_ << (obj_.
m[i.second])->cols() <<
"x" << (obj_.
m[i.second])->rows() << std::endl;
433 mic::types::MatrixPtr<eT>
lazyReturnSampleFromBatch (mic::types::MatrixPtr<eT> batch_ptr_, mic::types::MatrixArray<eT> & array_, std::string id_,
size_t sample_number_,
size_t sample_size_){
435 std::string sample_id = id_ + std::to_string(sample_number_);
436 mic::types::MatrixPtr<eT> sample;
440 if (!array_.keyExists(sample_id)) {
442 array_.add(sample_id, sample_size_, 1);
446 sample =
m[sample_id];
448 (*sample) = batch_ptr_->col(sample_number_);
460 inline mic::types::MatrixPtr<eT>
lazyReturnInputSample (mic::types::MatrixPtr<eT> batch_ptr_,
size_t sample_number_){
487 mic::types::MatrixPtr<eT>
lazyReturnChannelFromSample (mic::types::MatrixPtr<eT> sample_ptr_, mic::types::MatrixArray<eT> & array_, std::string id_,
size_t sample_number_,
size_t channel_number_,
size_t height_,
size_t width_){
489 std::string channel_id = id_ + std::to_string(channel_number_);
490 mic::types::MatrixPtr<eT> channel;
494 if (!array_.keyExists(channel_id)) {
496 array_.add(channel_id, height_*width_, 1);
500 channel =
m[channel_id];
502 sample_ptr_->resize(sample_ptr_->size(), 1);
504 (*channel) = sample_ptr_->block(channel_number_*height_*width_, 0, height_*width_, 1);
506 channel-> resize(height_, width_);
519 inline mic::types::MatrixPtr<eT>
lazyReturnInputChannel (mic::types::MatrixPtr<eT> sample_ptr_,
size_t sample_number_,
size_t channel_number_){
530 inline mic::types::MatrixPtr<eT>
lazyReturnOutputChannel (mic::types::MatrixPtr<eT> sample_ptr_,
size_t sample_number_,
size_t channel_number_){
543 void lazyAllocateMatrixVector(std::vector< std::shared_ptr <mic::types::Matrix<eT> > > & vector_,
size_t vector_size_,
size_t matrix_height_,
size_t matrix_width_) {
545 if (vector_.size() != vector_size_) {
549 for (
size_t i=0; i < vector_size_; i++) {
551 mic::types::MatrixPtr<eT>
m = MAKE_MATRIX_PTR(eT, matrix_height_, matrix_width_);
552 vector_.push_back(m);
592 mic::types::MatrixPtr<eT> batch_x =
s[
'x'];
598 mic::types::MatrixPtr<eT> sample_x =
m[
"xs"];
599 (*sample_x) = batch_x->col(ib);
604 mic::types::MatrixPtr<eT> row =
x_activations[ib*input_depth + ic];
627 mic::types::MatrixPtr<eT> batch_dx =
g[
'x'];
633 mic::types::MatrixPtr<eT> sample_dx =
m[
"xs"];
634 (*sample_dx) = batch_dx->col(ib);
639 mic::types::MatrixPtr<eT> row =
dx_activations[ib*input_depth + ic];
662 mic::types::MatrixPtr<eT> batch_y =
s[
'y'];
668 mic::types::MatrixPtr<eT> sample_y =
m[
"ys"];
669 (*sample_y) = batch_y->col(ib);
674 mic::types::MatrixPtr<eT> row =
y_activations[ib*output_depth + oc];
697 mic::types::MatrixPtr<eT> batch_dy =
g[
'y'];
703 mic::types::MatrixPtr<eT> sample_dy =
m[
"ys"];
704 (*sample_dy) = batch_dy->col(ib);
709 mic::types::MatrixPtr<eT> row =
dy_activations[ib*output_depth + oc];
753 mic::types::MatrixArray<eT>
s;
756 mic::types::MatrixArray<eT>
g;
759 mic::types::MatrixArray<eT>
p;
762 mic::types::MatrixArray<eT>
m;
792 friend class boost::serialization::access;
799 template<
class Archive>
800 void serialize(Archive & ar,
const unsigned int version) {
829 BOOST_CLASS_VERSION(mic::mlnn::Layer<
double>, 2)
virtual std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getInputGradientActivations()
Class implementing the layer with Exponential Linear Unit (ELU). http://arxiv.org/pdf/1511.07289v5.pdf.
size_t inputSize()
Returns size (length) of inputs.
mic::types::MatrixPtr< eT > lazyReturnInputChannel(mic::types::MatrixPtr< eT > sample_ptr_, size_t sample_number_, size_t channel_number_)
std::string layer_name
Name (identifier of the type) of the layer.
size_t input_depth
Number of channels of the input (e.g. 3 for RGB images).
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > y_activations
Vector containing activations of output neurons - used in visualization.
size_t batch_size
Size (length) of (mini)batch.
void add(std::string name_, size_t rows_, size_t cols_)
size_t batchSize()
Returns size (length) of (mini)batch.
size_t outputSize()
Returns size (length) of outputs.
const std::string type() const
void serialize(Archive &ar, const unsigned int version)
Class representing a multi-layer neural network based on backpropagation/gradient descent...
mic::types::MatrixPtr< eT > calculateNumericalGradient(mic::types::MatrixPtr< eT > x_, mic::types::MatrixPtr< eT > target_y_, mic::types::MatrixPtr< eT > param_, loss loss_, eT delta_)
virtual void backward()=0
mic::types::MatrixPtr< eT > lazyReturnOutputChannel(mic::types::MatrixPtr< eT > sample_ptr_, size_t sample_number_, size_t channel_number_)
mic::types::MatrixPtr< eT > backward(mic::types::MatrixPtr< eT > dy_)
const std::string name() const
Returns name of the layer.
LayerTypes layer_type
Type of the layer.
mic::types::MatrixPtr< eT > lazyReturnSampleFromBatch(mic::types::MatrixPtr< eT > batch_ptr_, mic::types::MatrixArray< eT > &array_, std::string id_, size_t sample_number_, size_t sample_size_)
virtual void resizeBatch(size_t batch_size_)
mic::types::MatrixPtr< eT > getGradient(std::string name_)
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_)
Class representing a multi-layer neural network.
size_t output_depth
Number of filters = number of output channels.
mic::types::MatrixPtr< eT > lazyReturnOutputSample(mic::types::MatrixPtr< eT > batch_ptr_, size_t sample_number_)
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > x_activations
Vector containing activations of input neurons - used in visualization.
virtual void update(eT alpha_, eT decay_=0.0f)=0
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > dx_activations
Vector containing activations of gradients of inputs (dx) - used in visualization.
mic::neural_nets::optimization::OptimizationArray< eT > opt
Array of optimization functions.
LayerTypes
Enumeration of possible layer types.
virtual void resetGrads()
virtual void forward(bool test=false)=0
mic::types::MatrixPtr< eT > getState(std::string name_)
mic::types::MatrixPtr< eT > forward(mic::types::MatrixPtr< eT > x_, bool test=false)
void setState(std::string name_, mic::types::MatrixPtr< eT > mat_ptr_)
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.
mic::types::MatrixPtr< eT > getParam(std::string name_)
mic::types::MatrixPtr< eT > lazyReturnInputSample(mic::types::MatrixPtr< eT > batch_ptr_, size_t sample_number_)
size_t input_width
Width of the input (e.g. 28 for MNIST).
virtual std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getInputActivations()
size_t output_height
Number of receptive fields in a single channel - vertical direction.
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > dy_activations
Vector containing activations of gradients of outputs (dy) - used in visualization.
Class representing a multi-layer neural network based on hebbian learning.
Layer(size_t input_height_, size_t input_width_, size_t input_depth_, size_t output_height_, size_t output_width_, size_t output_depth_, LayerTypes layer_type_, std::string name_="layer")
size_t output_width
Number of receptive fields in a single channel - horizontal direction.
friend std::ostream & operator<<(std::ostream &os_, Layer &obj_)
virtual std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getOutputActivations()
virtual std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getOutputGradientActivations()
virtual std::string streamLayerParameters()
mic::types::MatrixArray< eT > m
Memory - a list of temporal parameters, to be used by the derived classes.
mic::types::MatrixPtr< eT > lazyReturnChannelFromSample(mic::types::MatrixPtr< eT > sample_ptr_, mic::types::MatrixArray< eT > &array_, std::string id_, size_t sample_number_, size_t channel_number_, size_t height_, size_t width_)
mic::types::MatrixArray< eT > p
Parameters - parameters of the layer, to be used by the derived classes.