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

#include <series.h>

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

Public Member Functions

 Series (const STRING &name)
 
virtual ~Series ()=default
 
StaticShape OutputShape (const StaticShape &input_shape) const override
 
STRING spec () const override
 
int InitWeights (float range, TRand *randomizer) override
 
int RemapOutputs (int old_no, const std::vector< int > &code_map) override
 
bool SetupNeedsBackprop (bool needs_backprop) override
 
int XScaleFactor () const override
 
void CacheXScaleFactor (int factor) override
 
void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
 
bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
 
void SplitAt (int last_start, Series **start, Series **end)
 
void AppendSeries (Network *src)
 
- Public Member Functions inherited from tesseract::Plumbing
 Plumbing (const STRING &name)
 
virtual ~Plumbing ()=default
 
StaticShape InputShape () const override
 
bool IsPlumbingType () const override
 
void SetEnableTraining (TrainingState state) override
 
void SetNetworkFlags (uint32_t flags) override
 
void ConvertToInt () override
 
void SetRandomizer (TRand *randomizer) override
 
virtual void AddToStack (Network *network)
 
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
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

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)
 
- Protected Attributes inherited from tesseract::Plumbing
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_
 
- Static Protected Attributes inherited from tesseract::Network
static char const *const kTypeNames [NT_COUNT]
 

Detailed Description

Definition at line 27 of file series.h.

Constructor & Destructor Documentation

◆ Series()

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

Definition at line 29 of file series.cpp.

29  : Plumbing(name) {
30  type_ = NT_SERIES;
31 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
NetworkType type_
Definition: network.h:299
const STRING & name() const
Definition: network.h:138

◆ ~Series()

virtual tesseract::Series::~Series ( )
virtualdefault

Member Function Documentation

◆ AppendSeries()

void tesseract::Series::AppendSeries ( Network src)

Definition at line 190 of file series.cpp.

190  {
191  ASSERT_HOST(src->type() == NT_SERIES);
192  Series* src_series = static_cast<Series*>(src);
193  for (int s = 0; s < src_series->stack_.size(); ++s) {
194  AddToStack(src_series->stack_[s]);
195  src_series->stack_[s] = nullptr;
196  }
197  delete src;
198 }
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
Series(const STRING &name)
Definition: series.cpp:29
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ Backward()

bool tesseract::Series::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 129 of file series.cpp.

131  {
132  if (!IsTraining()) return false;
133  int stack_size = stack_.size();
134  ASSERT_HOST(stack_size > 1);
135  // Revolving intermediate buffers.
136  NetworkScratch::IO buffer1(fwd_deltas, scratch);
137  NetworkScratch::IO buffer2(fwd_deltas, scratch);
138  // Run each network in reverse order, giving the back_deltas output of n as
139  // the fwd_deltas input to n-1, with the 0 network providing the real output.
140  if (!stack_.back()->IsTraining() ||
141  !stack_.back()->Backward(debug, fwd_deltas, scratch, buffer1))
142  return false;
143  for (int i = stack_size - 2; i >= 0; i -= 2) {
144  if (!stack_[i]->IsTraining() ||
145  !stack_[i]->Backward(debug, *buffer1, scratch,
146  i > 0 ? buffer2 : back_deltas))
147  return false;
148  if (i == 0) return needs_to_backprop_;
149  if (!stack_[i - 1]->IsTraining() ||
150  !stack_[i - 1]->Backward(debug, *buffer2, scratch,
151  i > 1 ? buffer1 : back_deltas))
152  return false;
153  }
154  return needs_to_backprop_;
155 }
PointerVector< Network > stack_
Definition: plumbing.h:136
bool needs_to_backprop_
Definition: network.h:301
bool IsTraining() const
Definition: network.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ CacheXScaleFactor()

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

Reimplemented from tesseract::Plumbing.

Definition at line 101 of file series.cpp.

101  {
102  stack_[0]->CacheXScaleFactor(factor);
103 }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ Forward()

void tesseract::Series::Forward ( bool  debug,
const NetworkIO input,
const TransposedArray input_transpose,
NetworkScratch scratch,
NetworkIO output 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 107 of file series.cpp.

109  {
110  int stack_size = stack_.size();
111  ASSERT_HOST(stack_size > 1);
112  // Revolving intermediate buffers.
113  NetworkScratch::IO buffer1(input, scratch);
114  NetworkScratch::IO buffer2(input, scratch);
115  // Run each network in turn, giving the output of n as the input to n + 1,
116  // with the final network providing the real output.
117  stack_[0]->Forward(debug, input, input_transpose, scratch, buffer1);
118  for (int i = 1; i < stack_size; i += 2) {
119  stack_[i]->Forward(debug, *buffer1, nullptr, scratch,
120  i + 1 < stack_size ? buffer2 : output);
121  if (i + 1 == stack_size) return;
122  stack_[i + 1]->Forward(debug, *buffer2, nullptr, scratch,
123  i + 2 < stack_size ? buffer1 : output);
124  }
125 }
PointerVector< Network > stack_
Definition: plumbing.h:136
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ InitWeights()

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

Reimplemented from tesseract::Plumbing.

Definition at line 47 of file series.cpp.

47  {
48  num_weights_ = 0;
49  tprintf("Num outputs,weights in Series:\n");
50  for (int i = 0; i < stack_.size(); ++i) {
51  int weights = stack_[i]->InitWeights(range, randomizer);
52  tprintf(" %s:%d, %d\n",
53  stack_[i]->spec().string(), stack_[i]->NumOutputs(), weights);
54  num_weights_ += weights;
55  }
56  tprintf("Total weights = %d\n", num_weights_);
57  return num_weights_;
58 }
int NumOutputs() const
Definition: network.h:123
int32_t num_weights_
Definition: network.h:305
PointerVector< Network > stack_
Definition: plumbing.h:136
STRING spec() const override
Definition: series.h:37
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ OutputShape()

StaticShape tesseract::Series::OutputShape ( const StaticShape input_shape) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 35 of file series.cpp.

35  {
36  StaticShape result(input_shape);
37  int stack_size = stack_.size();
38  for (int i = 0; i < stack_size; ++i) {
39  result = stack_[i]->OutputShape(result);
40  }
41  return result;
42 }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ RemapOutputs()

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

Reimplemented from tesseract::Plumbing.

Definition at line 62 of file series.cpp.

62  {
63  num_weights_ = 0;
64  tprintf("Num (Extended) outputs,weights in Series:\n");
65  for (int i = 0; i < stack_.size(); ++i) {
66  int weights = stack_[i]->RemapOutputs(old_no, code_map);
67  tprintf(" %s:%d, %d\n", stack_[i]->spec().string(),
68  stack_[i]->NumOutputs(), weights);
69  num_weights_ += weights;
70  }
71  tprintf("Total weights = %d\n", num_weights_);
72  no_ = stack_.back()->NumOutputs();
73  return num_weights_;
74 }
int NumOutputs() const
Definition: network.h:123
int32_t num_weights_
Definition: network.h:305
PointerVector< Network > stack_
Definition: plumbing.h:136
STRING spec() const override
Definition: series.h:37
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ SetupNeedsBackprop()

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

Reimplemented from tesseract::Plumbing.

Definition at line 79 of file series.cpp.

79  {
80  needs_to_backprop_ = needs_backprop;
81  for (int i = 0; i < stack_.size(); ++i)
82  needs_backprop = stack_[i]->SetupNeedsBackprop(needs_backprop);
83  return needs_backprop;
84 }
PointerVector< Network > stack_
Definition: plumbing.h:136
bool needs_to_backprop_
Definition: network.h:301
bool SetupNeedsBackprop(bool needs_backprop) override
Definition: series.cpp:79

◆ spec()

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

Reimplemented from tesseract::Plumbing.

Definition at line 37 of file series.h.

37  {
38  STRING spec("[");
39  for (int i = 0; i < stack_.size(); ++i)
40  spec += stack_[i]->spec();
41  spec += "]";
42  return spec;
43  }
PointerVector< Network > stack_
Definition: plumbing.h:136
STRING spec() const override
Definition: series.h:37
Definition: strngs.h:45

◆ SplitAt()

void tesseract::Series::SplitAt ( int  last_start,
Series **  start,
Series **  end 
)

Definition at line 160 of file series.cpp.

160  {
161  *start = nullptr;
162  *end = nullptr;
163  if (last_start < 0 || last_start >= stack_.size()) {
164  tprintf("Invalid split index %d must be in range [0,%d]!\n",
165  last_start, stack_.size() - 1);
166  return;
167  }
168  Series* master_series = new Series("MasterSeries");
169  Series* boosted_series = new Series("BoostedSeries");
170  for (int s = 0; s <= last_start; ++s) {
171  if (s + 1 == stack_.size() && stack_[s]->type() == NT_SOFTMAX) {
172  // Change the softmax to a tanh.
173  FullyConnected* fc = static_cast<FullyConnected*>(stack_[s]);
174  fc->ChangeType(NT_TANH);
175  }
176  master_series->AddToStack(stack_[s]);
177  stack_[s] = nullptr;
178  }
179  for (int s = last_start + 1; s < stack_.size(); ++s) {
180  boosted_series->AddToStack(stack_[s]);
181  stack_[s] = nullptr;
182  }
183  *start = master_series;
184  *end = boosted_series;
185  delete this;
186 }
PointerVector< Network > stack_
Definition: plumbing.h:136
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
Series(const STRING &name)
Definition: series.cpp:29

◆ XScaleFactor()

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

Reimplemented from tesseract::Plumbing.

Definition at line 92 of file series.cpp.

92  {
93  int factor = 1;
94  for (int i = 0; i < stack_.size(); ++i)
95  factor *= stack_[i]->XScaleFactor();
96  return factor;
97 }
PointerVector< Network > stack_
Definition: plumbing.h:136
int XScaleFactor() const override
Definition: series.cpp:92

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