gctl_ai/lib/dnn/hlayer.cpp
2025-02-09 21:26:19 +08:00

120 lines
3.6 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.
******************************************************/
#include "hlayer.h"
gctl::dnn_hlayer::dnn_hlayer(){}
gctl::dnn_hlayer::~dnn_hlayer()
{
delete activator_;
}
int gctl::dnn_hlayer::in_size() const
{
return w_is_;
}
int gctl::dnn_hlayer::out_size() const
{
return w_outs_;
}
int gctl::dnn_hlayer::obs_size() const
{
return o_is_;
}
const gctl::matrix<double> &gctl::dnn_hlayer::forward_propagation_data()
{
return a_;
}
const gctl::matrix<double> &gctl::dnn_hlayer::backward_propagation_data()
{
return der_in_;
}
const gctl::matrix<double> &gctl::dnn_hlayer::get_linear_term()
{
return z_;
}
const gctl::matrix<double> &gctl::dnn_hlayer::get_linear_gradient()
{
return der_z_;
}
gctl::activation_type_e gctl::dnn_hlayer::get_activation_type() const
{
return activator_->activation_type();
}
std::string gctl::dnn_hlayer::get_activation_name() const
{
return activator_->activation_name();
}
void gctl::dnn_hlayer::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)
{
idx.resize(p_rows*p_cols);
idx.assign(-1);
int row_st = i*s_rows;
int col_st = j*s_cols;
for (size_t r = 0; r < p_rows; r++)
{
for (size_t c = 0; c < p_cols; c++)
{
idx[c + r*p_cols] = (row_st + r)*i_cols + col_st + c;
}
}
return;
}
void gctl::dnn_hlayer::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)
{
idx.resize(p_rows*p_cols);
idx.assign(-1);
int row_st = i*s_rows - u_pad;
int col_st = j*s_cols - l_pad;
for (size_t r = 0; r < p_rows; r++)
{
for (size_t c = 0; c < p_cols; c++)
{
if (row_st + r >= 0 && row_st + r < i_rows &&
col_st + c >= 0 && col_st + c < i_cols)
{
idx[c + r*p_cols] = (row_st + r)*i_cols + col_st + c;
}
}
}
return;
}