tesseract  5.0.0-alpha-619-ge9db
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  ~FullyConnected() override = 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_
tesseract::FullyConnected::external_source_
const TransposedArray * external_source_
Definition: fullyconnected.h:124
tesseract::FullyConnected::InitWeights
int InitWeights(float range, TRand *randomizer) override
Definition: fullyconnected.cpp:76
tesseract::FullyConnected::source_t_
TransposedArray source_t_
Definition: fullyconnected.h:121
tesseract::StaticShape
Definition: static_shape.h:38
tesseract::NT_POSCLIP
Definition: network.h:63
tesseract::FullyConnected::FinishBackward
void FinishBackward(const TransposedArray &errors_t)
Definition: fullyconnected.cpp:288
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tesseract::FullyConnected::~FullyConnected
~FullyConnected() override=default
tesseract::FullyConnected::Update
void Update(float learning_rate, float momentum, float adam_beta, int num_samples) override
Definition: fullyconnected.cpp:297
tesseract::FullyConnected::DeSerialize
bool DeSerialize(TFile *fp) override
Definition: fullyconnected.cpp:112
tesseract::FullyConnected::Backward
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: fullyconnected.cpp:220
tesseract::FullyConnected::SetupForward
void SetupForward(const NetworkIO &input, const TransposedArray *input_transpose)
Definition: fullyconnected.cpp:172
tesseract::FullyConnected::CountAlternators
void CountAlternators(const Network &other, double *same, double *changed) const override
Definition: fullyconnected.cpp:305
tesseract::FullyConnected::RemapOutputs
int RemapOutputs(int old_no, const std::vector< int > &code_map) override
Definition: fullyconnected.cpp:86
tesseract::FullyConnected::ChangeType
void ChangeType(NetworkType type)
Definition: fullyconnected.h:60
STRING
Definition: strngs.h:45
tesseract::NetworkScratch
Definition: networkscratch.h:34
tesseract::WeightMatrix
Definition: weightmatrix.h:65
network.h
tesseract::Network::type
NetworkType type() const
Definition: network.h:112
tesseract::NetworkType
NetworkType
Definition: network.h:43
tesseract::NT_SYMCLIP
Definition: network.h:64
tesseract::FullyConnected::SetEnableTraining
void SetEnableTraining(TrainingState state) override
Definition: fullyconnected.cpp:60
tesseract::FullyConnected::BackwardTimeStep
void BackwardTimeStep(const NetworkIO &fwd_deltas, int t, double *curr_errors, TransposedArray *errors_t, double *backprop)
Definition: fullyconnected.cpp:264
networkscratch.h
tesseract::FullyConnected::Forward
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
Definition: fullyconnected.cpp:118
tesseract::FullyConnected
Definition: fullyconnected.h:28
tesseract::Network::type_
NetworkType type_
Definition: network.h:293
tesseract::FullyConnected::weights_
WeightMatrix weights_
Definition: fullyconnected.h:119
tesseract::FullyConnected::ForwardTimeStep
void ForwardTimeStep(int t, double *output_line)
Definition: fullyconnected.cpp:184
tesseract::FullyConnected::OutputShape
StaticShape OutputShape(const StaticShape &input_shape) const override
Definition: fullyconnected.cpp:45
tesseract::NT_TANH
Definition: network.h:65
tesseract::TFile
Definition: serialis.h:75
tesseract::NetworkIO
Definition: networkio.h:39
tesseract::FullyConnected::FullyConnected
FullyConnected(const STRING &name, int ni, int no, NetworkType type)
Definition: fullyconnected.cpp:38
tesseract::FullyConnected::Serialize
bool Serialize(TFile *fp) const override
Definition: fullyconnected.cpp:105
tesseract
Definition: baseapi.h:65
tesseract::FullyConnected::acts_
NetworkIO acts_
Definition: fullyconnected.h:126
tesseract::Network
Definition: network.h:105
tesseract::FullyConnected::ConvertToInt
void ConvertToInt() override
Definition: fullyconnected.cpp:95
tesseract::FullyConnected::spec
STRING spec() const override
Definition: fullyconnected.h:37
tesseract::Network::name
const STRING & name() const
Definition: network.h:138
tesseract::NT_RELU
Definition: network.h:66
tesseract::TransposedArray
Definition: weightmatrix.h:32
tesseract::TrainingState
TrainingState
Definition: network.h:92
tesseract::NT_LINEAR
Definition: network.h:67
tesseract::NT_LOGISTIC
Definition: network.h:62
tesseract::Network::no_
int32_t no_
Definition: network.h:298
tesseract::TRand
Definition: helpers.h:50
tesseract::FullyConnected::DebugWeights
void DebugWeights() override
Definition: fullyconnected.cpp:100
tesseract::FullyConnected::int_mode_
bool int_mode_
Definition: fullyconnected.h:129
tesseract::NT_SOFTMAX
Definition: network.h:68