Making loss_categorical_crossentropy
This commit is contained in:
@@ -702,6 +702,106 @@ panic::tensor::matrix<T> clip_higher_colwise(const panic::tensor::matrix<T>& A,
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips values in the vector or scalar.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param lower Scalar value compared to each element of @p a.
|
||||
* @param higher Scalar value compared to each element of @p a.
|
||||
* @param c Output vector. Resized to match @p a.
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if resizing @p c failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip(const panic::tensor::vector<T>& a, const T lower, const T higher, const panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a cliped value in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param lower Scalar value compared to each element of @p a.
|
||||
* @param higher Scalar value compared to each element of @p a.
|
||||
*
|
||||
* @return A new vector containing the result.
|
||||
* @return An empty vector if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> clip(const panic::tensor::vector<T>& a, const T lower, const T higher);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param lower Scalar value compared to each element of @p a.
|
||||
* @param higher Scalar value compared to each element of @p a.
|
||||
* @param C Output Matrix. Resized to match @p A.
|
||||
*
|
||||
* @return true if @p C was resized and filled successfully.
|
||||
* @return false if resizing @p C failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing matrix to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip(const panic::tensor::matrix<T>& A, const T lower, const T higher, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the cliped min elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param lower Scalar value compared to each element of @p a.
|
||||
* @param higher Scalar value compared to each element of @p a.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> clip(const panic::tensor::matrix<T>& A, const T lower, const T higher);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: log.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate the logorithem of numbers
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief calculates the exponential of a value.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = exp(k)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param k Value to take the exp of.
|
||||
*
|
||||
* @return The calculated value
|
||||
*
|
||||
* @note This function is omp-friendly.
|
||||
*/
|
||||
template <typename T>
|
||||
T exp(const T x);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* c[i] = exp(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param c Output vector. Resized to match @p a.
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if resizing @p c failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[i] = exp(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
*
|
||||
* @return A new vector containing the result.
|
||||
* @return An empty vector if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Calculates the expnential elementwise of a matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(i,j) = exp(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output Matrix. Resized to match @p A.
|
||||
*
|
||||
* @return true if @p C was resized and filled successfully.
|
||||
* @return false if resizing @p C failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns the calculated ecponential elementwise of the matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result(i,j) = exp(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> exp(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,159 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 30-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate mean of tensor arrays
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the mean value of a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = mean(a)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
*
|
||||
* @return A new vector containing the result.
|
||||
* @return An empty vector if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Returns the mean value of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = mean(A)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::matrix<T>& A);
|
||||
|
||||
/**
|
||||
* @brief Find the mean values row-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* c(i) = mean(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param c Output vector. Resized to match @p A.rows().
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool mean_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the mean row-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[i] = mean(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> mean_rowwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Find the mean values column-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(j) = mean(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output matrix. Resized to match @p A.cols().
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool mean_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the mean column-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[j] = mean(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> mean_colwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,134 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: neural_network
|
||||
* File Name: loss.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 30-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the base loss struct used in other loss functions in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
|
||||
#include <tensor/matrix.hpp> // panic::tensor::real_matrix (uint_matrix, int_matrix)
|
||||
#include <tensor/vector.hpp>
|
||||
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Base loss for the rest of the neural network library to use
|
||||
*
|
||||
* This base layer should be used in all loss functions.
|
||||
* This is done so it's easy to make new loss functions in the model.
|
||||
* The virtual means it should use derived object's version when called with a pointer.
|
||||
* The derived object can use overloading on forward, or just use one of them, to support one-shot encoding.
|
||||
*
|
||||
* The struct is used for PANIC neural_network library.
|
||||
*/
|
||||
struct loss{
|
||||
|
||||
/**
|
||||
* @brief Emphty vector to store sample losses
|
||||
*
|
||||
*/
|
||||
panic::tensor::real_vector sample_losses;
|
||||
|
||||
/**
|
||||
* @brief Mean loss over the entire batch.
|
||||
*/
|
||||
panic::types::real_t data_loss;
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
*
|
||||
*/
|
||||
virtual ~loss() = default;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Virtual forward function for derivative loss functions
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Vector of true label of data.
|
||||
*
|
||||
* @Note If the derivatived object does not use this,
|
||||
* it returns false.
|
||||
*/
|
||||
virtual bool forward(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::uint_vector& y_true);
|
||||
|
||||
/**
|
||||
* @brief Virtual forward function for derivative loss functions
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Matrix of true label of data.
|
||||
*
|
||||
* @Note If the derivatived object does not use this,
|
||||
* it returns false.
|
||||
*/
|
||||
virtual bool forward(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::real_matrix& y_true);
|
||||
|
||||
/**
|
||||
* @brief Virtual calculate function that calculates the loss
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Vector of true label of data.
|
||||
*
|
||||
*/
|
||||
bool calculate(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::uint_vector& y_true);
|
||||
|
||||
/**
|
||||
* @brief Virtual calculate function that calculates the loss
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Matrix of true label of data.
|
||||
*
|
||||
*/
|
||||
bool calculate(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::real_matrix& y_true);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace neural_network
|
||||
} // namespace panic
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: neural_network
|
||||
* File Name: loss_categorical_crossentropy.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 30-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the base loss_categorical_crossentropy used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <neural_network/loss/loss.hpp>
|
||||
#include <config/omp.hpp>
|
||||
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
|
||||
#include <tensor/matrix.hpp> // panic::tensor::real_matrix (uint_matrix, int_matrix)
|
||||
#include <tensor/vector.hpp>
|
||||
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief loss_categorical_crossentropy used in the rest of the neural network library
|
||||
*
|
||||
*
|
||||
* The struct is used for PANIC neural_network library.
|
||||
*/
|
||||
struct loss_categorical_crossentropy: loss{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief forward function to calculate losses
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Vector of true label of data.
|
||||
*
|
||||
*/
|
||||
bool forward(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::uint_vector& y_true);
|
||||
|
||||
/**
|
||||
* @brief forward function to calculate losses
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Vector of true label of data.
|
||||
*
|
||||
* @Note Overloaded if one-shot endcoded
|
||||
* is used.
|
||||
*/
|
||||
bool forward(
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::real_matrix& y_true);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
} // namespace neural_network
|
||||
} // namespace panic
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user