All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
neuron.h
Go to the documentation of this file.
1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
3 // Author: ahmadab@google.com (Ahmad Abdulkader)
4 //
5 // neuron.h: Declarations of a class for an object that
6 // represents a single neuron in a neural network
7 //
8 
9 #ifndef NEURON_H
10 #define NEURON_H
11 
12 #include <math.h>
13 #include <vector>
14 
15 #ifdef USE_STD_NAMESPACE
16 using std::vector;
17 #endif
18 
19 namespace tesseract {
20 
21 // Input Node bias values
22 static const float kInputNodeBias = 0.0f;
23 
24 class Neuron {
25  public:
26  // Types of nodes
27  enum NeuronTypes {
28  Unknown = 0,
32  };
33  Neuron();
34  ~Neuron();
35  // set the forward dirty flag indicating that the
36  // activation of the net is not fresh
37  void Clear() {
38  frwd_dirty_ = true;
39  }
40  // Read a binary representation of the neuron info from
41  // an input buffer.
42  template <class BuffType> bool ReadBinary(BuffType *input_buff) {
43  float val;
44  if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
45  return false;
46  }
47  // input nodes should have no biases
48  if (node_type_ == Input) {
49  bias_ = kInputNodeBias;
50  } else {
51  bias_ = val;
52  }
53  // read fanin count
54  int fan_in_cnt;
55  if (input_buff->Read(&fan_in_cnt, sizeof(fan_in_cnt)) !=
56  sizeof(fan_in_cnt)) {
57  return false;
58  }
59  // validate fan-in cnt
60  if (fan_in_cnt != fan_in_.size()) {
61  return false;
62  }
63  // read the weights
64  for (int in = 0; in < fan_in_cnt; in++) {
65  if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
66  return false;
67  }
68  *(fan_in_weights_[in]) = val;
69  }
70  return true;
71  }
72 
73  // Add a new connection from this neuron *From*
74  // a target neuron using specfied params
75  // Note that what is actually copied in this function are pointers to the
76  // specified Neurons and weights and not the actualt values. This is by
77  // design to centralize the alloction of neurons and weights and so
78  // increase the locality of reference and improve cache-hits resulting
79  // in a faster net. This technique resulted in a 2X-10X speedup
80  // (depending on network size and processor)
81  void AddFromConnection(Neuron *neuron_vec,
82  float *wts_offset,
83  int from_cnt);
84  // Set the type of a neuron
85  void set_node_type(NeuronTypes type);
86  // Computes the output of the node by
87  // "pulling" the output of the fan-in nodes
88  void FeedForward();
89  // fast computation of sigmoid function using a lookup table
90  // defined in sigmoid_table.cpp
91  static float Sigmoid(float activation);
92  // Accessor functions
93  float output() const {
94  return output_;
95  }
96  void set_output(float out_val) {
97  output_ = out_val;
98  }
99  int id() const {
100  return id_;
101  }
102  int fan_in_cnt() const {
103  return fan_in_.size();
104  }
105  Neuron * fan_in(int idx) const {
106  return fan_in_[idx];
107  }
108  float fan_in_wts(int idx) const {
109  return *(fan_in_weights_[idx]);
110  }
111  void set_id(int id) {
112  id_ = id;
113  }
114  float bias() const {
115  return bias_;
116  }
118  return node_type_;
119  }
120 
121  protected:
122  // Type of Neuron
124  // unqique id of the neuron
125  int id_;
126  // node bias
127  float bias_;
128  // node net activation
129  float activation_;
130  // node output
131  float output_;
132  // pointers to fanin nodes
133  vector<Neuron *> fan_in_;
134  // pointers to fanin weights
135  vector<float *> fan_in_weights_;
136  // Sigmoid function lookup table used for fast computation
137  // of sigmoid function
138  static const float kSigmoidTable[];
139  // flag determining if the activation of the node
140  // is fresh or not (dirty)
142  // Initializer
143  void Init();
144 };
145 }
146 
147 #endif // NEURON_H__
void AddFromConnection(Neuron *neuron_vec, float *wts_offset, int from_cnt)
Definition: neuron.cpp:74
static const float kSigmoidTable[]
Definition: neuron.h:138
float fan_in_wts(int idx) const
Definition: neuron.h:108
void set_output(float out_val)
Definition: neuron.h:96
int id() const
Definition: neuron.h:99
bool frwd_dirty_
Definition: neuron.h:141
vector< float * > fan_in_weights_
Definition: neuron.h:135
Neuron::NeuronTypes node_type() const
Definition: neuron.h:117
Neuron * fan_in(int idx) const
Definition: neuron.h:105
float activation_
Definition: neuron.h:129
float bias() const
Definition: neuron.h:114
bool ReadBinary(BuffType *input_buff)
Definition: neuron.h:42
float output() const
Definition: neuron.h:93
vector< Neuron * > fan_in_
Definition: neuron.h:133
static float Sigmoid(float activation)
Definition: neuron.cpp:85
void Clear()
Definition: neuron.h:37
int fan_in_cnt() const
Definition: neuron.h:102
void set_id(int id)
Definition: neuron.h:111
void set_node_type(NeuronTypes type)
Definition: neuron.cpp:62
NeuronTypes node_type_
Definition: neuron.h:123
void FeedForward()
Definition: neuron.cpp:39