neuralpp/src/neuron.cpp

91 lines
2.3 KiB
C++
Raw Normal View History

2009-02-18 00:10:57 +01:00
/**************************************************************************************************
* LibNeural++ v.0.2 - All-purpose library for managing neural networks *
* Copyright (C) 2009, BlackLight *
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the *
* GNU General Public License as published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for *
* more details. You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************************************/
#include "neural++.hpp"
2009-02-18 00:10:57 +01:00
2009-08-16 11:09:42 +02:00
using std::vector;
2009-08-09 11:17:39 +02:00
namespace neuralpp {
2009-08-16 18:02:24 +02:00
Neuron::Neuron(double (*a) (double), double th) {
2009-08-09 11:17:39 +02:00
actv_f = a;
2009-08-16 18:02:24 +02:00
threshold = th;
2009-08-09 11:17:39 +02:00
}
Neuron::Neuron(vector < Synapsis > i, vector < Synapsis > o,
2009-08-16 18:02:24 +02:00
double (*a) (double), double th) {
2009-08-09 11:17:39 +02:00
in = i;
out = o;
actv_f = a;
2009-08-16 18:02:24 +02:00
threshold = th;
2009-08-09 11:17:39 +02:00
}
Synapsis & Neuron::synIn(size_t i) {
return in[i];
}
Synapsis & Neuron::synOut(size_t i) {
return out[i];
}
2009-08-16 11:09:42 +02:00
void Neuron::push_in(Synapsis s) {
2009-08-09 11:17:39 +02:00
in.push_back(s);
}
2009-08-16 11:09:42 +02:00
void Neuron::push_out(Synapsis s) {
2009-08-09 11:17:39 +02:00
out.push_back(s);
}
void Neuron::setProp(double val) {
prop_val = val;
}
void Neuron::setActv(double val) {
2009-08-16 18:02:24 +02:00
//actv_val = actv_f(val);
actv_val = val;
2009-08-09 11:17:39 +02:00
}
size_t Neuron::nIn() {
return in.size();
}
size_t Neuron::nOut() {
return out.size();
}
double Neuron::getProp() {
return prop_val;
}
double Neuron::getActv() {
return actv_val;
}
2009-08-16 11:09:42 +02:00
void Neuron::propagate() {
double aux = 0.0;
2009-08-09 11:17:39 +02:00
for (size_t i = 0; i < nIn(); i++)
2009-08-16 18:02:24 +02:00
aux += (in[i].getWeight() * in[i].getIn()->actv_val);
2009-08-16 11:09:42 +02:00
2009-08-16 19:25:58 +02:00
aux -= threshold;
2009-08-16 11:09:42 +02:00
setProp(aux);
2009-08-16 18:02:24 +02:00
setActv( actv_f(aux) );
2009-08-09 11:17:39 +02:00
}
void Neuron::synClear() {
in.clear();
out.clear();
}
2009-08-09 10:24:52 +02:00
}
2009-02-18 00:10:57 +01:00