machine_learning::neural_network::NeuralNetwork Class Reference
Collaboration diagram for machine_learning::neural_network::NeuralNetwork:
[legend]

## Public Member Functions

| NeuralNetwork ()=default
| NeuralNetwork (const std::vector< std::pair< int, std::string >> &config)
| NeuralNetwork (const NeuralNetwork &model)=default
| ~NeuralNetwork ()=default
NeuralNetwork & | operator= (const NeuralNetwork &model)=default
| NeuralNetwork (NeuralNetwork &&)=default
NeuralNetwork & | operator= (NeuralNetwork &&)=default
std::pair< std::vector< std::vector< std::valarray< double > > >, std::vector< std::vector< std::valarray< double > > > > | get_XY_from_csv (const std::string &file_name, const bool &last_label, const bool &normalize, const int &slip_lines=1)
std::vector< std::valarray< double > > | single_predict (const std::vector< std::valarray< double >> &X)
std::vector< std::vector< std::valarray< double > > > | batch_predict (const std::vector< std::vector< std::valarray< double >>> &X)
void | fit (const std::vector< std::vector< std::valarray< double >>> &X_, const std::vector< std::vector< std::valarray< double >>> &Y_, const int &epochs=100, const double &learning_rate=0.01, const size_t &batch_size=32, const bool &shuffle=true)
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)
void | evaluate (const std::vector< std::vector< std::valarray< double >>> &X, const std::vector< std::vector< std::valarray< double >>> &Y)
void | evaluate_from_csv (const std::string &file_name, const bool &last_label, const bool &normalize, const int &slip_lines=1)
void | save_model (const std::string &_file_name)
NeuralNetwork | load_model (const std::string &file_name)
void | summary ()

## Private Member Functions

| NeuralNetwork (const std::vector< std::pair< int, std::string >> &config, const std::vector< std::vector< std::valarray< double >>> &kernels)
std::vector< std::vector< std::valarray< double > > > | __detailed_single_prediction (const std::vector< std::valarray< double >> &X)

## Private Attributes

std::vector< neural_network::layers::DenseLayer > | layers

## Detailed Description

NeuralNetwork class is implements MLP. This class is used by actual user to create and train networks.

## Constructor & Destructor Documentation

## NeuralNetwork() [1/5]

machine_learning::neural_network::NeuralNetwork::NeuralNetwork | ( | const std::vector< std::pair< int, std::string >> & | config,
|  | const std::vector< std::vector< std::valarray< double >>> & | kernels
| ) |  |

inlineprivate

Private Constructor for class NeuralNetwork. This constructor is used internally to load model.

Parameters

  config | vector containing pair (neurons, activation)
  kernels | vector containing all pretrained kernels

{
// First layer should not have activation
if (config.begin()->second != "none") {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr
<< "First layer can't have activation other than none got "
<< config.begin()->second;
std::cerr << std::endl;
std::exit(EXIT_FAILURE);
}
// Network should have atleast two layers
if (config.size() <= 1) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Invalid size of network, ";
std::cerr << "Atleast two layers are required";
std::exit(EXIT_FAILURE);
}
// Reconstructing all pretrained layers
for (size_t i = 0; i < config.size(); i++) {
layers.emplace_back(neural_network::layers::DenseLayer(
config[i].first, config[i].second, kernels[i]));
}
std::cout << "INFO: Network constructed successfully" << std::endl;
}
std::cerr
std::vector::begin
T begin(T... args)
std::endl
T endl(T... args)
std::exit
T exit(T... args)
layers
This namespace contains layers used in MLP.
std::vector::size
T size(T... args)
Here is the call graph for this function:

## NeuralNetwork() [2/5]

machine_learning::neural_network::NeuralNetwork::NeuralNetwork | ( |  | ) |

default

Default Constructor for class NeuralNetwork. This constructor is used to create empty variable of type NeuralNetwork class.

## NeuralNetwork() [3/5]

machine_learning::neural_network::NeuralNetwork::NeuralNetwork | ( | const std::vector< std::pair< int, std::string >> & | config | ) |

inlineexplicit

Constructor for class NeuralNetwork. This constructor is used by user.

Parameters

  config | vector containing pair (neurons, activation)

{
// First layer should not have activation
if (config.begin()->second != "none") {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr
<< "First layer can't have activation other than none got "
<< config.begin()->second;
std::cerr << std::endl;
std::exit(EXIT_FAILURE);
}
// Network should have atleast two layers
if (config.size() <= 1) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Invalid size of network, ";
std::cerr << "Atleast two layers are required";
std::exit(EXIT_FAILURE);
}
// Separately creating first layer so it can have unit matrix
// as kernel.
layers.push_back(neural_network::layers::DenseLayer(
config[0].first, config[0].second,
{config[0].first, config[0].first}, false));
// Creating remaining layers
for (size_t i = 1; i < config.size(); i++) {
layers.push_back(neural_network::layers::DenseLayer(
config[i].first, config[i].second,
{config[i - 1].first, config[i].first}, true));
}
std::cout << "INFO: Network constructed successfully" << std::endl;
}
Here is the call graph for this function:

## NeuralNetwork() [4/5]

machine_learning::neural_network::NeuralNetwork::NeuralNetwork | ( | const NeuralNetwork & | model | ) |

default

Copy Constructor for class NeuralNetwork.

Parameters

  model | instance of class to be copied.

## ~NeuralNetwork()

machine_learning::neural_network::NeuralNetwork::~NeuralNetwork | ( |  | ) |

default

Destructor for class NeuralNetwork.

## NeuralNetwork() [5/5]

machine_learning::neural_network::NeuralNetwork::NeuralNetwork | ( | NeuralNetwork && |  | ) |

default

Move constructor for class NeuralNetwork

## Member Function Documentation

## __detailed_single_prediction()

std::vector<std::vector<std::valarray<double> > > machine_learning::neural_network::NeuralNetwork::__detailed_single_prediction | ( | const std::vector< std::valarray< double >> & | X | ) |

inlineprivate

Private function to get detailed predictions (i.e. activated neuron values). This function is used in backpropagation, single predict and batch predict.

Parameters

  X | input vector

{
std::vector<std::vector<std::valarray<double>>> details;
std::vector<std::valarray<double>> current_pass = X;
details.emplace_back(X);
for (const auto &l : layers) {
current_pass = multiply(current_pass, l.kernel);
current_pass = apply_function(current_pass, l.activation_function);
details.emplace_back(current_pass);
}
return details;
}
std::vector::emplace_back
T emplace_back(T... args)
machine_learning::multiply
std::vector< std::valarray< T > > multiply(const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
Definition: vector_ops.hpp:460
machine_learning::apply_function
std::vector< std::valarray< T > > apply_function(const std::vector< std::valarray< T >> &A, T(*func)(const T &))
Definition: vector_ops.hpp:329
std::vector
Here is the call graph for this function:

## batch_predict()

std::vector<std::vector<std::valarray<double> > > machine_learning::neural_network::NeuralNetwork::batch_predict | ( | const std::vector< std::vector< std::valarray< double >>> & | X | ) |

inline

Function to get prediction of model on batch

Parameters

  X | array of feature vectors

Returns
  returns predicted values as vector

{
// Store predicted values
std::vector<std::vector<std::valarray<double>>> predicted_batch(
X.size());
for (size_t i = 0; i < X.size(); i++) { // For every sample
// Push predicted values
predicted_batch[i] = this->single_predict(X[i]);
}
return predicted_batch; // Return predicted values
}
machine_learning::neural_network::NeuralNetwork::single_predict
std::vector< std::valarray< double > > single_predict(const std::vector< std::valarray< double >> &X)
Definition: neural_network.cpp:451
Here is the call graph for this function:

## evaluate()

void machine_learning::neural_network::NeuralNetwork::evaluate | ( | const std::vector< std::vector< std::valarray< double >>> & | X,
|  | const std::vector< std::vector< std::valarray< double >>> & | Y
| ) |  |

inline

Function to evaluate model on supplied data

Parameters

  X | array of feature vectors (input data)
  Y | array of target values (label)

{
std::cout << "INFO: Evaluation Started" << std::endl;
double acc = 0, loss = 0; // intialize performance metrics with zero
for (size_t i = 0; i < X.size(); i++) { // For every sample in input
// Get predictions
std::vector<std::valarray<double>> pred =
this->single_predict(X[i]);
// If predicted class is correct
if (argmax(pred) == argmax(Y[i])) {
acc += 1; // Increment accuracy
}
// Calculating loss - Mean Squared Error
loss += sum(apply_function((Y[i] - pred),
neural_network::util_functions::square) *
0.5);
}
acc /= X.size(); // Averaging accuracy
loss /= X.size(); // Averaging loss
// Prinitng performance of the model
std::cout << "Evaluation: Loss: " << loss;
std::cout << ", Accuracy: " << acc << std::endl;
return;
}
machine_learning::argmax
size_t argmax(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:307
machine_learning::sum
T sum(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:232
Here is the call graph for this function:

## evaluate_from_csv()

void machine_learning::neural_network::NeuralNetwork::evaluate_from_csv | ( | const std::string & | file_name,
|  | const bool & | last_label,
|  | const bool & | normalize,
|  | const int & | slip_lines = `1`
| ) |  |

inline

Function to evaluate model on data stored in csv file

Parameters

  file_name | csv file name
  last_label | flag for whether label is in first or last column
  normalize | flag for whether to normalize data
  slip_lines | number of lines to skip

{
// Getting training data from csv file
auto data =
this->get_XY_from_csv(file_name, last_label, normalize, slip_lines);
// Evaluating model
this->evaluate(data.first, data.second);
return;
}
machine_learning::neural_network::NeuralNetwork::evaluate
void evaluate(const std::vector< std::vector< std::valarray< double >>> &X, const std::vector< std::vector< std::valarray< double >>> &Y)
Definition: neural_network.cpp:606
machine_learning::neural_network::NeuralNetwork::get_XY_from_csv
std::pair< std::vector< std::vector< std::valarray< double > > >, std::vector< std::vector< std::valarray< double > > > > get_XY_from_csv(const std::string &file_name, const bool &last_label, const bool &normalize, const int &slip_lines=1)
Definition: neural_network.cpp:382
data
int data[MAX]
test data
Definition: hash_search.cpp:24
Here is the call graph for this function:

## fit()

void machine_learning::neural_network::NeuralNetwork::fit | ( | const std::vector< std::vector< std::valarray< double >>> & | X_,
|  | const std::vector< std::vector< std::valarray< double >>> & | Y_,
|  | const int & | epochs = `100`,
|  | const double & | learning_rate = `0.01`,
|  | const size_t & | batch_size = `32`,
|  | const bool & | shuffle = `true`
| ) |  |

inline

Function to fit model on supplied data

Parameters

  X | array of feature vectors
  Y | array of target values
  epochs | number of epochs (default = 100)
  learning_rate | learning rate (default = 0.01)
  batch_size | batch size for gradient descent (default = 32)
  shuffle | flag for whether to shuffle data (default = true)

{
std::vector<std::vector<std::valarray<double>>> X = X_, Y = Y_;
// Both label and input data should have same size
if (X.size() != Y.size()) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "X and Y in fit have different sizes" << std::endl;
std::exit(EXIT_FAILURE);
}
std::cout << "INFO: Training Started" << std::endl;
for (int epoch = 1; epoch <= epochs; epoch++) { // For every epoch
// Shuffle X and Y if flag is set
if (shuffle) {
equal_shuffle(X, Y);
}
auto start =
std::chrono::high_resolution_clock::now(); // Start clock
double loss = 0,
acc = 0; // Intialize performance metrics with zero
// For each starting index of batch
for (size_t batch_start = 0; batch_start < X.size();
batch_start += batch_size) {
for (size_t i = batch_start;
i < std::min(X.size(), batch_start + batch_size); i++) {
std::vector<std::valarray<double>> grad, cur_error,
predicted;
auto activations = this->__detailed_single_prediction(X[i]);
// Gradients vector to store gradients for all layers
// They will be averaged and applied to kernel
std::vector<std::vector<std::valarray<double>>> gradients;
gradients.resize(this->layers.size());
// First intialize gradients to zero
for (size_t i = 0; i < gradients.size(); i++) {
zeroes_initialization(
gradients[i], get_shape(this->layers[i].kernel));
}
predicted = activations.back(); // Predicted vector
cur_error = predicted - Y[i]; // Absoulute error
// Calculating loss with MSE
loss += sum(apply_function(
cur_error, neural_network::util_functions::square));
// If prediction is correct
if (argmax(predicted) == argmax(Y[i])) {
acc += 1;
}
// For every layer (except first) starting from last one
for (size_t j = this->layers.size() - 1; j >= 1; j--) {
// Backpropogating errors
cur_error = hadamard_product(
cur_error,
apply_function(
activations[j + 1],
this->layers[j].dactivation_function));
// Calculating gradient for current layer
grad = multiply(transpose(activations[j]), cur_error);
// Change error according to current kernel values
cur_error = multiply(cur_error,
transpose(this->layers[j].kernel));
// Adding gradient values to collection of gradients
gradients[j] = gradients[j] + grad / double(batch_size);
}
// Applying gradients
for (size_t j = this->layers.size() - 1; j >= 1; j--) {
// Updating kernel (aka weights)
this->layers[j].kernel = this->layers[j].kernel -
gradients[j] * learning_rate;
}
}
}
auto stop =
std::chrono::high_resolution_clock::now(); // Stoping the clock
// Calculate time taken by epoch
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(stop -
start);
loss /= X.size(); // Averaging loss
acc /= X.size(); // Averaging accuracy
std::cout.precision(4); // set output precision to 4
// Printing training stats
std::cout << "Training: Epoch " << epoch << '/' << epochs;
std::cout << ", Loss: " << loss;
std::cout << ", Accuracy: " << acc;
std::cout << ", Taken time: " << duration.count() / 1e6
<< " seconds";
std::cout << std::endl;
}
return;
}
machine_learning::neural_network::NeuralNetwork::__detailed_single_prediction
std::vector< std::vector< std::valarray< double > > > __detailed_single_prediction(const std::vector< std::valarray< double >> &X)
Definition: neural_network.cpp:289
std::min
T min(T... args)
activations
Various activation functions used in Neural network.
machine_learning::zeroes_initialization
void zeroes_initialization(std::vector< std::valarray< T >> &A, const std::pair< size_t, size_t > &shape)
Definition: vector_ops.hpp:213
machine_learning::equal_shuffle
void equal_shuffle(std::vector< std::vector< std::valarray< T >>> &A, std::vector< std::vector< std::valarray< T >>> &B)
Definition: vector_ops.hpp:136
machine_learning::hadamard_product
std::vector< std::valarray< T > > hadamard_product(const std::vector< std::valarray< T >> &A, const std::vector< std::valarray< T >> &B)
Definition: vector_ops.hpp:494
machine_learning::get_shape
std::pair< size_t, size_t > get_shape(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:247
machine_learning::transpose
std::vector< std::valarray< T > > transpose(const std::vector< std::valarray< T >> &A)
Definition: vector_ops.hpp:382
sorting::shuffle
std::array< T, N > shuffle(std::array< T, N > arr)
Definition: bogo_sort.cpp:36
std::chrono::high_resolution_clock::now
T now(T... args)
std::vector::resize
T resize(T... args)
Here is the call graph for this function:

## fit_from_csv()

void machine_learning::neural_network::NeuralNetwork::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`
| ) |  |

inline

Function to fit model on data stored in csv file

Parameters

  file_name | csv file name
  last_label | flag for whether label is in first or last column
  epochs | number of epochs
  learning_rate | learning rate
  normalize | flag for whether to normalize data
  slip_lines | number of lines to skip
  batch_size | batch size for gradient descent (default = 32)
  shuffle | flag for whether to shuffle data (default = true)

{
// Getting training data from csv file
auto data =
this->get_XY_from_csv(file_name, last_label, normalize, slip_lines);
// Fit the model on training data
this->fit(data.first, data.second, epochs, learning_rate, batch_size,
shuffle);
return;
}
machine_learning::neural_network::NeuralNetwork::fit
void fit(const std::vector< std::vector< std::valarray< double >>> &X_, const std::vector< std::vector< std::valarray< double >>> &Y_, const int &epochs=100, const double &learning_rate=0.01, const size_t &batch_size=32, const bool &shuffle=true)
Definition: neural_network.cpp:485
Here is the call graph for this function:

## get_XY_from_csv()

std::pair<std::vector<std::vector<std::valarray<double> > >, std::vector<std::vector<std::valarray<double> > > > machine_learning::neural_network::NeuralNetwork::get_XY_from_csv | ( | const std::string & | file_name,
|  | const bool & | last_label,
|  | const bool & | normalize,
|  | const int & | slip_lines = `1`
| ) |  |

inline

Function to get X and Y from csv file (where X = data, Y = label)

Parameters

  file_name | csv file name
  last_label | flag for whether label is in first or last column
  normalize | flag for whether to normalize data
  slip_lines | number of lines to skip

Returns
  returns pair of X and Y

{
std::ifstream in_file; // Ifstream to read file
in_file.open(file_name.c_str(), std::ios::in); // Open file
// If there is any problem in opening file
if (!in_file.is_open()) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Unable to open file: " << file_name << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::vector<std::valarray<double>>> X,
Y; // To store X and Y
std::string line; // To store each line
// Skip lines
for (int i = 0; i < slip_lines; i++) {
std::getline(in_file, line, '\n'); // Ignore line
}
// While file has information
while (!in_file.eof() && std::getline(in_file, line, '\n')) {
std::valarray<double> x_data,
y_data; // To store single sample and label
std::stringstream ss(line); // Constructing stringstream from line
std::string token; // To store each token in line (seprated by ',')
while (std::getline(ss, token, ',')) { // For each token
// Insert numerical value of token in x_data
x_data = insert_element(x_data, std::stod(token));
}
// If label is in last column
if (last_label) {
y_data.resize(this->layers.back().neurons);
// If task is classification
if (y_data.size() > 1) {
y_data[x_data[x_data.size() - 1]] = 1;
}
// If task is regrssion (of single value)
else {
y_data[0] = x_data[x_data.size() - 1];
}
x_data = pop_back(x_data); // Remove label from x_data
} else {
y_data.resize(this->layers.back().neurons);
// If task is classification
if (y_data.size() > 1) {
y_data[x_data[x_data.size() - 1]] = 1;
}
// If task is regrssion (of single value)
else {
y_data[0] = x_data[x_data.size() - 1];
}
x_data = pop_front(x_data); // Remove label from x_data
}
// Push collected X_data and y_data in X and Y
X.push_back({x_data});
Y.push_back({y_data});
}
// Normalize training data if flag is set
if (normalize) {
// Scale data between 0 and 1 using min-max scaler
X = minmax_scaler(X, 0.01, 1.0);
}
in_file.close(); // Closing file
return make_pair(X, Y); // Return pair of X and Y
}
std::ifstream
std::string
std::stringstream
std::string::c_str
T c_str(T... args)
std::ifstream::close
T close(T... args)
std::ifstream::eof
T eof(T... args)
std::getline
T getline(T... args)
std::ifstream::is_open
T is_open(T... args)
std::make_pair
T make_pair(T... args)
machine_learning::minmax_scaler
std::vector< std::vector< std::valarray< T > > > minmax_scaler(const std::vector< std::vector< std::valarray< T >>> &A, const T &low, const T &high)
Definition: vector_ops.hpp:269
machine_learning::pop_back
std::valarray< T > pop_back(const std::valarray< T > &A)
Definition: vector_ops.hpp:119
machine_learning::insert_element
std::valarray< T > insert_element(const std::valarray< T > &A, const T &ele)
Definition: vector_ops.hpp:85
machine_learning::pop_front
std::valarray< T > pop_front(const std::valarray< T > &A)
Definition: vector_ops.hpp:102
std::ifstream::open
T open(T... args)
std::vector::push_back
T push_back(T... args)
std::stod
T stod(T... args)
std::valarray< double >
Here is the call graph for this function:

## load_model()

NeuralNetwork machine_learning::neural_network::NeuralNetwork::load_model | ( | const std::string & | file_name | ) |

inline

Function to load earlier saved model.

Parameters

  file_name | file from which model will be loaded (*.model)

Returns
  instance of NeuralNetwork class with pretrained weights

{
std::ifstream in_file; // Ifstream to read file
in_file.open(file_name.c_str()); // Openinig file
// If there is any problem in opening file
if (!in_file.is_open()) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Unable to open file: " << file_name << std::endl;
std::exit(EXIT_FAILURE);
}
std::vector<std::pair<int, std::string>> config; // To store config
std::vector<std::vector<std::valarray<double>>>
kernels; // To store pretrained kernels
// Loading model from saved file format
size_t total_layers = 0;
in_file >> total_layers;
for (size_t i = 0; i < total_layers; i++) {
int neurons = 0;
std::string activation;
size_t shape_a = 0, shape_b = 0;
std::vector<std::valarray<double>> kernel;
in_file >> neurons >> activation >> shape_a >> shape_b;
for (size_t r = 0; r < shape_a; r++) {
std::valarray<double> row(shape_b);
for (size_t c = 0; c < shape_b; c++) {
in_file >> row[c];
}
kernel.push_back(row);
}
config.emplace_back(make_pair(neurons, activation));
;
kernels.emplace_back(kernel);
}
std::cout << "INFO: Model loaded successfully" << std::endl;
in_file.close(); // Closing file
return NeuralNetwork(
config, kernels); // Return instance of NeuralNetwork class
}
machine_learning::neural_network::NeuralNetwork::NeuralNetwork
NeuralNetwork()=default
Here is the call graph for this function:

## operator=() [1/2]

NeuralNetwork& machine_learning::neural_network::NeuralNetwork::operator= | ( | const NeuralNetwork & | model | ) |

default

Copy assignment operator for class NeuralNetwork

## operator=() [2/2]

NeuralNetwork& machine_learning::neural_network::NeuralNetwork::operator= | ( | NeuralNetwork && |  | ) |

default

Move assignment operator for class NeuralNetwork

## save_model()

void machine_learning::neural_network::NeuralNetwork::save_model | ( | const std::string & | _file_name | ) |

inline

Function to save current model.

Parameters

  file_name | file name to save model (*.model)

Format in which model is saved:

total_layers neurons(1st neural_network::layers::DenseLayer) activation_name(1st neural_network::layers::DenseLayer) kernel_shape(1st neural_network::layers::DenseLayer) kernel_values neurons(Nth neural_network::layers::DenseLayer) activation_name(Nth neural_network::layers::DenseLayer) kernel_shape(Nth neural_network::layers::DenseLayer) kernel_value

For Example, pretrained model with 3 layers:

     3
     4 none
     4 4
     1 0 0 0
     0 1 0 0
     0 0 1 0
     0 0 0 1
     6 relu
     4 6
     -1.88963 -3.61165 1.30757 -0.443906 -2.41039 -2.69653
     -0.684753 0.0891452 0.795294 -2.39619 2.73377 0.318202
     -2.91451 -4.43249 -0.804187 2.51995 -6.97524 -1.07049
     -0.571531 -1.81689 -1.24485 1.92264 -2.81322 1.01741
     3 sigmoid
     6 3
     0.390267 -0.391703 -0.0989607
     0.499234 -0.564539 -0.28097
     0.553386 -0.153974 -1.92493
     -2.01336 -0.0219682 1.44145
     1.72853 -0.465264 -0.705373
     -0.908409 -0.740547 0.376416

{
std::string file_name = _file_name;
// Adding ".model" extension if it is not already there in name
if (file_name.find(".model") == file_name.npos) {
file_name += ".model";
}
std::ofstream out_file; // Ofstream to write in file
// Open file in out|trunc mode
out_file.open(file_name.c_str(),
std::ofstream::out | std::ofstream::trunc);
// If there is any problem in opening file
if (!out_file.is_open()) {
std::cerr << "ERROR (" << __func__ << ") : ";
std::cerr << "Unable to open file: " << file_name << std::endl;
std::exit(EXIT_FAILURE);
}
/**
Format in which model is saved:
total_layers
neurons(1st neural_network::layers::DenseLayer) activation_name(1st
neural_network::layers::DenseLayer) kernel_shape(1st
neural_network::layers::DenseLayer) kernel_values
.
.
.
neurons(Nth neural_network::layers::DenseLayer) activation_name(Nth
neural_network::layers::DenseLayer) kernel_shape(Nth
neural_network::layers::DenseLayer) kernel_value
For Example, pretrained model with 3 layers:
<pre>
3
4 none
4 4
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
6 relu
4 6
-1.88963 -3.61165 1.30757 -0.443906 -2.41039 -2.69653
-0.684753 0.0891452 0.795294 -2.39619 2.73377 0.318202
-2.91451 -4.43249 -0.804187 2.51995 -6.97524 -1.07049
-0.571531 -1.81689 -1.24485 1.92264 -2.81322 1.01741
3 sigmoid
6 3
0.390267 -0.391703 -0.0989607
0.499234 -0.564539 -0.28097
0.553386 -0.153974 -1.92493
-2.01336 -0.0219682 1.44145
1.72853 -0.465264 -0.705373
-0.908409 -0.740547 0.376416
</pre>
*/
// Saving model in the same format
out_file << layers.size();
out_file << std::endl;
for (const auto &layer : this->layers) {
out_file << layer.neurons << ' ' << layer.activation << std::endl;
const auto shape = get_shape(layer.kernel);
out_file << shape.first << ' ' << shape.second << std::endl;
for (const auto &row : layer.kernel) {
for (const auto &val : row) {
out_file << val << ' ';
}
out_file << std::endl;
}
}
std::cout << "INFO: Model saved successfully with name : ";
std::cout << file_name << std::endl;
out_file.close(); // Closing file
return;
}
std::ofstream
std::string::find
T find(T... args)
Here is the call graph for this function:

## single_predict()

std::vector<std::valarray<double> > machine_learning::neural_network::NeuralNetwork::single_predict | ( | const std::vector< std::valarray< double >> & | X | ) |

inline

Function to get prediction of model on single sample.

Parameters

  X | array of feature vectors

Returns
  returns predictions as vector

{
// Get activations of all layers
auto activations = this->__detailed_single_prediction(X);
// Return activations of last layer (actual predicted values)
return activations.back();
}
Here is the call graph for this function:

## summary()

void machine_learning::neural_network::NeuralNetwork::summary | ( |  | ) |

inline

Function to print summary of the network.

{
// Printing Summary
std::cout
<< "==============================================================="
<< std::endl;
std::cout << "\t\t+ MODEL SUMMARY +\t\t\n";
std::cout
<< "==============================================================="
<< std::endl;
for (size_t i = 1; i <= layers.size(); i++) { // For every layer
std::cout << i << ")";
std::cout << " Neurons : "
<< layers[i - 1].neurons; // number of neurons
std::cout << ", Activation : "
<< layers[i - 1].activation; // activation
std::cout << ", kernel Shape : "
<< get_shape(layers[i - 1].kernel); // kernel shape
std::cout << std::endl;
}
std::cout
<< "==============================================================="
<< std::endl;
return;
}
Here is the call graph for this function:

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

- machine_learning/neural_network.cpp