Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

rufort / IAS0360_lab_excercises_2024

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Switch branch/tag
  • IAS0360_lab_excercises_2024
  • lib
  • includes
  • NeuralNetwork.h
Find file
BlameHistoryPermalink
  • Nazrul_being's avatar
    Added lab 4 · a4da20db
    Nazrul_being committed 7 months ago
    a4da20db
NeuralNetwork.h 3.39 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#ifndef NEURALNETWORK_H
#define NEURALNETWORK_H

#include <vector>
#include <string>

class NeuralNetwork {
public:
    // Single neuron calculation
    double singleNeuron(double input, double weight);

    // Multiple inputs, single output
    double multipleInputSingleOutput(std::vector<double> inputs, std::vector<double> weights, double bias);

    // Single input, multiple outputs
    void singleInputMultipleOutput(double input, std::vector<double> weights, double bias, std::vector<double>& outputs);

    // Multiple inputs, multiple outputs
    void multipleInputMultipleOutput(std::vector<double>& inputs, std::vector<double>& weights, std::vector<double>& biases, std::vector<double>& outputs, int inputSize, int outputSize);

    // Hidden layer function
    void hiddenLayer(std::vector<double>& inputs, std::vector<double>& hiddenWeights, std::vector<double>& hiddenBiases, std::vector<double>& hiddenOutputs, int inputSize, int hiddenSize);

    // Error calculation
    void calculateError(std::vector<double>& predictedOutput, std::vector<double>& groundTruth, std::vector<double>& error);

    // Mean Squared Error (MSE)
    double calculateMSE(std::vector<double>& error);

    // Root Mean Squared Error (RMSE)
    double calculateRMSE(double mse);

    // Brute-force learning to find the best weight
    void bruteForceLearning(double input, double& weight, double expectedValue, double learningRate, int maxEpochs);

    // Backpropagation learning function
    void backpropagation(const std::vector<double>& input, const std::vector<double>& expectedOutput, 
                     std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenBiases,
                     std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputBiases,
                     double learningRate, int epochs);


    // Activation functions (ReLU and Sigmoid)
    double relu(double x);
    double sigmoid(double x);

    // Vectorized activation functions
    void vectorReLU(std::vector<double>& inputVector, std::vector<double>& outputVector);
    void vectorSigmoid(std::vector<double>& inputVector, std::vector<double>& outputVector);

    // Print a 2D matrix
    void printMatrix(int rows, int cols, const std::vector<std::vector<double>>& matrix);

    // Compute cost for logistic regression
    double computeCost(int m, const std::vector<std::vector<double>>& yhat, const std::vector<std::vector<double>>& y);

    // Normalize a 2D matrix
    int normalizeData2D(const std::vector<std::vector<double>>& inputMatrix, std::vector<std::vector<double>>& outputMatrix);

    // Save network
    void saveNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
                     std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
                     std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias);

    // Load network
    void loadNetwork(const std::string& filename, int numOfFeatures, int numOfHiddenNodes, int numOfOutputNodes,
                     std::vector<std::vector<double>>& inputToHiddenWeights, std::vector<double>& hiddenLayerBias,
                     std::vector<std::vector<double>>& hiddenToOutputWeights, std::vector<double>& outputLayerBias);
};

#endif // NEURALNETWORK_H