Question

In: Computer Science

Create a basic functioning Deep Belief Network coding using C++ or MATLAB.

Create a basic functioning Deep Belief Network coding using C++ or MATLAB.

Solutions

Expert Solution

// NeuralNetwork.hpp
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <vector>

// use typedefs for future ease for changing data types like : float to double
typedef float Scalar;
typedef Eigen::MatrixXf Matrix;
typedef Eigen::RowVectorXf RowVector;
typedef Eigen::VectorXf ColVector;

// neural network implementation class!
class NeuralNetwork {
public:
   // constructor
   NeuralNetwork(std::vector<uint> topology, Scalar learningRate = Scalar(0.005));

   // function for forward propagation of data
   void propagateForward(RowVector& input);

   // function for backward propagation of errors made by neurons
   void propagateBackward(RowVector& output);

   // function to calculate errors made by neurons in each layer
   void calcErrors(RowVector& output);

   // function to update the weights of connections
   void updateWeights();

   // function to train the neural network give an array of data points
   void train(std::vector<RowVector*> data);

   // storage objects for working of neural network
   /*
       use pointers when using std::vector<Class> as std::vector<Class> calls destructor of
       Class as soon as it is pushed back! when we use pointers it can't do that, besides
       it also makes our neural network class less heavy!! It would be nice if you can use
       smart pointers instead of usual ones like this
       */
   std::vector<RowVector*> neuronLayers; // stores the different layers of out network
   std::vector<RowVector*> cacheLayers; // stores the unactivated (activation fn not yet applied) values of layers
   std::vector<RowVector*> deltas; // stores the error contribution of each neurons
   std::vector<Matrix*> weights; // the connection weights itself
   Scalar learningRate;
};

Code: Constructor for the Neural Network Class

filter_none

edit

play_arrow

brightness_4

// constructor of neural network class

NeuralNetwork::NeuralNetwork(std::vector<uint> topology, Scalar learningRate)

{

    this->topology = topology;

    this->learningRate = learningRate;

    for (uint i = 0; i < topology.size(); i++) {

        // initialze neuron layers

        if (i == topology.size() - 1)

            neuronLayers.push_back(new RowVector(topology[i]));

        else

            neuronLayers.push_back(new RowVector(topology[i] + 1));

        // initialize cache and delta vectors

        cacheLayers.push_back(new RowVector(neuronLayers.size()));

        deltas.push_back(new RowVector(neuronLayers.size()));

        // vector.back() gives the handle to recently added element

        // coeffRef gives the reference of value at that place

        // (using this as we are using pointers here)

        if (i != topology.size() - 1) {

            neuronLayers.back()->coeffRef(topology[i]) = 1.0;

            cacheLayers.back()->coeffRef(topology[i]) = 1.0;

        }

        // initialze weights matrix

        if (i > 0) {

            if (i != topology.size() - 1) {

                weights.push_back(new Matrix(topology[i - 1] + 1, topology[i] + 1));

                weights.back()->setRandom();

                weights.back()->col(topology[i]).setZero();

                weights.back()->coeffRef(topology[i - 1], topology[i]) = 1.0;

            }

            else {

                weights.push_back(new Matrix(topology[i - 1] + 1, topology[i]));

                weights.back()->setRandom();

            }

        }

    }

};

Code: Feed Forward Algorithm

filter_none

brightness_4

void NeuralNetwork::propagateForward(RowVector& input)

{

    // set the input to input layer

    // block returns a part of the given vector or matrix

    // block takes 4 arguments : startRow, startCol, blockRows, blockCols

    neuronLayers.front()->block(0, 0, 1, neuronLayers.front()->size() - 1) = input;

    // propagate the data forawrd

    for (uint i = 1; i < topology.size(); i++) {

        // already explained above

        (*neuronLayers[i]) = (*neuronLayers[i - 1]) * (*weights[i - 1]);

    }

    // apply the activation function to your network

    // unaryExpr applies the given function to all elements of CURRENT_LAYER

    for (uint i = 1; i < topology.size() - 1; i++) {

        neuronLayers[i]->block(0, 0, 1, topology[i]).unaryExpr(std::ptr_fun(activationFunction));

    }

}

Calculating Errors:

filter_none

brightness_4

void NeuralNetwork::calcErrors(RowVector& output)

{

    // calculate the errors made by neurons of last layer

    (*deltas.back()) = output - (*neuronLayers.back());

    // error calculation of hidden layers is different

    // we will begin by the last hidden layer

    // and we will continue till the first hidden layer

    for (uint i = topology.size() - 2; i > 0; i--) {

        (*deltas[i]) = (*deltas[i + 1]) * (weights[i]->transpose());

    }

}

Code: Updating the weights

filter_none

brightness_4

void NeuralNetwork::updateWeights()

{

    // topology.size()-1 = weights.size()

    for (uint i = 0; i < topology.size() - 1; i++) {

        // in this loop we are iterating over the different layers (from first hidden to output layer)

        // if this layer is the output layer, there is no bias neuron there, number of neurons specified = number of cols

        // if this layer not the output layer, there is a bias neuron and number of neurons specified = number of cols -1

        if (i != topology.size() - 2) {

            for (uint c = 0; c < weights[i]->cols() - 1; c++) {

                for (uint r = 0; r < weights[i]->rows(); r++) {

                    weights[i]->coeffRef(r, c) += learningRate * deltas[i + 1]->coeffRef(c) * activationFunctionDerivative(cacheLayers[i + 1]->coeffRef(c)) * neuronLayers[i]->coeffRef(r);

                }

            }

        }

        else {

            for (uint c = 0; c < weights[i]->cols(); c++) {

                for (uint r = 0; r < weights[i]->rows(); r++) {

                    weights[i]->coeffRef(r, c) += learningRate * deltas[i + 1]->coeffRef(c) * activationFunctionDerivative(cacheLayers[i + 1]->coeffRef(c)) * neuronLayers[i]->coeffRef(r);

                }

            }

        }

    }

}

Backpropagation Algorithm:

filter_none

brightness_4

void NeuralNetwork::propagateBackward(RowVector& output)

{

    calcErrors(output);

    updateWeights();

}

Code: Activation Function

filter_none

brightness_4

Scalar activationFunction(Scalar x)

{

    return tanhf(x);

}

Scalar activationFunctionDerivative(Scalar x)

{

    return 1 - tanhf(x) * tanhf(x);

}

// you can use your own code here!

Code: Training neural network

filter_none

brightness_4

void NeuralNetwork::train(std::vector<RowVector*> input_data, std::vector<RowVector*> output_data)

{

    for (uint i = 0; i < input_data.size(); i++) {

        std::cout << "Input to neural network is : " << *input_data[i] << std::endl;

        propagateForward(*input_data[i]);

        std::cout << "Expected output is : " << *output_data[i] << std::endl;

        std::cout << "Output produced is : " << *neuronLayers.back() << std::endl;

        propagateBackward(*output_data[i]);

        std::cout << "MSE : " << std::sqrt((*deltas.back()).dot((*deltas.back())) / deltas.back()->size()) << std::endl;

    }

}

Code: Loading data

filter_none

brightness_4

void ReadCSV(std::string filename, std::vector<RowVector*>& data)

{

    data.clear();

    std::ifstream file(filename);

    std::string line, word;

    // determine number of columns in file

    getline(file, line, '\n');

    std::stringstream ss(line);

    std::vector<Scalar> parsed_vec;

    while (getline(ss, word, ', ')) {

        parsed_vec.push_back(Scalar(std::stof(&word[0])));

    }

    uint cols = parsed_vec.size();

    data.push_back(new RowVector(cols));

    for (uint i = 0; i < cols; i++) {

        data.back()->coeffRef(1, i) = parsed_vec[i];

    }

    // read the file

    if (file.is_open()) {

        while (getline(file, line, '\n')) {

            std::stringstream ss(line);

            data.push_back(new RowVector(1, cols));

            uint i = 0;

            while (getline(ss, word, ', ')) {

                data.back()->coeffRef(i) = Scalar(std::stof(&word[0]));

                i++;

            }

        }

    }

}

The user can read csv files using this code and paste this in the neural network class but be careful, the declarations and definitions must be kept in separate files (NeuralNetwork.cpp and NeuralNetwork.h). Save all the files and be with me for a few minutes!

Code: Generate Some Noise i.e. training data

filter_none

edit

play_arrow

brightness_4

void genData(std::string filename)

{

    std::ofstream file1(filename + "-in");

    std::ofstream file2(filename + "-out");

    for (uint r = 0; r < 1000; r++) {

        Scalar x = rand() / Scalar(RAND_MAX);

        Scalar y = rand() / Scalar(RAND_MAX);

        file1 << x << ", " << y << std::endl;

        file2 << 2 * x + 10 + y << std::endl;

    }

    file1.close();

    file2.close();

}

Code: Implementation of the neural network.

filter_none

brightness_4

// main.cpp

// don't forget to include out neural network

#include "NeuralNetwork.hpp"

//... data generator code here

typedef std::vector<RowVector*> data;

int main()

{

    NeuralNetwork n({ 2, 3, 1 });

    data in_dat, out_dat;

    genData("test");

    ReadCSV("test-in", in_dat);

    ReadCSV("test-out", out_dat);

    n.train(in_dat, out_dat);

    return 0;

}

note: plzzz don't give dislike.....plzzz comment if you have any problem i will try to solve your problem.....plzzz give thumbs up i am in need....


Related Solutions

Provide a simple Deep Belief Network numerical method (calculation example). You can use your own values.
Provide a simple Deep Belief Network numerical method (calculation example). You can use your own values.
how to write coding in matlab using gauss elimination method for equation 10a + 50b +...
how to write coding in matlab using gauss elimination method for equation 10a + 50b + 20c + 10d = 100 5a + 15b + 75c - 25d=200 25a -15c - 5d = 300 10a + 20b - 30c + 100d = 400
Create a message based on the health belief model of a Hepatitis C. Please fill in...
Create a message based on the health belief model of a Hepatitis C. Please fill in the blanks on the table: Concept: Definition: Application: Perceived Susceptibility Perceived Severity Perceived Benefits Perceived Barriers Cues to Action Self-Efficacy
In c# I need to create a simple payroll management system using visual basic and GUI....
In c# I need to create a simple payroll management system using visual basic and GUI. I also need to connect to SQL database. It needs a log in screen, inside the login screen it needs another screen to enter or edit employee information. It needs somewhere to enter hours worked for that specific employee, and another screen that can access reports.
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops,...
MATLAB is program Respond fast please a quiz Create a MATLAB script. Using nested for loops, evaluate the multivariable function: z = sin ⁡ ( x ) cos ⁡ ( y ) for x between -4 and 4 in steps of 1 y between -2 and 2 in steps of 1 Display the matrix z Cut and paste the following into a word doc and submit to this question. Your script code Your input Your output
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with a few classes. Requirements: Automated Teller Machine (ATM) simulationGiven 3 trials, the user is able to see his balance by entering a four-digit pin that must NEVER be displayed on screen but masked by the Asterix (*) character. A list of pins stored on the file system must be loaded to verify the pin. The user should be able to withdrawfundsbelow a set limit...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
Using Coding langues C ++ ---------------------------------------- There are three seating categories at a stadium. For a...
Using Coding langues C ++ ---------------------------------------- There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales. Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always...
Using Matlab functions ‘fdesign’ and ‘design’, create the following filters (do not forget to create an...
Using Matlab functions ‘fdesign’ and ‘design’, create the following filters (do not forget to create an m file to write a script that should be printed in your report). [0.5] a-c each (a) Low pass Hamming and low pass Hann filters, with cut off frequency Fc=300 Hz, sampling frequency Fs=2000 Hz, filter order N=100. Plot both filters on the same graph and use legend in the figure to mark each filter. Comment on the figure. (b) Low pass Hamming filter...
Create a diagram of a Small/Medium size Business network Using Computer Network Defense and Defense in...
Create a diagram of a Small/Medium size Business network Using Computer Network Defense and Defense in Depth, Define what a secure network should look like on a modest budget. (Describe your diagram and why you placed your appliances where you chose to place them).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT