Tesseract  3.02
tesseract-ocr/neural_networks/runtime/neuron.h
Go to the documentation of this file.
00001 // Copyright 2008 Google Inc.
00002 // All Rights Reserved.
00003 // Author: ahmadab@google.com (Ahmad Abdulkader)
00004 //
00005 // neuron.h: Declarations of a class for an object that
00006 // represents a single neuron in a neural network
00007 //
00008 
00009 #ifndef NEURON_H
00010 #define NEURON_H
00011 
00012 #include <math.h>
00013 #include <vector>
00014 
00015 #ifdef USE_STD_NAMESPACE
00016 using std::vector;
00017 #endif
00018 
00019 namespace tesseract {
00020 
00021 // Input Node bias values
00022 static const float kInputNodeBias = 0.0f;
00023 
00024 class Neuron {
00025   public:
00026     // Types of nodes
00027     enum NeuronTypes {
00028       Unknown = 0,
00029       Input,
00030       Hidden,
00031       Output
00032     };
00033     Neuron();
00034     ~Neuron();
00035     // set the forward dirty flag indicating that the
00036     // activation of the net is not fresh
00037     void Clear() {
00038       frwd_dirty_  =  true;
00039     }
00040     // Read a binary representation of the neuron info from
00041     // an input buffer.
00042     template <class BuffType> bool ReadBinary(BuffType *input_buff) {
00043       float val;
00044       if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
00045         return false;
00046       }
00047       // input nodes should have no biases
00048       if (node_type_ == Input) {
00049         bias_ = kInputNodeBias;
00050       } else {
00051         bias_ = val;
00052       }
00053       // read fanin count
00054       int fan_in_cnt;
00055       if (input_buff->Read(&fan_in_cnt, sizeof(fan_in_cnt)) !=
00056           sizeof(fan_in_cnt)) {
00057         return false;
00058       }
00059       // validate fan-in cnt
00060       if (fan_in_cnt != fan_in_.size()) {
00061         return false;
00062       }
00063       // read the weights
00064       for (int in = 0; in < fan_in_cnt; in++) {
00065         if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
00066           return false;
00067         }
00068         *(fan_in_weights_[in]) = val;
00069       }
00070       return true;
00071     }
00072 
00073     // Add a new connection from this neuron *From*
00074     // a target neuron using specfied params
00075     // Note that what is actually copied in this function are pointers to the
00076     // specified Neurons and weights and not the actualt values. This is by
00077     // design to centralize the alloction of neurons and weights and so
00078     // increase the locality of reference and improve cache-hits resulting
00079     // in a faster net. This technique resulted in a 2X-10X speedup
00080     // (depending on network size and processor)
00081     void AddFromConnection(Neuron *neuron_vec,
00082                            float *wts_offset,
00083                            int from_cnt);
00084     // Set the type of a neuron
00085     void set_node_type(NeuronTypes type);
00086     // Computes the output of the node by
00087     // "pulling" the output of the fan-in nodes
00088     void FeedForward();
00089     // fast computation of sigmoid function using a lookup table
00090     // defined in sigmoid_table.cpp
00091     static float Sigmoid(float activation);
00092     // Accessor functions
00093     float output() const {
00094       return output_;
00095     }
00096     void set_output(float out_val) {
00097       output_ = out_val;
00098     }
00099     int id() const {
00100       return id_;
00101     }
00102     int fan_in_cnt() const {
00103       return fan_in_.size();
00104     }
00105     Neuron * fan_in(int idx) const {
00106       return fan_in_[idx];
00107     }
00108     float fan_in_wts(int idx) const {
00109       return *(fan_in_weights_[idx]);
00110     }
00111     void set_id(int id) {
00112       id_ = id;
00113     }
00114     float bias() const {
00115       return bias_;
00116     }
00117     Neuron::NeuronTypes node_type() const {
00118       return node_type_;
00119     }
00120 
00121   protected:
00122     // Type of Neuron
00123     NeuronTypes node_type_;
00124     // unqique id of the neuron
00125     int id_;
00126     // node bias
00127     float bias_;
00128     // node net activation
00129     float activation_;
00130     // node output
00131     float output_;
00132     // pointers to fanin nodes
00133     vector<Neuron *> fan_in_;
00134     // pointers to fanin weights
00135     vector<float *> fan_in_weights_;
00136     // Sigmoid function lookup table used for fast computation
00137     // of sigmoid function
00138     static const float kSigmoidTable[];
00139     // flag determining if the activation of the node
00140     // is fresh or not (dirty)
00141     bool frwd_dirty_;
00142     // Initializer
00143     void Init();
00144 };
00145 }
00146 
00147 #endif  // NEURON_H__