Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Neural Network (Multilayer Perceptron) #1025

Merged
merged 21 commits into from
Aug 19, 2020
Merged

feat: Add Neural Network (Multilayer Perceptron) #1025

merged 21 commits into from
Aug 19, 2020

Conversation

imdeep2905
Copy link
Contributor

@imdeep2905 imdeep2905 commented Aug 14, 2020

Description of Change

Added Neural Network as machine_learning/neural_network.cpp
Added Vector Ops as machine_learning/vector_ops.hpp (Operations required for Neural Network)
Added iris.csv as machine_learning/iris.csv (Training file for Network)

This will close #967

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Added documentation so that the program is self-explanatory and educational - Doxygen guidelines
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

@Panquesito7 Panquesito7 added the enhancement New feature or request label Aug 14, 2020
@imdeep2905
Copy link
Contributor Author

Is there any way to solve following :

/home/runner/work/C-Plus-Plus/C-Plus-Plus/machine_learning/vector_ops.hpp:163:10: error: no template named 'uniform_real_distribution' in namespace 'std'; did you mean 'uniform_int_distribution'? [clang-diagnostic-error]
    std::uniform_real_distribution <T> distribution(low, high);

@Panquesito7 Panquesito7 added the automated tests are failing Do not merge until tests pass label Aug 14, 2020
@imdeep2905 imdeep2905 requested a review from Panquesito7 August 14, 2020 20:22
@Panquesito7 Panquesito7 removed the automated tests are failing Do not merge until tests pass label Aug 14, 2020
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Show resolved Hide resolved
machine_learning/vector_ops.hpp Show resolved Hide resolved
machine_learning/vector_ops.hpp Show resolved Hide resolved
@Panquesito7 Panquesito7 added the requested changes changes have been requested label Aug 14, 2020
imdeep2905 and others added 7 commits August 15, 2020 02:18
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
@Panquesito7 Panquesito7 added automated tests are failing Do not merge until tests pass awaiting modification Do not merge until modifications are made labels Aug 14, 2020
Copy link
Collaborator

@kvedala kvedala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Excellent work Thanks 😄
Please note the comments.
Reviewed file neural_network.cpp. Haven't reviewed vector_ops.hpp and haven't executed the code yet.

machine_learning/iris.csv Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
machine_learning/neural_network.cpp Outdated Show resolved Hide resolved
* @param file_name file from which model will be loaded (*.model)
* @return instance of NeuralNetwork class with pretrained weights
*/
NeuralNetwork load_model (const std::string &file_name) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this function returns a class instance, for it to be usable, it'd require the implementation of a copy constructor for this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested it with default copy constructor and it works fine.

@lgtm-com

This comment has been minimized.

@Panquesito7 Panquesito7 removed automated tests are failing Do not merge until tests pass awaiting modification Do not merge until modifications are made labels Aug 16, 2020
@imdeep2905 imdeep2905 requested a review from kvedala August 18, 2020 18:06
Copy link
Collaborator

@kvedala kvedala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed response. I was waiting to get a change to execute and test the code. I did on GitPod using the link in the description and got the result below. I think there is some error in the code. Can you please review and fix. Thanks.

gitpod /workspace/C-Plus-Plus $ /workspace/C-Plus-Plus/build/machine_learning/neural_network
INFO: Network constructed successfully
===============================================================
                + MODEL SUMMARY +
===============================================================
1) Neurons : 4, Activation : none, Kernal Shape : (4, 4)
2) Neurons : 6, Activation : relu, Kernal Shape : (4, 6)
3) Neurons : 3, Activation : sigmoid, Kernal Shape : (6, 3)
===============================================================
INFO: Training Started
Training: Epoch 1/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 2/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 3/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 4/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 5/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 6/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 7/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 8/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 9/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 10/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 11/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 12/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 13/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 14/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 15/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 16/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 17/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 18/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 19/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 20/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 21/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 22/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 23/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 24/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 25/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 26/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 27/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 28/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 29/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 30/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 31/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 32/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 33/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 34/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 35/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 36/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 37/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 38/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 39/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 40/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 41/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 42/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 43/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 44/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 45/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 46/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 47/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 48/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 49/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 50/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 51/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 52/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 53/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 54/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 55/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 56/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 57/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 58/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 59/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 60/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 61/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 62/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 63/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 64/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 65/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 66/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 67/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 68/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 69/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 70/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 71/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 72/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 73/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 74/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 75/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 76/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 77/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 78/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 79/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 80/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 81/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 82/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 83/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 84/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 85/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 86/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 87/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 88/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 89/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 90/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 91/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 92/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 93/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 94/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 95/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 96/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 97/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 98/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 99/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
Training: Epoch 100/100, Loss: -nan, Accuracy: -nan, Taken time: 0 seconds
neural_network: ../machine_learning/neural_network.cpp:780: void test(): Assertion `machine_learning::argmax(myNN.single_predict({{6.4,2.9,4.3,1.3}})) == 1' failed.
Aborted (core dumped)
gitpod /workspace/C-Plus-Plus $ 

@imdeep2905
Copy link
Contributor Author

Sorry for the delayed response. I was waiting to get a change to execute and test the code. I did on GitPod using the link in the description and got the result below. I think there is some error in the code. Can you please review and fix. Thanks.

I did a quick run on gitpod and it works fine:
Capture

@imdeep2905 imdeep2905 requested a review from kvedala August 19, 2020 04:53
Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the same output result as @kvedala's #1025 (review).

@kvedala
Copy link
Collaborator

kvedala commented Aug 19, 2020

Sorry for the delayed response. I was waiting to get a change to execute and test the code. I did on GitPod using the link in the description and got the result below. I think there is some error in the code. Can you please review and fix. Thanks.

I did a quick run on gitpod and it works fine:

Not sure how you ran the code. I tried again with all available compilers with the same error. Steps to obtain the error:

  1. click on the open in gitpod link in description above
  2. after the environment loads, it gives the option to pick the compiler toolkit. I selected "GCC 9.3.0" once, another time I selected "Clang 11.0.0".
  3. Once the toolkit is selected, the environment executes configuration step on its own. If not, ignore and proceed to next step 4.
  4. Click on "Select the target to launch" button.
    image
  5. This will open up a drop-down list of available targets. Type "neural" and the file will be displayed. Click and select it.
    image
  6. Compile and Run the target by clicking on the "Run" icon.
    image
  7. The program will compile and execute. Please report the results without any modification to the code.

@imdeep2905
Copy link
Contributor Author

imdeep2905 commented Aug 19, 2020

  1. The program will compile and execute. Please report the results without any modification to the code.

I have done as you said. I am actually getting some weird output. But when I run program with command line (i.e. g++ -std=c++14) I get output without any issues. I think the issue is with iris.csv. Probably program can't find iris.csv. Should I check again with absolute path rather than relative (for iris.csv) ?

1
2

@imdeep2905
Copy link
Contributor Author

Probably program can't find iris.csv. Should I check again with absolute path rather than relative (for iris.csv) ?

Yup issue indeed was with path to iris.csv. Now it is working fine.
1

Now, should I change path in this PR ?

@imdeep2905 imdeep2905 requested a review from Panquesito7 August 19, 2020 19:15
@kvedala
Copy link
Collaborator

kvedala commented Aug 19, 2020

Probably program can't find iris.csv. Should I check again with absolute path rather than relative (for iris.csv) ?

Yup issue indeed was with path to iris.csv. Now it is working fine.
1

Now, should I change path in this PR ?

👍 Ah. no need to change the path. That is why I suggested to generate pattern data :)
Well done

@kvedala kvedala merged commit 4a34bec into TheAlgorithms:master Aug 19, 2020
@imdeep2905
Copy link
Contributor Author

Thank you very much !!! 🍾

One last thing. Actually I am bit new with this PR stuff. I can see review option on other PRs. Is it possible for me to do a review of PRs ?

@Panquesito7 Panquesito7 added approved Approved; waiting for merge and removed requested changes changes have been requested labels Aug 19, 2020
@kvedala
Copy link
Collaborator

kvedala commented Aug 19, 2020

Thank you very much !!! 🍾

One last thing. Actually I am bit new with this PR stuff. I can see review option on other PRs. Is it possible for me to do a review of PRs ?

Absolutely. Please do provide your feedback while respecting other contributors :)
Thank you 🍻

@imdeep2905 imdeep2905 deleted the nn branch August 19, 2020 19:38
@kvedala
Copy link
Collaborator

kvedala commented Aug 19, 2020

@imdeep2905 Can you create a PR with the following fix:

check if the file was opened successfully, if not, print error message to std::cerr and exit

@imdeep2905
Copy link
Contributor Author

@imdeep2905 Can you create a PR with the following fix:

check if the file was opened successfully, if not, print error message to std::cerr and exit

Sure !
On it's way...

const bool &normalize,
const int &slip_lines = 1) {
std::ifstream in_file; // Ifstream to read file
in_file.open(file_name.c_str(), std::ios::in); // Open file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here, you can add a check, something like this:

if(!in_file.is_open()) {
    std::cerr << "Unable to open file: "<< file_name << "\n";
    exit(EXIT_FAILURE);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Approved; waiting for merge enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Multilayer Perceptron
3 participants