-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forward propagation, loss and accuracy computation done
- Loading branch information
1 parent
39ea647
commit 88d20ab
Showing
13 changed files
with
359 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "neural_network.hpp" | ||
|
||
#include <iostream> | ||
#include <Eigen/Dense> | ||
|
||
using namespace std; | ||
using namespace Eigen; | ||
|
||
//==================== | ||
// Plain NN TRAINING: | ||
//==================== | ||
|
||
void PlainNN(MatrixXd X, MatrixXd Y, MatrixXd Y_onehot, MatrixXd &w_1, MatrixXd &w_2) | ||
{ | ||
for(int e = 0; e < NUM_EPOCHS; e ++) | ||
{ | ||
cout<< "Epoch Number: "<< e+1 << endl; | ||
double epoch_loss = 0.0; | ||
|
||
for(int i = 0; i < 1; i ++) //int(N_train/B) | ||
{ | ||
cout<< " Iteration Number: "<< i+1 << endl; | ||
|
||
// forward propagation that gives the final output and drelu values of first layer | ||
MatrixXd drelu_1(B, d_1); | ||
MatrixXd Z_1(B, d_1); | ||
MatrixXd Y_hat = ForwardPass(X.block(B*i, 0, B, X.cols()), w_1, w_2, Z_1, drelu_1); | ||
// cout << drelu_1.row(0) << endl; | ||
// cout << Z_1.row(0) << endl; | ||
// MatrixXd test = Y_hat.row(0); | ||
// cout << Y_hat.row(0) << endl; | ||
// cout << test.rowwise().sum() << endl; | ||
// cout << Y_onehot.row(0) << endl; | ||
// cout << Y(0,0) << endl; | ||
|
||
// loss and accuracy computation | ||
epoch_loss += ComputeLoss(Y_onehot.block(B*i, 0, B, Y_onehot.cols()), Y_hat); | ||
float acc = ComputeAccuracy(Y.block(B*i,0,B,Y.cols()), Y_hat); | ||
cout << " Loss: " << epoch_loss/(B*(i+1)) << endl; | ||
cout << " Accuracy: " << acc << "%" << endl; | ||
|
||
// backward propagation | ||
|
||
// delta_2 and delta_1 computation | ||
|
||
|
||
|
||
// weight update function | ||
|
||
|
||
|
||
|
||
// MatrixXd D = YY - Y.block(B * i,0,B,Y.cols()); // D = X_B_i.w - Y_B_i | ||
// //cout<< "diff: "<< endl << D << endl; | ||
// // Loss Computation | ||
// MatrixXd loss = D.transpose() * D; | ||
// MatrixXd delta = X.transpose().block(0,B * i,X.cols(),B) * D; // delta = X^T_B_i(X.w - Y) | ||
// //cout<< "grad: " << endl << delta << endl; | ||
// w = w - (delta / (B*100)); // w -= alpha/B * delta | ||
// //cout<<"weights: "<< endl << w <<endl; | ||
// //cout<<w<<endl; | ||
// epoch_loss += loss(0,0); | ||
} | ||
// cout<<endl; | ||
// cout<< "Loss: "<< epoch_loss/N << endl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef NEURAL_NETWORK_HPP | ||
#define NEURAL_NETWORK_HPP | ||
|
||
#include "define.hpp" | ||
#include "utils.hpp" | ||
|
||
#include <iostream> | ||
#include <Eigen/Dense> | ||
|
||
using namespace std; | ||
using namespace Eigen; | ||
|
||
// function that trains plain NN | ||
void PlainNN(MatrixXd X, MatrixXd Y, MatrixXd Y_onehot, MatrixXd &w_1, MatrixXd &w_2); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cout << X_train.rows() << "," << X_train.cols() << endl; | ||
cout << X_test.rows() << "," << X_test.cols() << endl; | ||
cout << Y_train.rows() << "," << Y_train.cols() << endl; | ||
cout << Y_train_onehot.rows() << "," << Y_train_onehot.cols() << endl; | ||
cout << Y_test.rows() << "," << Y_test.cols() << endl; | ||
cout << Y_test_onehot.rows() << "," << Y_test_onehot.cols() << endl; | ||
cout << w_1.rows() << "," << w_1.cols() << endl; | ||
cout << w_2.rows() << "," << w_1.cols() << endl; | ||
|
||
|
||
cout << Y_train.block(0,0,10,1) << endl; | ||
cout << Y_train_onehot.block(0,0,10,10) << endl; | ||
cout << w_1.block(0,0,10,10) << endl; | ||
cout << w_2.block(0,0,10,10) << endl; | ||
|
||
// cout << X_train.row(2).format(CleanFmt) <<endl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <Eigen/Dense> | ||
|
||
|
||
using namespace std; | ||
using namespace Eigen; | ||
|
||
double DReLU(double x) | ||
{ | ||
if (x > 0.0){ | ||
return 1; | ||
} | ||
else{ | ||
return 0; | ||
} | ||
} | ||
|
||
MatrixXd ReLU(MatrixXd X) | ||
{ | ||
MatrixXd X_DReLU = X.unaryExpr(&DReLU); | ||
MatrixXd res = X.cwiseProduct(X_DReLU); | ||
|
||
// MatrixXd res = X_DReLU.array() * X.array(); | ||
// res = res.array().abs(); | ||
return res.cwiseAbs(); | ||
} | ||
|
||
MatrixXd Softmax(MatrixXd X) | ||
{ | ||
MatrixXd res(X.rows(), X.cols()); | ||
MatrixXd X_e = X.array().exp(); | ||
cout << X_e << endl; | ||
VectorXd X_sum= X_e.rowwise().sum(); | ||
cout << X_sum << endl; | ||
|
||
for (int i = 0; i < X.rows(); i++) | ||
{ | ||
res.row(i) = X_e.row(i)/X_sum(i,0); | ||
} | ||
|
||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
cout << "Working " << endl; | ||
MatrixXd X = MatrixXd::Random(3,3); | ||
cout << X << endl; | ||
|
||
VectorXi argmax(X.rows()); | ||
cout << argmax.rows() << "," << argmax.cols() << endl; | ||
for (int i = 0; i < X.rows(); i++){ | ||
// cout << i << endl; | ||
X.row(i).maxCoeff(&argmax[i]); | ||
} | ||
cout << argmax << endl; | ||
|
||
// MatrixXd X_log = X.array().log(); | ||
// cout << X_log << endl; | ||
// MatrixXd X_mult = X.cwiseProduct(X_log); | ||
// cout << X_mult.sum() << endl; | ||
|
||
// MatrixXd X_R = ReLU(X); | ||
// cout << X_R << endl; | ||
|
||
// MatrixXd X_S = Softmax(X); | ||
// cout << X_S<< endl; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.