gctl_ai/lib/dnn/olayer_multientropy.cpp

163 lines
5.2 KiB
C++
Raw Normal View History

2024-09-10 20:15:33 +08:00
/********************************************************
*
*
*
*
*
*
* 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 "olayer_multientropy.h"
gctl::multi_entropy::multi_entropy() {}
gctl::multi_entropy::~multi_entropy() {}
void gctl::multi_entropy::check_target_data(const matrix<double> &target)
{
int nobs = target.col_size();
int ncls = target.row_size();
int one;
for (size_t i = 0; i < nobs; i++)
{
one = 0;
for (size_t j = 0; j < ncls; j++)
{
if (fabs(target[j][i] - 1.0) < 1e-6)
{
one++;
continue;
}
if (fabs(target[j][i]) > 1e-6)
{
throw std::invalid_argument("[gctl::multi_entropy] Target data should only contain zero or one.");
}
}
if (one != 1)
{
throw std::invalid_argument("[gctl::multi_entropy] Each column of target data should only contain one \"1\".");
}
}
return;
}
void gctl::multi_entropy::check_target_data(const array<int> &target)
{
int nobs = target.size();
for (size_t i = 0; i < nobs; i++)
{
if (target[i] < 0)
{
throw std::invalid_argument("[gctl::multi_entropy] Target data must be non-negative.");
}
}
return;
}
void gctl::multi_entropy::evaluation(const matrix<double> &prev_layer_data, const matrix<double> &target)
{
int nobs = prev_layer_data.col_size();
int ncls = prev_layer_data.row_size();
if (target.col_size() != nobs || target.row_size() != ncls)
{
throw std::invalid_argument("[gctl::multi_entropy] Target data have incorrect dimension.");
}
// Compute the derivative of the input of this layer
// L = -sum(log(phat) * y)
// in = phat
// d(L) / d(in) = -y / phat
der_in_.resize(ncls, nobs, 0.0);
int i, j;
#pragma omp parallel for private (i, j) schedule(guided)
for (i = 0; i < ncls; i++)
{
for (j = 0; j < nobs; j++)
{
der_in_[i][j] = -1.0*target[i][j]/prev_layer_data[i][j];
}
}
// L = -sum(log(phat) * y)
// in = phat
// d(L) / d(in) = -y / phat
// m_din contains 0 if y = 0, and -1/phat if y = 1
loss_ = 0.0;
//#pragma omp parallel for private (i, j) schedule(guided)
for (i = 0; i < ncls; i++)
{
for (j = 0; j < nobs; j++)
{
loss_ -= std::log(prev_layer_data[i][j])*target[i][j];
}
}
loss_ /= der_in_.col_size();
return;
}
void gctl::multi_entropy::evaluation(const matrix<double> &prev_layer_data, const array<int> &target)
{
// target is a vector of class labels that take values from [0, 1, ..., nclass - 1]
// The i-th element of target is the class label for observation i
int nobs = prev_layer_data.col_size();
int ncls = prev_layer_data.row_size();
if (target.size() != nobs)
{
throw std::invalid_argument("[gctl::multi_entropy] Target data have incorrect dimension.");
}
// Compute the derivative of the input of this layer
// L = -log(phat[y])
// in = phat
// d(L) / d(in) = [0, 0, ..., -1/phat[y], 0, ..., 0]
der_in_.resize(ncls, nobs);
der_in_.assign_all(0.0);
int i;
#pragma omp parallel for private (i) schedule(guided)
for (i = 0; i < nobs; i++)
{
der_in_[target[i]][i] = -1.0/prev_layer_data[target[i]][i];
}
return;
}
double gctl::multi_entropy::loss_value() const
{
return loss_;
}
std::string gctl::multi_entropy::get_output_name() const
{
return "MultiClassEntropy";
}
gctl::olayer_type_e gctl::multi_entropy::get_output_type() const
{
return MultiClassEntropy;
}