2016-11-08 07:38:07 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// File: fullyconnected.h
|
|
|
|
// Description: Simple feed-forward layer with various non-linearities.
|
|
|
|
// Author: Ray Smith
|
|
|
|
//
|
|
|
|
// (C) Copyright 2014, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef TESSERACT_LSTM_FULLYCONNECTED_H_
|
|
|
|
#define TESSERACT_LSTM_FULLYCONNECTED_H_
|
|
|
|
|
|
|
|
#include "network.h"
|
|
|
|
#include "networkscratch.h"
|
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
|
|
|
|
// C++ Implementation of the Softmax (output) class from lstm.py.
|
|
|
|
class FullyConnected : public Network {
|
2021-03-13 03:35:02 +08:00
|
|
|
public:
|
2020-12-31 21:31:10 +08:00
|
|
|
TESS_API
|
2021-03-13 03:35:02 +08:00
|
|
|
FullyConnected(const std::string &name, int ni, int no, NetworkType type);
|
2019-03-26 14:37:52 +08:00
|
|
|
~FullyConnected() override = default;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Returns the shape output from the network given an input shape (which may
|
|
|
|
// be partially unknown ie zero).
|
2021-03-13 03:35:02 +08:00
|
|
|
StaticShape OutputShape(const StaticShape &input_shape) const override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
2021-03-13 06:09:52 +08:00
|
|
|
std::string spec() const override {
|
|
|
|
std::string spec;
|
2016-11-08 07:38:07 +08:00
|
|
|
if (type_ == NT_TANH)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Ft" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_LOGISTIC)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fs" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_RELU)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fr" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_LINEAR)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fl" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_POSCLIP)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fp" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_SYMCLIP)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fn" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else if (type_ == NT_SOFTMAX)
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fc" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
else
|
2021-03-13 06:09:52 +08:00
|
|
|
spec += "Fm" + std::to_string(no_);
|
2016-11-08 07:38:07 +08:00
|
|
|
return spec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes the type to the given type. Used to commute a softmax to a
|
|
|
|
// non-output type for adding on other networks.
|
|
|
|
void ChangeType(NetworkType type) {
|
|
|
|
type_ = type;
|
|
|
|
}
|
|
|
|
|
2016-12-01 07:51:17 +08:00
|
|
|
// Suspends/Enables training by setting the training_ flag. Serialize and
|
|
|
|
// DeSerialize only operate on the run-time data if state is false.
|
2018-04-28 03:07:20 +08:00
|
|
|
void SetEnableTraining(TrainingState state) override;
|
2016-12-01 07:51:17 +08:00
|
|
|
|
2016-11-08 07:38:07 +08:00
|
|
|
// Sets up the network for training. Initializes weights using weights of
|
|
|
|
// scale `range` picked according to the random number generator `randomizer`.
|
2021-03-13 03:35:02 +08:00
|
|
|
int InitWeights(float range, TRand *randomizer) override;
|
2017-09-08 17:24:00 +08:00
|
|
|
// Recursively searches the network for softmaxes with old_no outputs,
|
|
|
|
// and remaps their outputs according to code_map. See network.h for details.
|
2021-03-13 03:35:02 +08:00
|
|
|
int RemapOutputs(int old_no, const std::vector<int> &code_map) override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Converts a float network to an int network.
|
2018-04-28 03:07:20 +08:00
|
|
|
void ConvertToInt() override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Provides debug output on the weights.
|
2018-04-28 03:07:20 +08:00
|
|
|
void DebugWeights() override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Writes to the given file. Returns false in case of error.
|
2021-03-13 03:35:02 +08:00
|
|
|
bool Serialize(TFile *fp) const override;
|
2016-11-08 07:38:07 +08:00
|
|
|
// Reads from the given file. Returns false in case of error.
|
2021-03-13 03:35:02 +08:00
|
|
|
bool DeSerialize(TFile *fp) override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Runs forward propagation of activations on the input line.
|
|
|
|
// See Network for a detailed discussion of the arguments.
|
2021-03-13 03:35:02 +08:00
|
|
|
void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose,
|
|
|
|
NetworkScratch *scratch, NetworkIO *output) override;
|
2016-11-08 07:38:07 +08:00
|
|
|
// Components of Forward so FullyConnected can be reused inside LSTM.
|
2021-03-13 03:35:02 +08:00
|
|
|
void SetupForward(const NetworkIO &input, const TransposedArray *input_transpose);
|
|
|
|
void ForwardTimeStep(int t, double *output_line);
|
|
|
|
void ForwardTimeStep(const double *d_input, int t, double *output_line);
|
|
|
|
void ForwardTimeStep(const int8_t *i_input, int t, double *output_line);
|
2016-11-08 07:38:07 +08:00
|
|
|
|
|
|
|
// Runs backward propagation of errors on the deltas line.
|
|
|
|
// See Network for a detailed discussion of the arguments.
|
2021-03-13 03:35:02 +08:00
|
|
|
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch,
|
|
|
|
NetworkIO *back_deltas) override;
|
2016-11-08 07:38:07 +08:00
|
|
|
// Components of Backward so FullyConnected can be reused inside LSTM.
|
2021-03-13 03:35:02 +08:00
|
|
|
void BackwardTimeStep(const NetworkIO &fwd_deltas, int t, double *curr_errors,
|
|
|
|
TransposedArray *errors_t, double *backprop);
|
|
|
|
void FinishBackward(const TransposedArray &errors_t);
|
2016-11-08 07:38:07 +08:00
|
|
|
|
2017-08-03 05:03:50 +08:00
|
|
|
// Updates the weights using the given learning rate, momentum and adam_beta.
|
|
|
|
// num_samples is used in the adam computation iff use_adam_ is true.
|
2021-03-13 03:35:02 +08:00
|
|
|
void Update(float learning_rate, float momentum, float adam_beta, int num_samples) override;
|
2016-11-08 07:38:07 +08:00
|
|
|
// Sums the products of weight updates in *this and other, splitting into
|
|
|
|
// positive (same direction) in *same and negative (different direction) in
|
|
|
|
// *changed.
|
2021-03-13 03:35:02 +08:00
|
|
|
void CountAlternators(const Network &other, double *same, double *changed) const override;
|
2016-11-08 07:38:07 +08:00
|
|
|
|
2021-03-13 03:35:02 +08:00
|
|
|
protected:
|
2016-11-08 07:38:07 +08:00
|
|
|
// Weight arrays of size [no, ni + 1].
|
|
|
|
WeightMatrix weights_;
|
|
|
|
// Transposed copy of input used during training of size [ni, width].
|
|
|
|
TransposedArray source_t_;
|
|
|
|
// Pointer to transposed input stored elsewhere. If not null, this is used
|
|
|
|
// in preference to calculating the transpose and storing it in source_t_.
|
2021-03-13 03:35:02 +08:00
|
|
|
const TransposedArray *external_source_;
|
2016-11-08 07:38:07 +08:00
|
|
|
// Activations from forward pass of size [width, no].
|
|
|
|
NetworkIO acts_;
|
|
|
|
// Memory of the integer mode input to forward as softmax always outputs
|
|
|
|
// float, so the information is otherwise lost.
|
|
|
|
bool int_mode_;
|
|
|
|
};
|
|
|
|
|
2021-03-13 03:35:02 +08:00
|
|
|
} // namespace tesseract.
|
2016-11-08 07:38:07 +08:00
|
|
|
|
2021-03-13 03:35:02 +08:00
|
|
|
#endif // TESSERACT_LSTM_FULLYCONNECTED_H_
|