// neuron.cpp // David R. Morrison #include "neuron.hpp" // Constructor Neuron::Neuron(int input_vector_size) { inputVectorSize = input_vector_size; // Randomize the weights; add one to factor in bias weights = getRandomWeights(inputVectorSize + 1); } // Just do a weighted sum of the inputs double Neuron::evaluateSample(vector inputs) { assert (inputs.size() == inputVectorSize); // The first one is the bias -- always has value of 1.0 double sum = weights[0]; // Sum up the rest, starting at 1 for (int j = 1; j < inputVectorSize + 1; ++j) sum += weights[j] * inputs[j - 1]; return sum; } // Get the weight at the specified index double Neuron::getWeight(unsigned int i) { assert(i < inputVectorSize + 1); return weights[i]; } // Change the weight at the specified index by delta void Neuron::changeWeight(unsigned int i, double delta) { assert(i < inputVectorSize + 1); weights[i] += delta; } ostream& Neuron::print(ostream& out) { out.precision(4); out << "[ "; for (int i = 0; i < weights.size(); ++i) out << weights[i] << " "; out << "]" << endl; } ostream& operator<<(ostream& out, Neuron& n) { return n.print(out); }