jacobian

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 9adbe6c93d1dcb5d1a91d11cf7d36943a4dec76c
parent a3114188ddc6ff1e853b331730bb7b3bad85eb0f
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Tue, 14 Jul 2020 20:33:29 -0700

Attempt at using matrix functions reveals they are slow

Diffstat:
Msrc/bpnn.cpp | 3+++
Atests/activations.cpp | 32++++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/src/bpnn.cpp b/src/bpnn.cpp @@ -9,6 +9,7 @@ #include "utils.hpp" #include <ctime> #include <random> +#include <Eigen/MatrixFunctions> #define SHUFFLED_PATH "./shuffled.txt" #define TEST_PATH "./test.txt" @@ -146,6 +147,7 @@ void Network::set_activation(int index, std::function<float(float)> custom, std: void Network::feedforward() { for (int j = 0; j < layers[0].contents->rows(); j++) { + if (strcmp(layers[0].activation_str, "linear") == 0) break; for (int k = 0; k < layers[0].contents->cols(); k++) { (*layers[0].dZ)(j,k) = layers[0].activation_deriv((*layers[0].contents)(j,k)); (*layers[0].contents)(j,k) = layers[0].activation((*layers[0].contents)(j,k)); @@ -160,6 +162,7 @@ void Network::feedforward() } for (int i = 1; i < length; i++) { for (int j = 0; j < layers[i].contents->rows(); j++) { + if (strcmp(layers[i].activation_str, "linear") == 0) break; for (int k = 0; k < layers[i].contents->cols(); k++) { (*layers[i].dZ)(j,k) = layers[i].activation_deriv((*layers[i].contents)(j,k)); (*layers[i].contents)(j,k) = layers[i].activation((*layers[i].contents)(j,k)); diff --git a/tests/activations.cpp b/tests/activations.cpp @@ -0,0 +1,32 @@ +#include <Eigen/Dense> +#include <Eigen/MatrixFunctions> + +#include <iostream> + +std::complex<float> lecun_tanh(std::complex<float> x, int) {return (float)1.7159 * tanh(((float)2.0/3) * x);} +//std::complex<float> lecun_tanh_deriv(std::complex<float> x, int) {return 1.14393 * pow(1.0/cosh(2.0/3 * x),2);} + +std::complex<float> mat_sigmoid(std::complex<float> x, int) {return (float)1.0/((float)1+exp(-x));} +float sigmoid(float x) {return 1.0/(1+exp(-x));} + +int main() +{ + int size; + std::cin >> size; + Eigen::MatrixXf m = Eigen::MatrixXf::Random(size,size); + auto mat_start = std::chrono::high_resolution_clock::now(); + m = m.matrixFunction(mat_sigmoid); + auto mat_end = std::chrono::high_resolution_clock::now(); + Eigen::MatrixXf m2 = Eigen::MatrixXf::Random(size,size); + auto start = std::chrono::high_resolution_clock::now(); + for (int j = 0; j < m2.rows(); j++) { + for (int k = 0; k < m2.cols(); k++) { + m2(j,k) = sigmoid(m2(j,k)); + } + } + auto end = std::chrono::high_resolution_clock::now(); + std::cout << "MATRIX: " << std::chrono::duration_cast<std::chrono::nanoseconds>(mat_end - mat_start).count() << " NORMAL: " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() << "\n"; + +} + +