TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
machine_learning::neural_network::layers::DenseLayer Class Reference
Collaboration diagram for machine_learning::neural_network::layers::DenseLayer:
[legend]

Public Member Functions

 DenseLayer (const int &neurons, const std::string &activation, const std::pair< size_t, size_t > &kernel_shape, const bool &random_kernel)
 
 DenseLayer (const int &neurons, const std::string &activation, const std::vector< std::valarray< double > > &kernel)
 
 DenseLayer (const DenseLayer &layer)=default
 
 ~DenseLayer ()=default
 
DenseLayeroperator= (const DenseLayer &layer)=default
 
 DenseLayer (DenseLayer &&)=default
 
DenseLayeroperator= (DenseLayer &&)=default
 

Public Attributes

double(* activation_function )(const double &)
 
double(* dactivation_function )(const double &)
 
int neurons
 
std::string activation
 
std::vector< std::valarray< double > > kernel
 

Detailed Description

neural_network::layers::DenseLayer class is used to store all necessary information about the layers (i.e. neurons, activation and kernel). This class is used by NeuralNetwork class to store layers.

Definition at line 125 of file neural_network.cpp.

Constructor & Destructor Documentation

◆ DenseLayer() [1/4]

machine_learning::neural_network::layers::DenseLayer::DenseLayer ( const int & neurons,
const std::string & activation,
const std::pair< size_t, size_t > & kernel_shape,
const bool & random_kernel )
inline

Constructor for neural_network::layers::DenseLayer class

Parameters
neuronsnumber of neurons
activationactivation function for layer
kernel_shapeshape of kernel
random_kernelflag for whether to initialize kernel randomly

Definition at line 141 of file neural_network.cpp.

143 {
144 // Choosing activation (and it's derivative)
145 if (activation == "sigmoid") {
146 activation_function = neural_network::activations::sigmoid;
147 dactivation_function = neural_network::activations::sigmoid;
148 } else if (activation == "relu") {
149 activation_function = neural_network::activations::relu;
150 dactivation_function = neural_network::activations::drelu;
151 } else if (activation == "tanh") {
152 activation_function = neural_network::activations::tanh;
153 dactivation_function = neural_network::activations::dtanh;
154 } else if (activation == "none") {
155 // Set identity function in casse of none is supplied
156 activation_function =
157 neural_network::util_functions::identity_function;
158 dactivation_function =
159 neural_network::util_functions::identity_function;
160 } else {
161 // If supplied activation is invalid
162 std::cerr << "ERROR (" << __func__ << ") : ";
163 std::cerr << "Invalid argument. Expected {none, sigmoid, relu, "
164 "tanh} got ";
165 std::cerr << activation << std::endl;
166 std::exit(EXIT_FAILURE);
167 }
168 this->activation = activation; // Setting activation name
169 this->neurons = neurons; // Setting number of neurons
170 // Initialize kernel according to flag
171 if (random_kernel) {
172 uniform_random_initialization(kernel, kernel_shape, -1.0, 1.0);
173 } else {
174 unit_matrix_initialization(kernel, kernel_shape);
175 }
176 }
void unit_matrix_initialization(std::vector< std::valarray< T > > &A, const std::pair< size_t, size_t > &shape)
void uniform_random_initialization(std::vector< std::valarray< T > > &A, const std::pair< size_t, size_t > &shape, const T &low, const T &high)

◆ DenseLayer() [2/4]

machine_learning::neural_network::layers::DenseLayer::DenseLayer ( const int & neurons,
const std::string & activation,
const std::vector< std::valarray< double > > & kernel )
inline

Constructor for neural_network::layers::DenseLayer class

Parameters
neuronsnumber of neurons
activationactivation function for layer
kernelvalues of kernel (useful in loading model)

Definition at line 183 of file neural_network.cpp.

184 {
185 // Choosing activation (and it's derivative)
186 if (activation == "sigmoid") {
187 activation_function = neural_network::activations::sigmoid;
188 dactivation_function = neural_network::activations::sigmoid;
189 } else if (activation == "relu") {
190 activation_function = neural_network::activations::relu;
191 dactivation_function = neural_network::activations::drelu;
192 } else if (activation == "tanh") {
193 activation_function = neural_network::activations::tanh;
194 dactivation_function = neural_network::activations::dtanh;
195 } else if (activation == "none") {
196 // Set identity function in casse of none is supplied
197 activation_function =
198 neural_network::util_functions::identity_function;
199 dactivation_function =
200 neural_network::util_functions::identity_function;
201 } else {
202 // If supplied activation is invalid
203 std::cerr << "ERROR (" << __func__ << ") : ";
204 std::cerr << "Invalid argument. Expected {none, sigmoid, relu, "
205 "tanh} got ";
206 std::cerr << activation << std::endl;
207 std::exit(EXIT_FAILURE);
208 }
209 this->activation = activation; // Setting activation name
210 this->neurons = neurons; // Setting number of neurons
211 this->kernel = kernel; // Setting supplied kernel values
212 }

◆ DenseLayer() [3/4]

machine_learning::neural_network::layers::DenseLayer::DenseLayer ( const DenseLayer & layer)
default

Copy Constructor for class DenseLayer.

Parameters
modelinstance of class to be copied.

◆ ~DenseLayer()

machine_learning::neural_network::layers::DenseLayer::~DenseLayer ( )
default

Destructor for class DenseLayer.

◆ DenseLayer() [4/4]

machine_learning::neural_network::layers::DenseLayer::DenseLayer ( DenseLayer && )
default

Move constructor for class DenseLayer

Member Function Documentation

◆ operator=() [1/2]

DenseLayer & machine_learning::neural_network::layers::DenseLayer::operator= ( const DenseLayer & layer)
default

Copy assignment operator for class DenseLayer

◆ operator=() [2/2]

DenseLayer & machine_learning::neural_network::layers::DenseLayer::operator= ( DenseLayer && )
default

Move assignment operator for class DenseLayer

Member Data Documentation

◆ activation

std::string machine_learning::neural_network::layers::DenseLayer::activation

Definition at line 131 of file neural_network.cpp.

◆ activation_function

double(* machine_learning::neural_network::layers::DenseLayer::activation_function) (const double &)

Definition at line 128 of file neural_network.cpp.

◆ dactivation_function

double(* machine_learning::neural_network::layers::DenseLayer::dactivation_function) (const double &)

Definition at line 129 of file neural_network.cpp.

◆ kernel

std::vector<std::valarray<double> > machine_learning::neural_network::layers::DenseLayer::kernel

Definition at line 132 of file neural_network.cpp.

◆ neurons

int machine_learning::neural_network::layers::DenseLayer::neurons

Definition at line 130 of file neural_network.cpp.


The documentation for this class was generated from the following file: