All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::Neuron Class Reference

#include <neuron.h>

Public Types

enum  NeuronTypes { Unknown = 0, Input, Hidden, Output }
 

Public Member Functions

 Neuron ()
 
 ~Neuron ()
 
void Clear ()
 
template<class BuffType >
bool ReadBinary (BuffType *input_buff)
 
void AddFromConnection (Neuron *neuron_vec, float *wts_offset, int from_cnt)
 
void set_node_type (NeuronTypes type)
 
void FeedForward ()
 
float output () const
 
void set_output (float out_val)
 
int id () const
 
int fan_in_cnt () const
 
Neuronfan_in (int idx) const
 
float fan_in_wts (int idx) const
 
void set_id (int id)
 
float bias () const
 
Neuron::NeuronTypes node_type () const
 

Static Public Member Functions

static float Sigmoid (float activation)
 

Protected Member Functions

void Init ()
 

Protected Attributes

NeuronTypes node_type_
 
int id_
 
float bias_
 
float activation_
 
float output_
 
vector< Neuron * > fan_in_
 
vector< float * > fan_in_weights_
 
bool frwd_dirty_
 

Static Protected Attributes

static const float kSigmoidTable []
 

Detailed Description

Definition at line 24 of file neuron.h.

Member Enumeration Documentation

Enumerator
Unknown 
Input 
Hidden 
Output 

Definition at line 27 of file neuron.h.

Constructor & Destructor Documentation

tesseract::Neuron::Neuron ( )

Definition at line 17 of file neuron.cpp.

17  {
18  Init();
19 }
tesseract::Neuron::~Neuron ( )

Definition at line 22 of file neuron.cpp.

22  {
23 }

Member Function Documentation

void tesseract::Neuron::AddFromConnection ( Neuron neuron_vec,
float *  wts_offset,
int  from_cnt 
)

Definition at line 74 of file neuron.cpp.

76  {
77  for (int in = 0; in < from_cnt; in++) {
78  fan_in_.push_back(neurons + in);
79  fan_in_weights_.push_back(wts_offset + in);
80  }
81 }
vector< float * > fan_in_weights_
Definition: neuron.h:135
vector< Neuron * > fan_in_
Definition: neuron.h:133
float tesseract::Neuron::bias ( ) const
inline

Definition at line 114 of file neuron.h.

114  {
115  return bias_;
116  }
void tesseract::Neuron::Clear ( )
inline

Definition at line 37 of file neuron.h.

37  {
38  frwd_dirty_ = true;
39  }
bool frwd_dirty_
Definition: neuron.h:141
Neuron* tesseract::Neuron::fan_in ( int  idx) const
inline

Definition at line 105 of file neuron.h.

105  {
106  return fan_in_[idx];
107  }
vector< Neuron * > fan_in_
Definition: neuron.h:133
int tesseract::Neuron::fan_in_cnt ( ) const
inline

Definition at line 102 of file neuron.h.

102  {
103  return fan_in_.size();
104  }
vector< Neuron * > fan_in_
Definition: neuron.h:133
float tesseract::Neuron::fan_in_wts ( int  idx) const
inline

Definition at line 108 of file neuron.h.

108  {
109  return *(fan_in_weights_[idx]);
110  }
vector< float * > fan_in_weights_
Definition: neuron.h:135
void tesseract::Neuron::FeedForward ( )

Definition at line 39 of file neuron.cpp.

39  {
40  if (!frwd_dirty_ ) {
41  return;
42  }
43  // nothing to do for input nodes: just pass the input to the o/p
44  // otherwise, pull the output of all fan-in neurons
45  if (node_type_ != Input) {
46  int fan_in_cnt = fan_in_.size();
47  // sum out the activation
48  activation_ = -bias_;
49  for (int in = 0; in < fan_in_cnt; in++) {
50  if (fan_in_[in]->frwd_dirty_) {
51  fan_in_[in]->FeedForward();
52  }
53  activation_ += ((*(fan_in_weights_[in])) * fan_in_[in]->output_);
54  }
55  // sigmoid it
57  }
58  frwd_dirty_ = false;
59 }
bool frwd_dirty_
Definition: neuron.h:141
vector< float * > fan_in_weights_
Definition: neuron.h:135
float activation_
Definition: neuron.h:129
vector< Neuron * > fan_in_
Definition: neuron.h:133
static float Sigmoid(float activation)
Definition: neuron.cpp:85
int fan_in_cnt() const
Definition: neuron.h:102
NeuronTypes node_type_
Definition: neuron.h:123
int tesseract::Neuron::id ( ) const
inline

Definition at line 99 of file neuron.h.

99  {
100  return id_;
101  }
void tesseract::Neuron::Init ( )
protected

Definition at line 26 of file neuron.cpp.

26  {
27  id_ = -1;
28  frwd_dirty_ = false;
29  fan_in_.clear();
30  fan_in_weights_.clear();
31  activation_ = 0.0f;
32  output_ = 0.0f;
33  bias_ = 0.0f;
35 }
bool frwd_dirty_
Definition: neuron.h:141
vector< float * > fan_in_weights_
Definition: neuron.h:135
float activation_
Definition: neuron.h:129
vector< Neuron * > fan_in_
Definition: neuron.h:133
NeuronTypes node_type_
Definition: neuron.h:123
Neuron::NeuronTypes tesseract::Neuron::node_type ( ) const
inline

Definition at line 117 of file neuron.h.

117  {
118  return node_type_;
119  }
NeuronTypes node_type_
Definition: neuron.h:123
float tesseract::Neuron::output ( ) const
inline

Definition at line 93 of file neuron.h.

93  {
94  return output_;
95  }
template<class BuffType >
template bool tesseract::Neuron::ReadBinary ( BuffType *  input_buff)
inline

Definition at line 42 of file neuron.h.

42  {
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  }
vector< float * > fan_in_weights_
Definition: neuron.h:135
vector< Neuron * > fan_in_
Definition: neuron.h:133
int fan_in_cnt() const
Definition: neuron.h:102
NeuronTypes node_type_
Definition: neuron.h:123
void tesseract::Neuron::set_id ( int  id)
inline

Definition at line 111 of file neuron.h.

111  {
112  id_ = id;
113  }
int id() const
Definition: neuron.h:99
void tesseract::Neuron::set_node_type ( NeuronTypes  type)

Definition at line 62 of file neuron.cpp.

62  {
63  node_type_ = Type;
64 }
NeuronTypes node_type_
Definition: neuron.h:123
void tesseract::Neuron::set_output ( float  out_val)
inline

Definition at line 96 of file neuron.h.

96  {
97  output_ = out_val;
98  }
float tesseract::Neuron::Sigmoid ( float  activation)
static

Definition at line 85 of file neuron.cpp.

85  {
86  if (activation <= -10.0f) {
87  return 0.0f;
88  } else if (activation >= 10.0f) {
89  return 1.0f;
90  } else {
91  return kSigmoidTable[static_cast<int>(100 * (activation + 10.0))];
92  }
93 }
static const float kSigmoidTable[]
Definition: neuron.h:138

Member Data Documentation

float tesseract::Neuron::activation_
protected

Definition at line 129 of file neuron.h.

float tesseract::Neuron::bias_
protected

Definition at line 127 of file neuron.h.

vector<Neuron *> tesseract::Neuron::fan_in_
protected

Definition at line 133 of file neuron.h.

vector<float *> tesseract::Neuron::fan_in_weights_
protected

Definition at line 135 of file neuron.h.

bool tesseract::Neuron::frwd_dirty_
protected

Definition at line 141 of file neuron.h.

int tesseract::Neuron::id_
protected

Definition at line 125 of file neuron.h.

const float tesseract::Neuron::kSigmoidTable
staticprotected

Definition at line 138 of file neuron.h.

NeuronTypes tesseract::Neuron::node_type_
protected

Definition at line 123 of file neuron.h.

float tesseract::Neuron::output_
protected

Definition at line 131 of file neuron.h.


The documentation for this class was generated from the following files: