Skip to content

Instantly share code, notes, and snippets.

@olooney
Created May 17, 2018 23:14
Show Gist options
  • Save olooney/e9795d1c9e60e81b2672cebc29cc0b69 to your computer and use it in GitHub Desktop.
Save olooney/e9795d1c9e60e81b2672cebc29cc0b69 to your computer and use it in GitHub Desktop.
//g++ -std=c++17 -O3 -I eigen main.cpp -o main && time ./main
//g++ -std=c++17 -fopenmp -O3 -I eigen main.cpp -o main && time ./main
//g++ -std=c++17 -pg -fopenmp -O3 -I eigen main.cpp -o main && time ./main && gprof main gmon.out > profile.txt
#include <iostream>
#include <Eigen/Dense>
#include <cmath>
using namespace Eigen;
float logistic(float x) {
return 1.0 / (1.0 + std::exp(-x));
}
void nn_test() {
//Eigen::setNbThreads(10);
//std::cout << "Eigen is using " << Eigen::nbThreads() << " threads." << std::endl;
int n = 252*100;
std::cout << "\nforward application of neural network: " << n << " trials" << std::endl;
auto layer1 = MatrixXf::Random(128, 588); // 588 is the number of columns in PDI variables file
auto layer2 = MatrixXf::Random(32, 128);
auto layer_out = MatrixXf::Random(1, 32);
auto input = Matrix<float, 588, 1>::Random();
for ( int i=1; i<=n; i++ ) {
auto activation = (layer_out * (layer2 * (layer1 * input).unaryExpr(&logistic)).unaryExpr(&logistic)).unaryExpr(&logistic);
if ( i > 1 and i % (n/10) == 0 or activation(0,0) == 0.2 ) {
std::cout << i << "th score: " << activation(0,0) << std::endl;
}
}
}
void basic_tests() {
//MatrixXd m(2,2);
Matrix2f m(2,2);
m(0,0) = 2;
m(1,0) = 0.5;
m(0,1) = m(1,0);
m(1,1) = 1;
std::cout << "Original:" << std::endl;
std::cout << m << std::endl;
std::cout << std::endl;
Matrix2f mi = m.inverse();
std::cout << "Inverse:" << std::endl;
std::cout << mi << std::endl;
std::cout << std::endl;
ComplexEigenSolver<Matrix2f> e(m);
if ( e.info() == Success ) {
std::cout << "Eigenvalues:\n" << e.eigenvalues() << std::endl;
std::cout << "\nEigenvector matrix:\n" << e.eigenvectors() << std::endl;
} else {
std::cout << "failed to solve the eigen-problem" << std::endl;
}
}
int main(int argc, char** argv) {
nn_test();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment