tesseract v5.3.3.20231005
tesseract::Reversed Class Reference

#include <reversed.h>

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

Public Member Functions

TESS_API Reversed (const std::string &name, NetworkType type)
 
 ~Reversed () override=default
 
StaticShape OutputShape (const StaticShape &input_shape) const override
 
std::string spec () const override
 
TESS_API void SetNetwork (Network *network)
 
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
 
- Public Member Functions inherited from tesseract::Plumbing
 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
 
void EnumerateLayers (const std::string *prefix, std::vector< std::string > &layers) const
 
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)
 
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 InputShape () const
 
virtual StaticShape OutputShape (const StaticShape &input_shape) const
 
const std::string & name () const
 
virtual std::string spec () const
 
bool TestFlag (NetworkFlags flag) const
 
virtual bool IsPlumbingType () const
 
virtual void SetEnableTraining (TrainingState state)
 
virtual void SetNetworkFlags (uint32_t flags)
 
virtual int InitWeights (float range, TRand *randomizer)
 
virtual int RemapOutputs (int old_no, const std::vector< int > &code_map)
 
virtual void ConvertToInt ()
 
virtual void SetRandomizer (TRand *randomizer)
 
virtual bool SetupNeedsBackprop (bool needs_backprop)
 
virtual int XScaleFactor () const
 
virtual void CacheXScaleFactor (int factor)
 
virtual void DebugWeights ()=0
 
virtual bool Serialize (TFile *fp) const
 
virtual bool DeSerialize (TFile *fp)=0
 
virtual void Update (float learning_rate, float momentum, float adam_beta, int num_samples)
 
virtual void CountAlternators (const Network &other, TFloat *same, 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)
 

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)
 
- Protected Attributes inherited from tesseract::Plumbing
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_
 

Detailed Description

Definition at line 27 of file reversed.h.

Constructor & Destructor Documentation

◆ Reversed()

tesseract::Reversed::Reversed ( const std::string &  name,
NetworkType  type 
)
explicit

Definition at line 26 of file reversed.cpp.

26 : Plumbing(name) {
27 type_ = type;
28}
NetworkType type_
Definition: network.h:300
const std::string & name() const
Definition: network.h:140
NetworkType type() const
Definition: network.h:110
Plumbing(const std::string &name)
Definition: plumbing.cpp:24

◆ ~Reversed()

tesseract::Reversed::~Reversed ( )
overridedefault

Member Function Documentation

◆ Backward()

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

Implements tesseract::Network.

Definition at line 63 of file reversed.cpp.

64 {
65 NetworkScratch::IO rev_input(fwd_deltas, scratch);
66 ReverseData(fwd_deltas, rev_input);
67 NetworkScratch::IO rev_output(fwd_deltas, scratch);
68 if (stack_[0]->Backward(debug, *rev_input, scratch, rev_output)) {
69 ReverseData(*rev_output, back_deltas);
70 return true;
71 }
72 return false;
73}
std::vector< Network * > stack_
Definition: plumbing.h:147
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: reversed.cpp:63

◆ Forward()

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

Implements tesseract::Network.

Definition at line 52 of file reversed.cpp.

53 {
54 NetworkScratch::IO rev_input(input, scratch);
55 ReverseData(input, rev_input);
56 NetworkScratch::IO rev_output(input, scratch);
57 stack_[0]->Forward(debug, *rev_input, nullptr, scratch, rev_output);
58 ReverseData(*rev_output, output);
59}

◆ OutputShape()

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

Reimplemented from tesseract::Network.

Definition at line 32 of file reversed.cpp.

32 {
33 if (type_ == NT_XYTRANSPOSE) {
34 StaticShape x_shape(input_shape);
35 x_shape.set_width(input_shape.height());
36 x_shape.set_height(input_shape.width());
37 x_shape = stack_[0]->OutputShape(x_shape);
38 x_shape.SetShape(x_shape.batch(), x_shape.width(), x_shape.height(), x_shape.depth());
39 return x_shape;
40 }
41 return stack_[0]->OutputShape(input_shape);
42}
@ NT_XYTRANSPOSE
Definition: network.h:56

◆ SetNetwork()

void tesseract::Reversed::SetNetwork ( Network network)

Definition at line 45 of file reversed.cpp.

45 {
46 stack_.clear();
47 AddToStack(network);
48}
virtual void AddToStack(Network *network)
Definition: plumbing.cpp:84

◆ spec()

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

Reimplemented from tesseract::Plumbing.

Definition at line 37 of file reversed.h.

37 {
38 std::string spec(type_ == NT_XREVERSED ? "Rx" : (type_ == NT_YREVERSED ? "Ry" : "Txy"));
39 // For most simple cases, we will output Rx<net> or Ry<net> where <net> is
40 // the network in stack_[0], but in the special case that <net> is an
41 // LSTM, we will just output the LSTM's spec modified to take the reversal
42 // into account. This is because when the user specified Lfy64, we actually
43 // generated TxyLfx64, and if the user specified Lrx64 we actually
44 // generated RxLfx64, and we want to display what the user asked for.
45 std::string net_spec(stack_[0]->spec());
46 if (net_spec[0] == 'L') {
47 // Setup a from and to character according to the type of the reversal
48 // such that the LSTM spec gets modified to the spec that the user
49 // asked for
50 char from = 'f';
51 char to = 'r';
52 if (type_ == NT_XYTRANSPOSE) {
53 from = 'x';
54 to = 'y';
55 }
56 // Change the from char to the to char.
57 for (auto &it : net_spec) {
58 if (it == from) {
59 it = to;
60 }
61 }
62 spec += net_spec;
63 return spec;
64 }
65 spec += net_spec;
66 return spec;
67 }
@ NT_XREVERSED
Definition: network.h:54
@ NT_YREVERSED
Definition: network.h:55
std::string spec() const override
Definition: reversed.h:37

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