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

102 lines
3.5 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 "activation_softmax.h"
gctl::softmax::softmax() {}
gctl::softmax::~softmax() {}
void gctl::softmax::activate(const matrix<double> &z, matrix<double> &a)
{
// a = activation(z) = softmax(z)
// Z = [z1, ..., zn], A = [a1, ..., an], n observations
a.resize(z.row_size(), z.col_size());
double max_z, row_s;
int i, j;
#pragma omp parallel for private (i, j, max_z, row_s) schedule(guided)
for (j = 0; j < a.col_size(); j++)
{
max_z = z[0][j];
for (i = 0; i < a.row_size(); i++)
{
max_z = max_z>z[i][j]?max_z:z[i][j];
}
row_s = 0.0;
for (i = 0; i < a.row_size(); i++)
{
row_s += std::exp(z[i][j] - max_z);
}
for (i = 0; i < a.row_size(); i++)
{
a[i][j] = std::exp(z[i][j] - max_z)/row_s;
}
}
return;
}
void gctl::softmax::apply_jacobian(const matrix<double> &z,
const matrix<double> &a, const matrix<double> &f, matrix<double> &g)
{
// Apply the Jacobian matrix J to a vector f
// J = d_a / d_z = diag(a) - a * a'
// g = J * f = a .* f - a * (a' * f) = a .* (f - a'f)
// Z = [z1, ..., zn], G = [g1, ..., gn], F = [f1, ..., fn]
// Note: When entering this function, Z and G may point to the same matrix
g.resize(a.row_size(), a.col_size());
double af;
int i, j;
#pragma omp parallel for private (i, j, af) schedule(guided)
for (j = 0; j < g.col_size(); j++)
{
af = 0.0;
for (i = 0; i < g.row_size(); i++)
{
af += a[i][j]*f[i][j];
}
for (i = 0; i < g.row_size(); i++)
{
g[i][j] = a[i][j]*(f[i][j] - af);
}
}
return;
}
std::string gctl::softmax::activation_name() const
{
return "SoftMax";
}
gctl::activation_type_e gctl::softmax::activation_type() const
{
return SoftMax;
}