23 #ifndef SRC_MLNN_LINEAR_HPP_
24 #define SRC_MLNN_LINEAR_HPP_
30 namespace fully_connected {
33 template <
typename eT>
41 template <
typename eT=
float>
51 Linear(
size_t inputs_,
size_t outputs_, std::string name_ =
"Linear") :
52 Linear(inputs_, 1, 1, outputs_, 1, 1, name_)
68 Linear(
size_t input_height_,
size_t input_width_,
size_t input_depth_,
69 size_t output_height_,
size_t output_width_,
size_t output_depth_,
70 std::string name_ =
"Linear") :
71 Layer<eT>::
Layer(input_height_, input_width_, input_depth_,
72 output_height_, output_width_, output_depth_,
92 Layer<eT>::template setOptimization<mic::neural_nets::optimization::GradientDescent<eT> > ();
107 mic::types::MatrixPtr<eT> x =
s[
'x'];
108 mic::types::MatrixPtr<eT> W =
p[
'W'];
109 mic::types::MatrixPtr<eT> b =
p[
'b'];
111 mic::types::MatrixPtr<eT> y =
s[
'y'];
114 (*y) = (*W) * (*x) + (*b).replicate(1, (*x).cols());
128 mic::types::MatrixPtr<eT> dy =
g[
'y'];
129 mic::types::MatrixPtr<eT> x =
s[
'x'];
130 mic::types::MatrixPtr<eT> W =
p[
'W'];
132 mic::types::MatrixPtr<eT> dW =
g[
'W'];
133 mic::types::MatrixPtr<eT> db =
g[
'b'];
134 mic::types::MatrixPtr<eT> dx =
g[
'x'];
137 (*dW) = (*dy) * (*x).transpose();
138 (*db) = (*dy).rowwise().sum();
139 (*dx) = (*W).transpose() * (*dy);
159 void update(eT alpha_, eT decay_ = 0.0f) {
163 opt[
"W"]->update(
p[
'W'],
g[
'W'], alpha_, decay_);
164 opt[
"b"]->update(
p[
'b'],
g[
'b'], alpha_, 0.0);
179 mic::types::MatrixPtr<eT> W =
p[
"W"];
202 mic::types::MatrixPtr<eT> dW =
g[
"W"];
226 mic::types::MatrixPtr<eT> W =
p[
"W"];
234 (*row) = W->block(i, j*input_depth, 1,
input_height*input_width);
254 mic::types::MatrixPtr<eT> batch_y =
s[
'y'];
256 mic::types::MatrixPtr<eT> W =
p[
"W"];
262 mic::types::MatrixPtr<eT> sample_y =
m[
"ys"];
263 (*sample_y) = batch_y->col(ib);
266 mic::types::MatrixPtr<eT> x_act =
m[
"xs"];
267 (*x_act) = W->transpose() * (*sample_y);
292 mic::types::MatrixPtr<eT> batch_x =
s[
'x'];
302 mic::types::MatrixPtr<eT> sample_x =
m[
"xs"];
303 (*sample_x) = batch_x->col(ib);
304 eT* sample_x_ptr = (*sample_x).data();
307 mic::types::MatrixPtr<eT> reconstructed_x = reconstructed_batch_x[ib];
308 eT* reconstructed_x_ptr = (*reconstructed_x).data();
312 error += fabs(sample_x_ptr[i] - reconstructed_x_ptr[i]);
316 return (error/batch_size);
Class representing a convolution layer, with "valid padding" and variable stride. ...
std::vector< mic::types::MatrixPtr< eT > > & getInverseWeightActivations()
std::vector< mic::types::MatrixPtr< eT > > & getWeightGradientActivations()
std::vector< mic::types::MatrixPtr< eT > > w_activations
Vector containing activations of weights/filters.
size_t input_depth
Number of channels of the input (e.g. 3 for RGB images).
std::vector< mic::types::MatrixPtr< eT > > dw_activations
Vector containing activations of gradients of weights (dW).
void update(eT alpha_, eT decay_=0.0f)
size_t batch_size
Size (length) of (mini)batch.
Linear(size_t input_height_, size_t input_width_, size_t input_depth_, size_t output_height_, size_t output_width_, size_t output_depth_, std::string name_="Linear")
eT calculateMeanReconstructionError()
std::vector< mic::types::MatrixPtr< eT > > & getInverseOutputActivations()
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.
Class implementing a linear, fully connected layer.
std::vector< mic::types::MatrixPtr< eT > > inverse_w_activations
Vector containing "inverse activations" of each neuron weights(W^T).
mic::neural_nets::optimization::OptimizationArray< eT > opt
Array of optimization functions.
LayerTypes
Enumeration of possible layer types.
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.
Linear(size_t inputs_, size_t outputs_, std::string name_="Linear")
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.
Class implementing a linear, fully connected layer with sparsity regulation.
size_t output_width
Number of receptive fields in a single channel - horizontal direction.
Contains a template class representing a layer.
void forward(bool test_=false)
mic::types::MatrixArray< eT > m
Memory - a list of temporal parameters, to be used by the derived classes.
std::vector< mic::types::MatrixPtr< eT > > & getWeightActivations()
std::vector< mic::types::MatrixPtr< eT > > inverse_y_activations
Vector containing activations of neurons (y*W^T).
mic::types::MatrixArray< eT > p
Parameters - parameters of the layer, to be used by the derived classes.