tesseract  3.05.02
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 33 of file neuron.h.

Member Enumeration Documentation

◆ NeuronTypes

Enumerator
Unknown 
Input 
Hidden 
Output 

Definition at line 36 of file neuron.h.

Constructor & Destructor Documentation

◆ Neuron()

tesseract::Neuron::Neuron ( )

Definition at line 26 of file neuron.cpp.

26  {
27  Init();
28 }

◆ ~Neuron()

tesseract::Neuron::~Neuron ( )

Definition at line 31 of file neuron.cpp.

31  {
32 }

Member Function Documentation

◆ AddFromConnection()

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

Definition at line 83 of file neuron.cpp.

85  {
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 }
vector< float * > fan_in_weights_
Definition: neuron.h:144
vector< Neuron * > fan_in_
Definition: neuron.h:142

◆ bias()

float tesseract::Neuron::bias ( ) const
inline

Definition at line 123 of file neuron.h.

123  {
124  return bias_;
125  }

◆ Clear()

void tesseract::Neuron::Clear ( )
inline

Definition at line 46 of file neuron.h.

46  {
47  frwd_dirty_ = true;
48  }
bool frwd_dirty_
Definition: neuron.h:150

◆ fan_in()

Neuron* tesseract::Neuron::fan_in ( int  idx) const
inline

Definition at line 114 of file neuron.h.

114  {
115  return fan_in_[idx];
116  }
vector< Neuron * > fan_in_
Definition: neuron.h:142

◆ fan_in_cnt()

int tesseract::Neuron::fan_in_cnt ( ) const
inline

Definition at line 111 of file neuron.h.

111  {
112  return fan_in_.size();
113  }
vector< Neuron * > fan_in_
Definition: neuron.h:142

◆ fan_in_wts()

float tesseract::Neuron::fan_in_wts ( int  idx) const
inline

Definition at line 117 of file neuron.h.

117  {
118  return *(fan_in_weights_[idx]);
119  }
vector< float * > fan_in_weights_
Definition: neuron.h:144

◆ FeedForward()

void tesseract::Neuron::FeedForward ( )

Definition at line 48 of file neuron.cpp.

48  {
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 }
vector< float * > fan_in_weights_
Definition: neuron.h:144
int fan_in_cnt() const
Definition: neuron.h:111
bool frwd_dirty_
Definition: neuron.h:150
vector< Neuron * > fan_in_
Definition: neuron.h:142
NeuronTypes node_type_
Definition: neuron.h:132
static float Sigmoid(float activation)
Definition: neuron.cpp:94
float activation_
Definition: neuron.h:138

◆ id()

int tesseract::Neuron::id ( ) const
inline

Definition at line 108 of file neuron.h.

108  {
109  return id_;
110  }

◆ Init()

void tesseract::Neuron::Init ( )
protected

Definition at line 35 of file neuron.cpp.

35  {
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 }
vector< float * > fan_in_weights_
Definition: neuron.h:144
bool frwd_dirty_
Definition: neuron.h:150
vector< Neuron * > fan_in_
Definition: neuron.h:142
NeuronTypes node_type_
Definition: neuron.h:132
float activation_
Definition: neuron.h:138

◆ node_type()

Neuron::NeuronTypes tesseract::Neuron::node_type ( ) const
inline

Definition at line 126 of file neuron.h.

126  {
127  return node_type_;
128  }
NeuronTypes node_type_
Definition: neuron.h:132

◆ output()

float tesseract::Neuron::output ( ) const
inline

Definition at line 102 of file neuron.h.

102  {
103  return output_;
104  }

◆ ReadBinary()

template<class BuffType >
template bool tesseract::Neuron::ReadBinary ( BuffType *  input_buff)
inline

Definition at line 51 of file neuron.h.

51  {
52  float val;
53  if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
54  return false;
55  }
56  // input nodes should have no biases
57  if (node_type_ == Input) {
58  bias_ = kInputNodeBias;
59  } else {
60  bias_ = val;
61  }
62  // read fanin count
63  int fan_in_cnt;
64  if (input_buff->Read(&fan_in_cnt, sizeof(fan_in_cnt)) !=
65  sizeof(fan_in_cnt)) {
66  return false;
67  }
68  // validate fan-in cnt
69  if (fan_in_cnt != fan_in_.size()) {
70  return false;
71  }
72  // read the weights
73  for (int in = 0; in < fan_in_cnt; in++) {
74  if (input_buff->Read(&val, sizeof(val)) != sizeof(val)) {
75  return false;
76  }
77  *(fan_in_weights_[in]) = val;
78  }
79  return true;
80  }
vector< float * > fan_in_weights_
Definition: neuron.h:144
int fan_in_cnt() const
Definition: neuron.h:111
vector< Neuron * > fan_in_
Definition: neuron.h:142
NeuronTypes node_type_
Definition: neuron.h:132

◆ set_id()

void tesseract::Neuron::set_id ( int  id)
inline

Definition at line 120 of file neuron.h.

120  {
121  id_ = id;
122  }
int id() const
Definition: neuron.h:108

◆ set_node_type()

void tesseract::Neuron::set_node_type ( NeuronTypes  type)

Definition at line 71 of file neuron.cpp.

71  {
72  node_type_ = Type;
73 }
NeuronTypes node_type_
Definition: neuron.h:132

◆ set_output()

void tesseract::Neuron::set_output ( float  out_val)
inline

Definition at line 105 of file neuron.h.

105  {
106  output_ = out_val;
107  }

◆ Sigmoid()

float tesseract::Neuron::Sigmoid ( float  activation)
static

Definition at line 94 of file neuron.cpp.

94  {
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 }
static const float kSigmoidTable[]
Definition: neuron.h:147

Member Data Documentation

◆ activation_

float tesseract::Neuron::activation_
protected

Definition at line 138 of file neuron.h.

◆ bias_

float tesseract::Neuron::bias_
protected

Definition at line 136 of file neuron.h.

◆ fan_in_

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

Definition at line 142 of file neuron.h.

◆ fan_in_weights_

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

Definition at line 144 of file neuron.h.

◆ frwd_dirty_

bool tesseract::Neuron::frwd_dirty_
protected

Definition at line 150 of file neuron.h.

◆ id_

int tesseract::Neuron::id_
protected

Definition at line 134 of file neuron.h.

◆ kSigmoidTable

const float tesseract::Neuron::kSigmoidTable
staticprotected

Definition at line 147 of file neuron.h.

◆ node_type_

NeuronTypes tesseract::Neuron::node_type_
protected

Definition at line 132 of file neuron.h.

◆ output_

float tesseract::Neuron::output_
protected

Definition at line 140 of file neuron.h.


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