TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
neural_network.cpp File Reference

Implementation of [Multilayer Perceptron] (https://en.wikipedia.org/wiki/Multilayer_perceptron). More...

#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <valarray>
#include <vector>
#include "vector_ops.hpp"
Include dependency graph for neural_network.cpp:

Go to the source code of this file.

Classes

class  machine_learning::neural_network::layers::DenseLayer
 
class  machine_learning::neural_network::NeuralNetwork
 

Namespaces

namespace  machine_learning
 A* search algorithm
 
namespace  neural_network
 Neural Network or Multilayer Perceptron.
 
namespace  activations
 Various activation functions used in Neural network.
 
namespace  util_functions
 Various utility functions used in Neural network.
 
namespace  layers
 This namespace contains layers used in MLP.
 

Functions

double machine_learning::neural_network::activations::sigmoid (const double &x)
 
double machine_learning::neural_network::activations::dsigmoid (const double &x)
 
double machine_learning::neural_network::activations::relu (const double &x)
 
double machine_learning::neural_network::activations::drelu (const double &x)
 
double machine_learning::neural_network::activations::tanh (const double &x)
 
double machine_learning::neural_network::activations::dtanh (const double &x)
 
double machine_learning::neural_network::util_functions::square (const double &x)
 
double machine_learning::neural_network::util_functions::identity_function (const double &x)
 
static void test ()
 
int main ()
 Main function.
 

Detailed Description

Implementation of [Multilayer Perceptron] (https://en.wikipedia.org/wiki/Multilayer_perceptron).

Author
Deep Raval

A multilayer perceptron (MLP) is a class of feedforward artificial neural network (ANN). The term MLP is used ambiguously, sometimes loosely to any feedforward ANN, sometimes strictly to refer to networks composed of multiple layers of perceptrons (with threshold activation). Multilayer perceptrons are sometimes colloquially referred to as "vanilla" neural networks, especially when they have a single hidden layer.

An MLP consists of at least three layers of nodes: an input layer, a hidden layer and an output layer. Except for the input nodes, each node is a neuron that uses a nonlinear activation function. MLP utilizes a supervised learning technique called backpropagation for training. Its multiple layers and non-linear activation distinguish MLP from a linear perceptron. It can distinguish data that is not linearly separable.

See Backpropagation for training algorithm.

Note
This implementation uses mini-batch gradient descent as optimizer and MSE as loss function. Bias is also not included.

Definition in file neural_network.cpp.

Function Documentation

◆ drelu()

double machine_learning::neural_network::activations::drelu ( const double & x)

Derivative of relu function

Parameters
XValue
Returns
derivative of relu(x)

Definition at line 81 of file neural_network.cpp.

81{ return x >= 0.0 ? 1.0 : 0.0; }

◆ dsigmoid()

double machine_learning::neural_network::activations::dsigmoid ( const double & x)

Derivative of sigmoid function

Parameters
XValue
Returns
Returns derivative of sigmoid(x)

Definition at line 67 of file neural_network.cpp.

67{ return x * (1 - x); }

◆ dtanh()

double machine_learning::neural_network::activations::dtanh ( const double & x)

Derivative of Sigmoid function

Parameters
XValue
Returns
Returns derivative of tanh(x)

Definition at line 95 of file neural_network.cpp.

95{ return 1 - x * x; }

◆ identity_function()

double machine_learning::neural_network::util_functions::identity_function ( const double & x)

Identity function

Parameters
XValue
Returns
Returns x

Definition at line 112 of file neural_network.cpp.

112{ return x; }

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 833 of file neural_network.cpp.

833 {
834 // Testing
835 test();
836 return 0;
837}
static void test()

◆ relu()

double machine_learning::neural_network::activations::relu ( const double & x)

Relu function

Parameters
XValue
Returns
relu(x)

Definition at line 74 of file neural_network.cpp.

74{ return std::max(0.0, x); }

◆ sigmoid()

double machine_learning::neural_network::activations::sigmoid ( const double & x)

Sigmoid function

Parameters
XValue
Returns
Returns sigmoid(x)

Definition at line 60 of file neural_network.cpp.

60{ return 1.0 / (1.0 + std::exp(-x)); }

◆ square()

double machine_learning::neural_network::util_functions::square ( const double & x)

Square function

Parameters
XValue
Returns
Returns x * x

Definition at line 106 of file neural_network.cpp.

106{ return x * x; }

◆ tanh()

double machine_learning::neural_network::activations::tanh ( const double & x)

Tanh function

Parameters
XValue
Returns
Returns tanh(x)

Definition at line 88 of file neural_network.cpp.

88{ return 2 / (1 + std::exp(-2 * x)) - 1; }

◆ test()

static void test ( )
static

Function to test neural network

Returns
none

Definition at line 805 of file neural_network.cpp.

805 {
806 // Creating network with 3 layers for "iris.csv"
809 {4, "none"}, // First layer with 3 neurons and "none" as activation
810 {6,
811 "relu"}, // Second layer with 6 neurons and "relu" as activation
812 {3, "sigmoid"} // Third layer with 3 neurons and "sigmoid" as
813 // activation
814 });
815 // Printing summary of model
816 myNN.summary();
817 // Training Model
818 myNN.fit_from_csv("iris.csv", true, 100, 0.3, false, 2, 32, true);
819 // Testing predictions of model
821 myNN.single_predict({{5, 3.4, 1.6, 0.4}})) == 0);
823 myNN.single_predict({{6.4, 2.9, 4.3, 1.3}})) == 1);
825 myNN.single_predict({{6.2, 3.4, 5.4, 2.3}})) == 2);
826 return;
827}
std::vector< std::valarray< double > > single_predict(const std::vector< std::valarray< double > > &X)
void fit_from_csv(const std::string &file_name, const bool &last_label, const int &epochs, const double &learning_rate, const bool &normalize, const int &slip_lines=1, const size_t &batch_size=32, const bool &shuffle=true)
size_t argmax(const std::vector< std::valarray< T > > &A)