Files

141 lines
4.2 KiB
C++

/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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 Matrix for backwards pass
*/
panic::tensor::real_matrix dinputs;
/**
* @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