tesseract  4.0.0-1-g2a2b
fullyconnected.h
Go to the documentation of this file.
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.
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_
bool DeSerialize(TFile *fp) override
FullyConnected(const STRING &name, int ni, int no, NetworkType type)
void ConvertToInt() override
void ChangeType(NetworkType type)
bool Serialize(TFile *fp) const override
void BackwardTimeStep(const NetworkIO &fwd_deltas, int t, double *curr_errors, TransposedArray *errors_t, double *backprop)
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
NetworkType
Definition: network.h:43
int InitWeights(float range, TRand *randomizer) override
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
int RemapOutputs(int old_no, const std::vector< int > &code_map) override
NetworkType type_
Definition: network.h:299
virtual ~FullyConnected()=default
TrainingState
Definition: network.h:92
void SetEnableTraining(TrainingState state) override
const TransposedArray * external_source_
const STRING & name() const
Definition: network.h:138
StaticShape OutputShape(const StaticShape &input_shape) const override
NetworkType type() const
Definition: network.h:112
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
STRING spec() const override
void ForwardTimeStep(int t, double *output_line)
void FinishBackward(const TransposedArray &errors_t)
Definition: strngs.h:45
void CountAlternators(const Network &other, double *same, double *changed) const override
void Update(float learning_rate, float momentum, float adam_beta, int num_samples) override
void SetupForward(const NetworkIO &input, const TransposedArray *input_transpose)