gctl_ai/lib/dnn/hlayer.h
2024-09-10 20:15:33 +08:00

99 lines
4.2 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_DNN_HLAYER_H
#define _GCTL_DNN_HLAYER_H
#include "activation_identity.h"
#include "activation_mish.h"
#include "activation_relu.h"
#include "activation_prelu.h"
#include "activation_sigmoid.h"
#include "activation_softmax.h"
#include "activation_tanh.h"
namespace gctl
{
enum hlayer_type_e
{
FullyConnected,
MaxPooling,
AvgPooling,
Convolution,
};
enum pad_type_e
{
Valid,
Same,
};
class dnn_hlayer
{
public:
dnn_hlayer();
virtual ~dnn_hlayer();
int in_size() const;
int out_size() const;
int obs_size() const;
const matrix<double> &forward_propagation_data();
const matrix<double> &backward_propagation_data();
const matrix<double> &get_linear_term();
const matrix<double> &get_linear_gradient();
activation_type_e get_activation_type() const;
std::string get_activation_name() const;
virtual void forward_propagation(const array<double> &all_weights, const matrix<double> &prev_layer_data) = 0;
virtual void backward_propagation(const array<double> &all_weights, const array<double> &all_ders,
const matrix<double> &prev_layer_data, const matrix<double> &next_layer_data) = 0;
virtual hlayer_type_e get_layer_type() const = 0;
virtual std::string get_layer_name() const = 0;
virtual std::string layer_info() const = 0;
virtual void save_layer_setup(std::ofstream &os) const = 0;
virtual void load_layer_setup(std::ifstream &is) = 0;
virtual void save_weights2text(const array<double> &all_weights, std::ofstream &os) const = 0;
protected:
void cal_valid_padding_idx(array<int> &idx, int i, int j, int i_rows, int i_cols, int p_rows, int p_cols, int s_rows, int s_cols);
void cal_same_padding_idx(array<int> &idx, int i, int j, int i_rows, int i_cols, int p_rows, int p_cols, int s_rows, int s_cols, int u_pad, int l_pad);
protected:
dnn_activation *activator_; // activation object
int w_is_, w_outs_; // weight start index, weight input size, weight output size
int o_is_; // observation input size
matrix<double> z_; // Linear term, z = W' * in + b
matrix<double> a_; // Output of this layer, a_ = act(z)
matrix<double> der_in_; // Derivative of the input of this layer, which is also the output of previous layer
matrix<double> der_z_; // Derivative of the linear term
};
}
#endif // _GCTL_DNN_HLAYER_H