tesseract  3.05.02
neuron.cpp
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.cpp: The implementation of a class for an object
6 // that represents a single neuron in a neural network
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "neuron.h"
18 #include "input_file_buffer.h"
19 
20 namespace tesseract {
21 
22 // Instantiate all supported templates
23 template bool Neuron::ReadBinary(InputFileBuffer *input_buffer);
24 
25 // default and only constructor
27  Init();
28 }
29 
30 // virtual destructor
32 }
33 
34 // Initializer
35 void Neuron::Init() {
36  id_ = -1;
37  frwd_dirty_ = false;
38  fan_in_.clear();
39  fan_in_weights_.clear();
40  activation_ = 0.0f;
41  output_ = 0.0f;
42  bias_ = 0.0f;
44 }
45 
46 // Computes the activation and output of the neuron if not fresh
47 // by pulling the outputs of all fan-in neurons
49  if (!frwd_dirty_ ) {
50  return;
51  }
52  // nothing to do for input nodes: just pass the input to the o/p
53  // otherwise, pull the output of all fan-in neurons
54  if (node_type_ != Input) {
55  int fan_in_cnt = fan_in_.size();
56  // sum out the activation
57  activation_ = -bias_;
58  for (int in = 0; in < fan_in_cnt; in++) {
59  if (fan_in_[in]->frwd_dirty_) {
60  fan_in_[in]->FeedForward();
61  }
62  activation_ += ((*(fan_in_weights_[in])) * fan_in_[in]->output_);
63  }
64  // sigmoid it
66  }
67  frwd_dirty_ = false;
68 }
69 
70 // set the type of the neuron
72  node_type_ = Type;
73 }
74 
75 // Adds new connections *to* this neuron *From*
76 // a target neuron using specfied params
77 // Note that what is actually copied in this function are pointers to the
78 // specified Neurons and weights and not the actualt values. This is by
79 // design to centralize the alloction of neurons and weights and so
80 // increase the locality of reference and improve cache-hits resulting
81 // in a faster net. This technique resulted in a 2X-10X speedup
82 // (depending on network size and processor)
84  float *wts_offset,
85  int from_cnt) {
86  for (int in = 0; in < from_cnt; in++) {
87  fan_in_.push_back(neurons + in);
88  fan_in_weights_.push_back(wts_offset + in);
89  }
90 }
91 
92 // fast computation of sigmoid function using a lookup table
93 // defined in sigmoid_table.cpp
94 float Neuron::Sigmoid(float activation) {
95  if (activation <= -10.0f) {
96  return 0.0f;
97  } else if (activation >= 10.0f) {
98  return 1.0f;
99  } else {
100  return kSigmoidTable[static_cast<int>(100 * (activation + 10.0))];
101  }
102 }
103 }
vector< float * > fan_in_weights_
Definition: neuron.h:144
void set_node_type(NeuronTypes type)
Definition: neuron.cpp:71
static const float kSigmoidTable[]
Definition: neuron.h:147
int fan_in_cnt() const
Definition: neuron.h:111
bool frwd_dirty_
Definition: neuron.h:150
vector< Neuron * > fan_in_
Definition: neuron.h:142
void AddFromConnection(Neuron *neuron_vec, float *wts_offset, int from_cnt)
Definition: neuron.cpp:83
bool ReadBinary(BuffType *input_buff)
Definition: neuron.h:51
void FeedForward()
Definition: neuron.cpp:48
NeuronTypes node_type_
Definition: neuron.h:132
static float Sigmoid(float activation)
Definition: neuron.cpp:94
float activation_
Definition: neuron.h:138