Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Created December 14, 2018 16:24
Show Gist options
  • Save fearofcode/7f8c714909e50432b44ed71d0641244a to your computer and use it in GitHub Desktop.
Save fearofcode/7f8c714909e50432b44ed71d0641244a to your computer and use it in GitHub Desktop.
single artificial neuron with eigen
#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