forked from cryptixcoder/neural-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron.h
79 lines (65 loc) · 1.69 KB
/
Neuron.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
class Neuron {
private:
int numInputs;
std::vector<double>* weights;
double delta;
double activation;
double value;
public:
Neuron() {}
Neuron(int inputs) {
numInputs = inputs;
// There is an extra weight for the bias input.
weights = new std::vector<double>(numInputs + 1);
// Setup weights with an initial random value between -1 and 1. There is
// one weight for each input and an additional bias weight.
for (int i = 0; i < weights->size(); i++) {
(*weights)[i] = 10 * (((double)rand() / (double)RAND_MAX) * 2 - 1);
}
}
~Neuron() {
delete weights;
}
// Get the corresponding weight.
double getWeight(int n) const {
return (*weights)[n];
}
// Add an update value to a specified input weight.
void updateWeight(int pos, double update) {
(*weights)[pos] += update;
}
// Get the linear combination of inputs to the neuron.
double getActivation() const {
return activation;
}
// Get the value of the neuron (sigmoid applied to the activation).
double getValue() const {
return value;
}
// Set the value of the neuron.
void setValue(double v) {
value = v;
}
// Get the delta value for this neuron.
double getDelta() const {
return delta;
}
// Set the delta value for this neuron.
void setDelta(double new_delta) {
delta = new_delta;
}
// Compute and set the linear combination of inputs to the neuron.
void setActivation(double a) {
activation = a;
}
double printWeights() {
for (int i = 0; i < weights->size()-1; i++) {
std::cout << (*weights)[i] << " ";
}
std::cout << "\n";
}
};