tesseract  4.00.00dev
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();
32 
33  // Returns the shape output from the network given an input shape (which may
34  // be partially unknown ie zero).
35  virtual StaticShape OutputShape(const StaticShape& input_shape) const;
36 
37  virtual STRING spec() const {
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  virtual void SetEnableTraining(TrainingState state);
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  virtual int InitWeights(float range, TRand* randomizer);
71 
72  // Converts a float network to an int network.
73  virtual void ConvertToInt();
74 
75  // Provides debug output on the weights.
76  virtual void DebugWeights();
77 
78  // Writes to the given file. Returns false in case of error.
79  virtual bool Serialize(TFile* fp) const;
80  // Reads from the given file. Returns false in case of error.
81  virtual bool DeSerialize(TFile* fp);
82 
83  // Runs forward propagation of activations on the input line.
84  // See Network for a detailed discussion of the arguments.
85  virtual void Forward(bool debug, const NetworkIO& input,
86  const TransposedArray* input_transpose,
87  NetworkScratch* scratch, NetworkIO* output);
88  // Components of Forward so FullyConnected can be reused inside LSTM.
89  void SetupForward(const NetworkIO& input,
90  const TransposedArray* input_transpose);
91  void ForwardTimeStep(const double* d_input, const inT8* i_input, int t,
92  double* output_line);
93 
94  // Runs backward propagation of errors on the deltas line.
95  // See Network for a detailed discussion of the arguments.
96  virtual bool Backward(bool debug, const NetworkIO& fwd_deltas,
97  NetworkScratch* scratch,
98  NetworkIO* back_deltas);
99  // Components of Backward so FullyConnected can be reused inside LSTM.
100  void BackwardTimeStep(const NetworkIO& fwd_deltas, int t, double* curr_errors,
101  TransposedArray* errors_t, double* backprop);
102  void FinishBackward(const TransposedArray& errors_t);
103 
104  // Updates the weights using the given learning rate and momentum.
105  // num_samples is the quotient to be used in the adagrad computation iff
106  // use_ada_grad_ is true.
107  virtual void Update(float learning_rate, float momentum, int num_samples);
108  // Sums the products of weight updates in *this and other, splitting into
109  // positive (same direction) in *same and negative (different direction) in
110  // *changed.
111  virtual void CountAlternators(const Network& other, double* same,
112  double* changed) const;
113 
114  protected:
115  // Weight arrays of size [no, ni + 1].
117  // Transposed copy of input used during training of size [ni, width].
119  // Pointer to transposed input stored elsewhere. If not null, this is used
120  // in preference to calculating the transpose and storing it in source_t_.
122  // Activations from forward pass of size [width, no].
124  // Memory of the integer mode input to forward as softmax always outputs
125  // float, so the information is otherwise lost.
126  bool int_mode_;
127 };
128 
129 } // namespace tesseract.
130 
131 
132 
133 #endif // TESSERACT_LSTM_FULLYCONNECTED_H_
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
FullyConnected(const STRING &name, int ni, int no, NetworkType type)
virtual void Update(float learning_rate, float momentum, int num_samples)
virtual STRING spec() const
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
NetworkType type() const
Definition: network.h:112
TrainingState
Definition: network.h:92
void ChangeType(NetworkType type)
virtual bool DeSerialize(TFile *fp)
Definition: strngs.h:45
virtual void SetEnableTraining(TrainingState state)
void SetupForward(const NetworkIO &input, const TransposedArray *input_transpose)
NetworkType
Definition: network.h:43
int8_t inT8
Definition: host.h:34
NetworkType type_
Definition: network.h:285
void ForwardTimeStep(const double *d_input, const inT8 *i_input, int t, double *output_line)
const STRING & name() const
Definition: network.h:138
virtual StaticShape OutputShape(const StaticShape &input_shape) const
const TransposedArray * external_source_
virtual bool Serialize(TFile *fp) const
virtual void CountAlternators(const Network &other, double *same, double *changed) const
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
void FinishBackward(const TransposedArray &errors_t)
virtual int InitWeights(float range, TRand *randomizer)
void BackwardTimeStep(const NetworkIO &fwd_deltas, int t, double *curr_errors, TransposedArray *errors_t, double *backprop)