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