Created
December 14, 2018 16:24
-
-
Save fearofcode/7f8c714909e50432b44ed71d0641244a to your computer and use it in GitHub Desktop.
single artificial neuron with eigen
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
#include <iostream> | |
#include <random> | |
#include <cmath> | |
#include <Eigen/Core> | |
#include "pcg_random.hpp" | |
using namespace Eigen; | |
template<unsigned int Size> | |
struct neuron { | |
VectorXd weights; | |
double bias; | |
neuron() { | |
pcg_extras::seed_seq_from<std::random_device> seed_source; | |
pcg32 rng(seed_source); | |
std::uniform_real_distribution<> real_dist(-0.5, 0.5); | |
weights = VectorXd(Size); | |
for (int i = 0; i < Size; i++) { | |
weights[i] = real_dist(rng); | |
} | |
bias = real_dist(rng); | |
std::cout << "weights = " << weights << std::endl; | |
std::cout << "bias = " << bias << std::endl; | |
} | |
double sense(VectorXd input) { | |
return tanh(weights.dot(input) + bias); | |
} | |
}; | |
int main() | |
{ | |
neuron<2> n; | |
VectorXd input(2); | |
input << 1.0, 2.0; | |
std::cout << n.sense(input) << '\n'; | |
std::cin.get(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment