tesseract  5.0.0
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 std::string &name)
 
 ~Plumbing () override
 
StaticShape InputShape () const override
 
std::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 std::vector< Network * > & stack () const
 
TESS_API void EnumerateLayers (const std::string *prefix, std::vector< std::string > &layers) const
 
TESS_API NetworkGetLayer (const char *id) const
 
float LayerLearningRate (const char *id)
 
void ScaleLayerLearningRate (const char *id, double factor)
 
void SetLayerLearningRate (const char *id, float learning_rate)
 
TESS_API float * LayerLearningRatePtr (const char *id)
 
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, TFloat *same, TFloat *changed) const override
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const std::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 std::string & name () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual int RemapOutputs ([[maybe_unused]] int old_no, [[maybe_unused]] const std::vector< int > &code_map)
 
virtual void CacheXScaleFactor ([[maybe_unused]] int factor)
 
virtual void Update ([[maybe_unused]] float learning_rate, [[maybe_unused]] float momentum, [[maybe_unused]] float adam_beta, [[maybe_unused]] int num_samples)
 
virtual void CountAlternators ([[maybe_unused]] const Network &other, [[maybe_unused]] TFloat *same, [[maybe_unused]] TFloat *changed) const
 
virtual void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)=0
 
virtual bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)=0
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Protected Attributes

std::vector< Network * > stack_
 
std::vector< 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_
 
std::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 (Image pix, ScrollView *window)
 
- Protected Member Functions inherited from tesseract::Network
TFloat Random (TFloat range)
 

Detailed Description

Definition at line 28 of file plumbing.h.

Constructor & Destructor Documentation

◆ Plumbing()

tesseract::Plumbing::Plumbing ( const std::string &  name)
explicit

Definition at line 24 of file plumbing.cpp.

24 : Network(NT_PARALLEL, name, 0, 0) {}
@ NT_PARALLEL
Definition: network.h:47
const std::string & name() const
Definition: network.h:140

◆ ~Plumbing()

tesseract::Plumbing::~Plumbing ( )
inlineoverride

Definition at line 32 of file plumbing.h.

32  {
33  for (auto data : stack_) {
34  delete data;
35  }
36  }
std::vector< Network * > stack_
Definition: plumbing.h:150

Member Function Documentation

◆ AddToStack()

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

Definition at line 84 of file plumbing.cpp.

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

◆ CacheXScaleFactor()

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

Definition at line 130 of file plumbing.cpp.

130  {
131  for (auto &i : stack_) {
132  i->CacheXScaleFactor(factor);
133  }
134 }

◆ ConvertToInt()

void tesseract::Plumbing::ConvertToInt ( )
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 68 of file plumbing.cpp.

68  {
69  for (auto &i : stack_) {
70  i->ConvertToInt();
71  }
72 }

◆ CountAlternators()

void tesseract::Plumbing::CountAlternators ( const Network other,
TFloat same,
TFloat changed 
) const
override

Definition at line 258 of file plumbing.cpp.

258  {
259  ASSERT_HOST(other.type() == type_);
260  const auto *plumbing = static_cast<const Plumbing *>(&other);
261  ASSERT_HOST(plumbing->stack_.size() == stack_.size());
262  for (size_t i = 0; i < stack_.size(); ++i) {
263  stack_[i]->CountAlternators(*plumbing->stack_[i], same, changed);
264  }
265 }
Plumbing(const std::string &name)
Definition: plumbing.cpp:24

◆ DebugWeights()

void tesseract::Plumbing::DebugWeights ( )
overridevirtual

Implements tesseract::Network.

Definition at line 137 of file plumbing.cpp.

137  {
138  for (auto &i : stack_) {
139  i->DebugWeights();
140  }
141 }

◆ DeSerialize()

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

Implements tesseract::Network.

Definition at line 215 of file plumbing.cpp.

215  {
216  for (auto data : stack_) {
217  delete data;
218  }
219  stack_.clear();
220  no_ = 0; // We will be modifying this as we AddToStack.
221  uint32_t size;
222  if (!fp->DeSerialize(&size)) {
223  return false;
224  }
225  for (uint32_t i = 0; i < size; ++i) {
226  Network *network = CreateFromFile(fp);
227  if (network == nullptr) {
228  return false;
229  }
230  AddToStack(network);
231  }
232  if ((network_flags_ & NF_LAYER_SPECIFIC_LR) && !fp->DeSerialize(learning_rates_)) {
233  return false;
234  }
235  return true;
236 }
@ NF_LAYER_SPECIFIC_LR
Definition: network.h:85
int32_t network_flags_
Definition: network.h:303
static Network * CreateFromFile(TFile *fp)
Definition: network.cpp:217
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:84
std::vector< float > learning_rates_
Definition: plumbing.h:153

◆ EnumerateLayers()

void tesseract::Plumbing::EnumerateLayers ( const std::string *  prefix,
std::vector< std::string > &  layers 
) const

Definition at line 144 of file plumbing.cpp.

144  {
145  for (size_t i = 0; i < stack_.size(); ++i) {
146  std::string layer_name;
147  if (prefix) {
148  layer_name = *prefix;
149  }
150  layer_name += ":" + std::to_string(i);
151  if (stack_[i]->IsPlumbingType()) {
152  auto *plumbing = static_cast<Plumbing *>(stack_[i]);
153  plumbing->EnumerateLayers(&layer_name, layers);
154  } else {
155  layers.push_back(layer_name);
156  }
157  }
158 }
bool IsPlumbingType() const override
Definition: plumbing.h:48

◆ GetLayer()

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

Definition at line 161 of file plumbing.cpp.

161  {
162  char *next_id;
163  int index = strtol(id, &next_id, 10);
164  if (index < 0 || static_cast<unsigned>(index) >= stack_.size()) {
165  return nullptr;
166  }
167  if (stack_[index]->IsPlumbingType()) {
168  auto *plumbing = static_cast<Plumbing *>(stack_[index]);
169  ASSERT_HOST(*next_id == ':');
170  return plumbing->GetLayer(next_id + 1);
171  }
172  return stack_[index];
173 }

◆ InitWeights()

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

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 49 of file plumbing.cpp.

49  {
50  num_weights_ = 0;
51  for (auto &i : stack_) {
52  num_weights_ += i->InitWeights(range, randomizer);
53  }
54  return num_weights_;
55 }
int32_t num_weights_
Definition: network.h:306

◆ InputShape()

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

Reimplemented from tesseract::Network.

Definition at line 39 of file plumbing.h.

39  {
40  return stack_[0]->InputShape();
41  }

◆ IsPlumbingType()

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

Reimplemented from tesseract::Network.

Definition at line 48 of file plumbing.h.

48  {
49  return true;
50  }

◆ LayerLearningRate()

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

Definition at line 112 of file plumbing.h.

112  {
113  const float *lr_ptr = LayerLearningRatePtr(id);
114  ASSERT_HOST(lr_ptr != nullptr);
115  return *lr_ptr;
116  }
TESS_API float * LayerLearningRatePtr(const char *id)
Definition: plumbing.cpp:176

◆ LayerLearningRatePtr()

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

Definition at line 176 of file plumbing.cpp.

176  {
177  char *next_id;
178  int index = strtol(id, &next_id, 10);
179  if (index < 0 || static_cast<unsigned>(index) >= stack_.size()) {
180  return nullptr;
181  }
182  if (stack_[index]->IsPlumbingType()) {
183  auto *plumbing = static_cast<Plumbing *>(stack_[index]);
184  ASSERT_HOST(*next_id == ':');
185  return plumbing->LayerLearningRatePtr(next_id + 1);
186  }
187  if (static_cast<unsigned>(index) >= learning_rates_.size()) {
188  return nullptr;
189  }
190  return &learning_rates_[index];
191 }

◆ RemapOutputs()

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

Definition at line 59 of file plumbing.cpp.

59  {
60  num_weights_ = 0;
61  for (auto &i : stack_) {
62  num_weights_ += i->RemapOutputs(old_no, code_map);
63  }
64  return num_weights_;
65 }

◆ ScaleLayerLearningRate()

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

Definition at line 118 of file plumbing.h.

118  {
119  float *lr_ptr = LayerLearningRatePtr(id);
120  ASSERT_HOST(lr_ptr != nullptr);
121  *lr_ptr *= factor;
122  }

◆ Serialize()

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

Reimplemented from tesseract::Network.

Definition at line 194 of file plumbing.cpp.

194  {
195  if (!Network::Serialize(fp)) {
196  return false;
197  }
198  uint32_t size = stack_.size();
199  // Can't use PointerVector::Serialize here as we need a special DeSerialize.
200  if (!fp->Serialize(&size)) {
201  return false;
202  }
203  for (uint32_t i = 0; i < size; ++i) {
204  if (!stack_[i]->Serialize(fp)) {
205  return false;
206  }
207  }
208  if ((network_flags_ & NF_LAYER_SPECIFIC_LR) && !fp->Serialize(learning_rates_)) {
209  return false;
210  }
211  return true;
212 }
virtual bool Serialize(TFile *fp) const
Definition: network.cpp:158
bool Serialize(TFile *fp) const override
Definition: plumbing.cpp:194

◆ SetEnableTraining()

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

Reimplemented from tesseract::Network.

Definition at line 28 of file plumbing.cpp.

28  {
30  for (auto &i : stack_) {
31  i->SetEnableTraining(state);
32  }
33 }
virtual void SetEnableTraining(TrainingState state)
Definition: network.cpp:113

◆ SetLayerLearningRate()

void tesseract::Plumbing::SetLayerLearningRate ( const char *  id,
float  learning_rate 
)
inline

Definition at line 125 of file plumbing.h.

125  {
126  float *lr_ptr = LayerLearningRatePtr(id);
127  ASSERT_HOST(lr_ptr != nullptr);
128  *lr_ptr = learning_rate;
129  }

◆ SetNetworkFlags()

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

Reimplemented from tesseract::Network.

Definition at line 37 of file plumbing.cpp.

37  {
39  for (auto &i : stack_) {
40  i->SetNetworkFlags(flags);
41  }
42 }
virtual void SetNetworkFlags(uint32_t flags)
Definition: network.cpp:131

◆ SetRandomizer()

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

Reimplemented from tesseract::Network.

Definition at line 77 of file plumbing.cpp.

77  {
78  for (auto &i : stack_) {
79  i->SetRandomizer(randomizer);
80  }
81 }

◆ SetupNeedsBackprop()

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

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 102 of file plumbing.cpp.

102  {
103  if (IsTraining()) {
104  needs_to_backprop_ = needs_backprop;
105  bool retval = needs_backprop;
106  for (auto &i : stack_) {
107  if (i->SetupNeedsBackprop(needs_backprop)) {
108  retval = true;
109  }
110  }
111  return retval;
112  }
113  // Frozen networks don't do backprop.
114  needs_to_backprop_ = false;
115  return false;
116 }
bool needs_to_backprop_
Definition: network.h:302
bool IsTraining() const
Definition: network.h:113

◆ spec()

std::string tesseract::Plumbing::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

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

Definition at line 42 of file plumbing.h.

42  {
43  return "Sub-classes of Plumbing must implement spec()!";
44  }

◆ stack()

const std::vector<Network *>& tesseract::Plumbing::stack ( ) const
inline

Definition at line 102 of file plumbing.h.

102  {
103  return stack_;
104  }

◆ Update()

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

Definition at line 240 of file plumbing.cpp.

240  {
241  for (size_t i = 0; i < stack_.size(); ++i) {
243  if (i < learning_rates_.size()) {
244  learning_rate = learning_rates_[i];
245  } else {
246  learning_rates_.push_back(learning_rate);
247  }
248  }
249  if (stack_[i]->IsTraining()) {
250  stack_[i]->Update(learning_rate, momentum, adam_beta, num_samples);
251  }
252  }
253 }

◆ XScaleFactor()

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

Reimplemented from tesseract::Network.

Reimplemented in tesseract::Series.

Definition at line 124 of file plumbing.cpp.

124  {
125  return stack_[0]->XScaleFactor();
126 }

Member Data Documentation

◆ learning_rates_

std::vector<float> tesseract::Plumbing::learning_rates_
protected

Definition at line 153 of file plumbing.h.

◆ stack_

std::vector<Network *> tesseract::Plumbing::stack_
protected

Definition at line 150 of file plumbing.h.


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