tesseract  5.0.0-alpha-619-ge9db
tesseract::Series Class Reference

#include <series.h>

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

Public Member Functions

 Series (const STRING &name)
 
 ~Series () override=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)
 
 ~Plumbing () override=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_
 

Detailed Description

Definition at line 27 of file series.h.

Constructor & Destructor Documentation

◆ Series()

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

Definition at line 28 of file series.cpp.

28  : Plumbing(name) {
29  type_ = NT_SERIES;
30 }

◆ ~Series()

tesseract::Series::~Series ( )
overridedefault

Member Function Documentation

◆ AppendSeries()

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

Definition at line 189 of file series.cpp.

189  {
190  ASSERT_HOST(src->type() == NT_SERIES);
191  auto* src_series = static_cast<Series*>(src);
192  for (int s = 0; s < src_series->stack_.size(); ++s) {
193  AddToStack(src_series->stack_[s]);
194  src_series->stack_[s] = nullptr;
195  }
196  delete src;
197 }

◆ Backward()

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

Implements tesseract::Network.

Definition at line 128 of file series.cpp.

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

◆ CacheXScaleFactor()

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

Reimplemented from tesseract::Plumbing.

Definition at line 100 of file series.cpp.

100  {
101  stack_[0]->CacheXScaleFactor(factor);
102 }

◆ Forward()

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

Implements tesseract::Network.

Definition at line 106 of file series.cpp.

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

◆ InitWeights()

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

Reimplemented from tesseract::Plumbing.

Definition at line 46 of file series.cpp.

46  {
47  num_weights_ = 0;
48  tprintf("Num outputs,weights in Series:\n");
49  for (int i = 0; i < stack_.size(); ++i) {
50  int weights = stack_[i]->InitWeights(range, randomizer);
51  tprintf(" %s:%d, %d\n",
52  stack_[i]->spec().c_str(), stack_[i]->NumOutputs(), weights);
53  num_weights_ += weights;
54  }
55  tprintf("Total weights = %d\n", num_weights_);
56  return num_weights_;
57 }

◆ OutputShape()

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

Reimplemented from tesseract::Network.

Definition at line 34 of file series.cpp.

34  {
35  StaticShape result(input_shape);
36  int stack_size = stack_.size();
37  for (int i = 0; i < stack_size; ++i) {
38  result = stack_[i]->OutputShape(result);
39  }
40  return result;
41 }

◆ RemapOutputs()

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

Reimplemented from tesseract::Plumbing.

Definition at line 61 of file series.cpp.

61  {
62  num_weights_ = 0;
63  tprintf("Num (Extended) outputs,weights in Series:\n");
64  for (int i = 0; i < stack_.size(); ++i) {
65  int weights = stack_[i]->RemapOutputs(old_no, code_map);
66  tprintf(" %s:%d, %d\n", stack_[i]->spec().c_str(),
67  stack_[i]->NumOutputs(), weights);
68  num_weights_ += weights;
69  }
70  tprintf("Total weights = %d\n", num_weights_);
71  no_ = stack_.back()->NumOutputs();
72  return num_weights_;
73 }

◆ SetupNeedsBackprop()

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

Reimplemented from tesseract::Plumbing.

Definition at line 78 of file series.cpp.

78  {
79  needs_to_backprop_ = needs_backprop;
80  for (int i = 0; i < stack_.size(); ++i)
81  needs_backprop = stack_[i]->SetupNeedsBackprop(needs_backprop);
82  return needs_backprop;
83 }

◆ 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  }

◆ SplitAt()

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

Definition at line 159 of file series.cpp.

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

◆ XScaleFactor()

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

Reimplemented from tesseract::Plumbing.

Definition at line 91 of file series.cpp.

91  {
92  int factor = 1;
93  for (int i = 0; i < stack_.size(); ++i)
94  factor *= stack_[i]->XScaleFactor();
95  return factor;
96 }

The documentation for this class was generated from the following files:
tesseract::Plumbing::AddToStack
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:82
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::Plumbing::Plumbing
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
STRING
Definition: strngs.h:45
tesseract::Series::XScaleFactor
int XScaleFactor() const override
Definition: series.cpp:91
tesseract::Network::needs_to_backprop_
bool needs_to_backprop_
Definition: network.h:295
tesseract::Plumbing::stack_
PointerVector< Network > stack_
Definition: plumbing.h:136
tesseract::Network::IsTraining
bool IsTraining() const
Definition: network.h:115
tesseract::Series::Series
Series(const STRING &name)
Definition: series.cpp:28
tesseract::NT_SERIES
Definition: network.h:54
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
tesseract::NT_TANH
Definition: network.h:65
tesseract::Series::spec
STRING spec() const override
Definition: series.h:37
tesseract::Network::NumOutputs
int NumOutputs() const
Definition: network.h:123
tesseract::Network::num_weights_
int32_t num_weights_
Definition: network.h:299
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesseract::Series::SetupNeedsBackprop
bool SetupNeedsBackprop(bool needs_backprop) override
Definition: series.cpp:78
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::NT_SOFTMAX
Definition: network.h:68