tesseract  4.0.0-1-g2a2b
tesseract::Plumbing Class Reference

#include <plumbing.h>

Inheritance diagram for tesseract::Plumbing:
tesseract::Network tesseract::Parallel tesseract::Reversed tesseract::Series

Public Member Functions

 Plumbing (const STRING &name)
 
virtual ~Plumbing ()=default
 
StaticShape InputShape () const override
 
STRING spec () const override
 
bool IsPlumbingType () const override
 
void SetEnableTraining (TrainingState state) override
 
void SetNetworkFlags (uint32_t flags) override
 
int InitWeights (float range, TRand *randomizer) override
 
int RemapOutputs (int old_no, const std::vector< int > &code_map) override
 
void ConvertToInt () override
 
void SetRandomizer (TRand *randomizer) override
 
virtual void AddToStack (Network *network)
 
bool SetupNeedsBackprop (bool needs_backprop) override
 
int XScaleFactor () const override
 
void CacheXScaleFactor (int factor) override
 
void DebugWeights () override
 
const PointerVector< Network > & stack () const
 
void EnumerateLayers (const STRING *prefix, GenericVector< STRING > *layers) const
 
NetworkGetLayer (const char *id) const
 
float LayerLearningRate (const char *id) const
 
void ScaleLayerLearningRate (const char *id, double factor)
 
float * LayerLearningRatePtr (const char *id) const
 
bool Serialize (TFile *fp) const override
 
bool DeSerialize (TFile *fp) override
 
void Update (float learning_rate, float momentum, float adam_beta, int num_samples) override
 
void CountAlternators (const Network &other, double *same, double *changed) const override
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()=default
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Protected Attributes

PointerVector< Networkstack_
 
GenericVector< float > learning_rates_
 
- Protected Attributes inherited from tesseract::Network
NetworkType type_
 
TrainingState training_
 
bool needs_to_backprop_
 
int32_t network_flags_
 
int32_t ni_
 
int32_t no_
 
int32_t num_weights_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 

Additional Inherited Members

- Static Public Member Functions inherited from tesseract::Network
static NetworkCreateFromFile (TFile *fp)
 
static void ClearWindow (bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
 
static int DisplayImage (Pix *pix, ScrollView *window)
 
- Protected Member Functions inherited from tesseract::Network
double Random (double range)
 
- Static Protected Attributes inherited from tesseract::Network
static char const *const kTypeNames [NT_COUNT]
 

Detailed Description

Definition at line 30 of file plumbing.h.

Constructor & Destructor Documentation

◆ Plumbing()

tesseract::Plumbing::Plumbing ( const STRING name)
explicit

Definition at line 25 of file plumbing.cpp.

26  : Network(NT_PARALLEL, name, 0, 0) {
27 }
const STRING & name() const
Definition: network.h:138

◆ ~Plumbing()

virtual tesseract::Plumbing::~Plumbing ( )
virtualdefault

Member Function Documentation

◆ AddToStack()

void tesseract::Plumbing::AddToStack ( Network network)
virtual

Definition at line 82 of file plumbing.cpp.

82  {
83  if (stack_.empty()) {
84  ni_ = network->NumInputs();
85  no_ = network->NumOutputs();
86  } else if (type_ == NT_SERIES) {
87  // ni is input of first, no output of last, others match output to input.
88  ASSERT_HOST(no_ == network->NumInputs());
89  no_ = network->NumOutputs();
90  } else {
91  // All parallel types. Output is sum of outputs, inputs all match.
92  ASSERT_HOST(ni_ == network->NumInputs());
93  no_ += network->NumOutputs();
94  }
95  stack_.push_back(network);
96 }
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ CacheXScaleFactor()

void tesseract::Plumbing::CacheXScaleFactor ( int  factor)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 126 of file plumbing.cpp.

126  {
127  for (int i = 0; i < stack_.size(); ++i) {
128  stack_[i]->CacheXScaleFactor(factor);
129  }
130 }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ ConvertToInt()

void tesseract::Plumbing::ConvertToInt ( )
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 68 of file plumbing.cpp.

68  {
69  for (int i = 0; i < stack_.size(); ++i)
70  stack_[i]->ConvertToInt();
71 }
void ConvertToInt() override
Definition: plumbing.cpp:68
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ CountAlternators()

void tesseract::Plumbing::CountAlternators ( const Network other,
double *  same,
double *  changed 
) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 234 of file plumbing.cpp.

235  {
236  ASSERT_HOST(other.type() == type_);
237  const Plumbing* plumbing = static_cast<const Plumbing*>(&other);
238  ASSERT_HOST(plumbing->stack_.size() == stack_.size());
239  for (int i = 0; i < stack_.size(); ++i)
240  stack_[i]->CountAlternators(*plumbing->stack_[i], same, changed);
241 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
void CountAlternators(const Network &other, double *same, double *changed) const override
Definition: plumbing.cpp:234
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ DebugWeights()

void tesseract::Plumbing::DebugWeights ( )
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 133 of file plumbing.cpp.

133  {
134  for (int i = 0; i < stack_.size(); ++i)
135  stack_[i]->DebugWeights();
136 }
PointerVector< Network > stack_
Definition: plumbing.h:136
void DebugWeights() override
Definition: plumbing.cpp:133

◆ DeSerialize()

bool tesseract::Plumbing::DeSerialize ( TFile fp)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 197 of file plumbing.cpp.

197  {
198  stack_.truncate(0);
199  no_ = 0; // We will be modifying this as we AddToStack.
200  uint32_t size;
201  if (!fp->DeSerialize(&size)) return false;
202  for (uint32_t i = 0; i < size; ++i) {
203  Network* network = CreateFromFile(fp);
204  if (network == nullptr) return false;
205  AddToStack(network);
206  }
209  return false;
210  }
211  return true;
212 }
static Network * CreateFromFile(TFile *fp)
Definition: network.cpp:199
GenericVector< float > learning_rates_
Definition: plumbing.h:139
bool DeSerialize(bool swap, FILE *fp)
PointerVector< Network > stack_
Definition: plumbing.h:136
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
int32_t network_flags_
Definition: network.h:302

◆ EnumerateLayers()

void tesseract::Plumbing::EnumerateLayers ( const STRING prefix,
GenericVector< STRING > *  layers 
) const

Definition at line 139 of file plumbing.cpp.

140  {
141  for (int i = 0; i < stack_.size(); ++i) {
142  STRING layer_name;
143  if (prefix) layer_name = *prefix;
144  layer_name.add_str_int(":", i);
145  if (stack_[i]->IsPlumbingType()) {
146  Plumbing* plumbing = static_cast<Plumbing*>(stack_[i]);
147  plumbing->EnumerateLayers(&layer_name, layers);
148  } else {
149  layers->push_back(layer_name);
150  }
151  }
152 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
PointerVector< Network > stack_
Definition: plumbing.h:136
bool IsPlumbingType() const override
Definition: plumbing.h:44
int push_back(T object)
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
Definition: strngs.h:45

◆ GetLayer()

Network * tesseract::Plumbing::GetLayer ( const char *  id) const

Definition at line 155 of file plumbing.cpp.

155  {
156  char* next_id;
157  int index = strtol(id, &next_id, 10);
158  if (index < 0 || index >= stack_.size()) return nullptr;
159  if (stack_[index]->IsPlumbingType()) {
160  Plumbing* plumbing = static_cast<Plumbing*>(stack_[index]);
161  ASSERT_HOST(*next_id == ':');
162  return plumbing->GetLayer(next_id + 1);
163  }
164  return stack_[index];
165 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
PointerVector< Network > stack_
Definition: plumbing.h:136
bool IsPlumbingType() const override
Definition: plumbing.h:44
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ InitWeights()

int tesseract::Plumbing::InitWeights ( float  range,
TRand randomizer 
)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 50 of file plumbing.cpp.

50  {
51  num_weights_ = 0;
52  for (int i = 0; i < stack_.size(); ++i)
53  num_weights_ += stack_[i]->InitWeights(range, randomizer);
54  return num_weights_;
55 }
int32_t num_weights_
Definition: network.h:305
int InitWeights(float range, TRand *randomizer) override
Definition: plumbing.cpp:50
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ InputShape()

StaticShape tesseract::Plumbing::InputShape ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 37 of file plumbing.h.

37 { return stack_[0]->InputShape(); }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ IsPlumbingType()

bool tesseract::Plumbing::IsPlumbingType ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 44 of file plumbing.h.

44 { return true; }

◆ LayerLearningRate()

float tesseract::Plumbing::LayerLearningRate ( const char *  id) const
inline

Definition at line 105 of file plumbing.h.

105  {
106  const float* lr_ptr = LayerLearningRatePtr(id);
107  ASSERT_HOST(lr_ptr != nullptr);
108  return *lr_ptr;
109  }
float * LayerLearningRatePtr(const char *id) const
Definition: plumbing.cpp:168
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ LayerLearningRatePtr()

float * tesseract::Plumbing::LayerLearningRatePtr ( const char *  id) const

Definition at line 168 of file plumbing.cpp.

168  {
169  char* next_id;
170  int index = strtol(id, &next_id, 10);
171  if (index < 0 || index >= stack_.size()) return nullptr;
172  if (stack_[index]->IsPlumbingType()) {
173  Plumbing* plumbing = static_cast<Plumbing*>(stack_[index]);
174  ASSERT_HOST(*next_id == ':');
175  return plumbing->LayerLearningRatePtr(next_id + 1);
176  }
177  if (index >= learning_rates_.size()) return nullptr;
178  return &learning_rates_[index];
179 }
int size() const
Definition: genericvector.h:71
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
GenericVector< float > learning_rates_
Definition: plumbing.h:139
PointerVector< Network > stack_
Definition: plumbing.h:136
bool IsPlumbingType() const override
Definition: plumbing.h:44
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ RemapOutputs()

int tesseract::Plumbing::RemapOutputs ( int  old_no,
const std::vector< int > &  code_map 
)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 59 of file plumbing.cpp.

59  {
60  num_weights_ = 0;
61  for (int i = 0; i < stack_.size(); ++i) {
62  num_weights_ += stack_[i]->RemapOutputs(old_no, code_map);
63  }
64  return num_weights_;
65 }
int32_t num_weights_
Definition: network.h:305
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ ScaleLayerLearningRate()

void tesseract::Plumbing::ScaleLayerLearningRate ( const char *  id,
double  factor 
)
inline

Definition at line 111 of file plumbing.h.

111  {
112  float* lr_ptr = LayerLearningRatePtr(id);
113  ASSERT_HOST(lr_ptr != nullptr);
114  *lr_ptr *= factor;
115  }
float * LayerLearningRatePtr(const char *id) const
Definition: plumbing.cpp:168
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Serialize()

bool tesseract::Plumbing::Serialize ( TFile fp) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 182 of file plumbing.cpp.

182  {
183  if (!Network::Serialize(fp)) return false;
184  uint32_t size = stack_.size();
185  // Can't use PointerVector::Serialize here as we need a special DeSerialize.
186  if (!fp->Serialize(&size)) return false;
187  for (uint32_t i = 0; i < size; ++i)
188  if (!stack_[i]->Serialize(fp)) return false;
190  !learning_rates_.Serialize(fp)) {
191  return false;
192  }
193  return true;
194 }
GenericVector< float > learning_rates_
Definition: plumbing.h:139
bool Serialize(FILE *fp) const
PointerVector< Network > stack_
Definition: plumbing.h:136
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:151
bool Serialize(TFile *fp) const override
Definition: plumbing.cpp:182
int32_t network_flags_
Definition: network.h:302

◆ SetEnableTraining()

void tesseract::Plumbing::SetEnableTraining ( TrainingState  state)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 31 of file plumbing.cpp.

31  {
33  for (int i = 0; i < stack_.size(); ++i)
34  stack_[i]->SetEnableTraining(state);
35 }
PointerVector< Network > stack_
Definition: plumbing.h:136
virtual void SetEnableTraining(TrainingState state)
Definition: network.cpp:110
void SetEnableTraining(TrainingState state) override
Definition: plumbing.cpp:31

◆ SetNetworkFlags()

void tesseract::Plumbing::SetNetworkFlags ( uint32_t  flags)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 39 of file plumbing.cpp.

39  {
41  for (int i = 0; i < stack_.size(); ++i)
42  stack_[i]->SetNetworkFlags(flags);
43 }
PointerVector< Network > stack_
Definition: plumbing.h:136
virtual void SetNetworkFlags(uint32_t flags)
Definition: network.cpp:124
void SetNetworkFlags(uint32_t flags) override
Definition: plumbing.cpp:39

◆ SetRandomizer()

void tesseract::Plumbing::SetRandomizer ( TRand randomizer)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 76 of file plumbing.cpp.

76  {
77  for (int i = 0; i < stack_.size(); ++i)
78  stack_[i]->SetRandomizer(randomizer);
79 }
void SetRandomizer(TRand *randomizer) override
Definition: plumbing.cpp:76
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ SetupNeedsBackprop()

bool tesseract::Plumbing::SetupNeedsBackprop ( bool  needs_backprop)
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 100 of file plumbing.cpp.

100  {
101  if (IsTraining()) {
102  needs_to_backprop_ = needs_backprop;
103  bool retval = needs_backprop;
104  for (int i = 0; i < stack_.size(); ++i) {
105  if (stack_[i]->SetupNeedsBackprop(needs_backprop)) retval = true;
106  }
107  return retval;
108  }
109  // Frozen networks don't do backprop.
110  needs_to_backprop_ = false;
111  return false;
112 }
PointerVector< Network > stack_
Definition: plumbing.h:136
bool needs_to_backprop_
Definition: network.h:301
bool SetupNeedsBackprop(bool needs_backprop) override
Definition: plumbing.cpp:100
bool IsTraining() const
Definition: network.h:115

◆ spec()

STRING tesseract::Plumbing::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Reversed, and tesseract::Series.

Definition at line 38 of file plumbing.h.

38  {
39  return "Sub-classes of Plumbing must implement spec()!";
40  }

◆ stack()

const PointerVector<Network>& tesseract::Plumbing::stack ( ) const
inline

Definition at line 96 of file plumbing.h.

96  {
97  return stack_;
98  }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ Update()

void tesseract::Plumbing::Update ( float  learning_rate,
float  momentum,
float  adam_beta,
int  num_samples 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 216 of file plumbing.cpp.

217  {
218  for (int i = 0; i < stack_.size(); ++i) {
220  if (i < learning_rates_.size())
221  learning_rate = learning_rates_[i];
222  else
223  learning_rates_.push_back(learning_rate);
224  }
225  if (stack_[i]->IsTraining()) {
226  stack_[i]->Update(learning_rate, momentum, adam_beta, num_samples);
227  }
228  }
229 }
int size() const
Definition: genericvector.h:71
GenericVector< float > learning_rates_
Definition: plumbing.h:139
PointerVector< Network > stack_
Definition: plumbing.h:136
int push_back(T object)
bool IsTraining() const
Definition: network.h:115
int32_t network_flags_
Definition: network.h:302

◆ XScaleFactor()

int tesseract::Plumbing::XScaleFactor ( ) const
overridevirtual

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 120 of file plumbing.cpp.

120  {
121  return stack_[0]->XScaleFactor();
122 }
PointerVector< Network > stack_
Definition: plumbing.h:136

Member Data Documentation

◆ learning_rates_

GenericVector<float> tesseract::Plumbing::learning_rates_
protected

Definition at line 139 of file plumbing.h.

◆ stack_

PointerVector<Network> tesseract::Plumbing::stack_
protected

Definition at line 136 of file plumbing.h.


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