Making loss_categorical_crossentropy
This commit is contained in:
@@ -0,0 +1,316 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: math
|
||||
* File Name: mean.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 30-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate mean of tensor arrays
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/mean.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
#include <math/sum.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t mean_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean
|
||||
//
|
||||
// Description:
|
||||
// Find the mean value for a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::vector<T>& a){
|
||||
|
||||
T sum = panic::math::sum(a);
|
||||
|
||||
return sum / static_cast<T>(a.size());
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t mean<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t mean<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t mean<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean
|
||||
//
|
||||
// Description:
|
||||
// Find the mean value for a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::matrix<T>& A){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
T sum = panic::math::sum(A);
|
||||
|
||||
return sum / static_cast<T>(work);
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t mean<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t mean<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t mean<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Find the mean row-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool mean_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(rows) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sum_rowwise(A, b)){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Each thread handles separate rows and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(rows > mean_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
b[i] /= cols;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool mean_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool mean_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool mean_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Returns row-wise sum values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> mean_rowwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!mean_rowwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::vector<panic::types::uint_t>
|
||||
mean_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
mean_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
mean_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean_colwise
|
||||
//
|
||||
// Description:
|
||||
// Find the mean column-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool mean_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! sum_colwise(A,b)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each thread handles separate cols and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > mean_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < cols; ++i){
|
||||
b[i] /= cols;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool mean_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool mean_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool mean_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::mean_colwise
|
||||
//
|
||||
// Description:
|
||||
// Returns column-wise mean values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> mean_colwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!mean_colwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::vector<panic::types::uint_t>
|
||||
mean_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
mean_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
mean_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
Reference in New Issue
Block a user