tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
fullyconnected.h
1 // File: fullyconnected.h
3 // Description: Simple feed-forward layer with various non-linearities.
4 // Author: Ray Smith
5 // Created: Wed Feb 26 14:46:06 PST 2014
6 //
7 // (C) Copyright 2014, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #ifndef TESSERACT_LSTM_FULLYCONNECTED_H_
20 #define TESSERACT_LSTM_FULLYCONNECTED_H_
21 
22 #include "network.h"
23 #include "networkscratch.h"
24 
25 namespace tesseract {
26 
27 // C++ Implementation of the Softmax (output) class from lstm.py.
28 class FullyConnected : public Network {
29  public:
30  FullyConnected(const STRING& name, int ni, int no, NetworkType type);
31  virtual ~FullyConnected() = default;
32 
33  // Returns the shape output from the network given an input shape (which may
34  // be partially unknown ie zero).
35  StaticShape OutputShape(const StaticShape& input_shape) const override;
36 
37  STRING spec() const override {
38  STRING spec;
39  if (type_ == NT_TANH)
40  spec.add_str_int("Ft", no_);
41  else if (type_ == NT_LOGISTIC)
42  spec.add_str_int("Fs", no_);
43  else if (type_ == NT_RELU)
44  spec.add_str_int("Fr", no_);
45  else if (type_ == NT_LINEAR)
46  spec.add_str_int("Fl", no_);
47  else if (type_ == NT_POSCLIP)
48  spec.add_str_int("Fp", no_);
49  else if (type_ == NT_SYMCLIP)
50  spec.add_str_int("Fs", no_);
51  else if (type_ == NT_SOFTMAX)
52  spec.add_str_int("Fc", no_);
53  else
54  spec.add_str_int("Fm", no_);
55  return spec;
56  }
57 
58  // Changes the type to the given type. Used to commute a softmax to a
59  // non-output type for adding on other networks.
60  void ChangeType(NetworkType type) {
61  type_ = type;
62  }
63 
64  // Suspends/Enables training by setting the training_ flag. Serialize and
65  // DeSerialize only operate on the run-time data if state is false.
66  void SetEnableTraining(TrainingState state) override;
67 
68  // Sets up the network for training. Initializes weights using weights of
69  // scale `range` picked according to the random number generator `randomizer`.
70  int InitWeights(float range, TRand* randomizer) override;
71  // Recursively searches the network for softmaxes with old_no outputs,
72  // and remaps their outputs according to code_map. See network.h for details.
73  int RemapOutputs(int old_no, const std::vector<int>& code_map) override;
74 
75  // Converts a float network to an int network.
76  void ConvertToInt() override;
77 
78  // Provides debug output on the weights.
79  void DebugWeights() override;
80 
81  // Writes to the given file. Returns false in case of error.
82  bool Serialize(TFile* fp) const override;
83  // Reads from the given file. Returns false in case of error.
84  bool DeSerialize(TFile* fp) override;
85 
86  // Runs forward propagation of activations on the input line.
87  // See Network for a detailed discussion of the arguments.
88  void Forward(bool debug, const NetworkIO& input,
89  const TransposedArray* input_transpose, NetworkScratch* scratch,
90  NetworkIO* output) override;
91  // Components of Forward so FullyConnected can be reused inside LSTM.
92  void SetupForward(const NetworkIO& input,
93  const TransposedArray* input_transpose);
94  void ForwardTimeStep(int t, double* output_line);
95  void ForwardTimeStep(const double* d_input, int t, double* output_line);
96  void ForwardTimeStep(const int8_t* i_input, int t, double* output_line);
97 
98  // Runs backward propagation of errors on the deltas line.
99  // See Network for a detailed discussion of the arguments.
100  bool Backward(bool debug, const NetworkIO& fwd_deltas,
101  NetworkScratch* scratch, NetworkIO* back_deltas) override;
102  // Components of Backward so FullyConnected can be reused inside LSTM.
103  void BackwardTimeStep(const NetworkIO& fwd_deltas, int t, double* curr_errors,
104  TransposedArray* errors_t, double* backprop);
105  void FinishBackward(const TransposedArray& errors_t);
106 
107  // Updates the weights using the given learning rate, momentum and adam_beta.
108  // num_samples is used in the adam computation iff use_adam_ is true.
109  void Update(float learning_rate, float momentum, float adam_beta,
110  int num_samples) override;
111  // Sums the products of weight updates in *this and other, splitting into
112  // positive (same direction) in *same and negative (different direction) in
113  // *changed.
114  void CountAlternators(const Network& other, double* same,
115  double* changed) const override;
116 
117  protected:
118  // Weight arrays of size [no, ni + 1].
120  // Transposed copy of input used during training of size [ni, width].
122  // Pointer to transposed input stored elsewhere. If not null, this is used
123  // in preference to calculating the transpose and storing it in source_t_.
125  // Activations from forward pass of size [width, no].
127  // Memory of the integer mode input to forward as softmax always outputs
128  // float, so the information is otherwise lost.
129  bool int_mode_;
130 };
131 
132 } // namespace tesseract.
133 
134 
135 
136 #endif // TESSERACT_LSTM_FULLYCONNECTED_H_
const TransposedArray * external_source_
Definition: fullyconnected.h:124
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
void CountAlternators(const Network &other, double *same, double *changed) const override
Definition: fullyconnected.cpp:306
Definition: network.h:63
void SetEnableTraining(TrainingState state) override
Definition: fullyconnected.cpp:61
Definition: network.h:62
Definition: helpers.h:42
void ForwardTimeStep(int t, double *output_line)
Definition: fullyconnected.cpp:185
WeightMatrix weights_
Definition: fullyconnected.h:119
Definition: static_shape.h:38
Definition: networkscratch.h:36
void BackwardTimeStep(const NetworkIO &fwd_deltas, int t, double *curr_errors, TransposedArray *errors_t, double *backprop)
Definition: fullyconnected.cpp:265
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: fullyconnected.cpp:119
FullyConnected(const STRING &name, int ni, int no, NetworkType type)
Definition: fullyconnected.cpp:39
Definition: serialis.h:77
TransposedArray source_t_
Definition: fullyconnected.h:121
void Update(float learning_rate, float momentum, float adam_beta, int num_samples) override
Definition: fullyconnected.cpp:298
Definition: baseapi.cpp:94
Definition: network.h:65
void ConvertToInt() override
Definition: fullyconnected.cpp:96
Definition: network.h:64
const STRING & name() const
Definition: network.h:138
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: fullyconnected.cpp:221
int InitWeights(float range, TRand *randomizer) override
Definition: fullyconnected.cpp:77
NetworkType
Definition: network.h:43
void SetupForward(const NetworkIO &input, const TransposedArray *input_transpose)
Definition: fullyconnected.cpp:173
Definition: weightmatrix.h:33
Definition: network.h:105
Definition: weightmatrix.h:66
NetworkType type() const
Definition: network.h:112
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: fullyconnected.cpp:46
Definition: fullyconnected.h:28
Definition: strngs.h:45
bool DeSerialize(TFile *fp) override
Definition: fullyconnected.cpp:113
bool Serialize(TFile *fp) const override
Definition: fullyconnected.cpp:106
TrainingState
Definition: network.h:92
NetworkIO acts_
Definition: fullyconnected.h:126
Definition: network.h:68
Definition: network.h:66
int32_t no_
Definition: network.h:304
Definition: networkio.h:39
STRING spec() const override
Definition: fullyconnected.h:37
virtual ~FullyConnected()=default
void ChangeType(NetworkType type)
Definition: fullyconnected.h:60
NetworkType type_
Definition: network.h:299
int RemapOutputs(int old_no, const std::vector< int > &code_map) override
Definition: fullyconnected.cpp:87
bool int_mode_
Definition: fullyconnected.h:129
void DebugWeights() override
Definition: fullyconnected.cpp:101
void FinishBackward(const TransposedArray &errors_t)
Definition: fullyconnected.cpp:289
Definition: network.h:67