Done with activation softmax backwards
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: argmax.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to find the index of the maximum value;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <config/types.hpp>
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the index of maximum value of a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @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::types::uint_t argmax(const panic::tensor::vector<T>& a);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the index of the maximum value of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param b Output vector for index
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
bool argmax(const panic::tensor::matrix<T>& A, panic::tensor::uint_vector& b);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the index of the maximum value of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @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>
|
||||
panic::types::uint_t argmax(const panic::tensor::matrix<T>& A);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Find the index of the maximum values row-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output vector with indexes. 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 argmax_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::uint_vector& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the index of the maiximum row-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @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::uint_vector argmax_rowwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Find the index of the maximum values column-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output matrix. 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 argmax_colwise(const panic::tensor::matrix<T>& A, panic::tensor::uint_vector& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the index of the maiximum column-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @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::uint_vector argmax_colwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,102 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: dot.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate dot product of tensors;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp> // for panic::real_matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
/**
|
||||
* @brief Returns dor product of vector and vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector for dot product. Need to be @p a.size()
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
T dot(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector of dot product of a matrix and vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param b Input vector. @ p b.size needs to be the size of @p A.cols.
|
||||
* @param c Output vector with result.
|
||||
*
|
||||
* @return true if success
|
||||
* @return false if resizeing fails or dimention mismatch
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
bool dot(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& b, panic::tensor::vector<T>& c);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector of dot product of a matrix and vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param b Input vector. @ p b.size needs to be the size of @p A.cols.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty vector if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> dot(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& b);
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,294 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: equal.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to find the equal in panic::tensor
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* @file mul.hpp
|
||||
* @brief Public API for multiplying operations on PANIC vectors and matrices.
|
||||
*
|
||||
* This header contains the declarations that users of the math module should call.
|
||||
* The comments here describe how each function is used, what dimensions are required,
|
||||
* and what is returned on failure.
|
||||
*
|
||||
* Implementation details, OpenMP thresholds, and explicit template instantiations are
|
||||
* kept in mul.cpp.
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
/**
|
||||
* @brief Find the equal for a scalar to every element of a vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k 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 equal(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector containing a scalar equaled to every element.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k 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> equal(const panic::tensor::vector<T>& a, const T k);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Equal a vector elementwise too a vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector. Must have size 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 vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool equal(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector containing a vector with equals elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector. Must have size of @p a
|
||||
* @param k 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> equal(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Equal compared a scalar to every element of a matix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value multiplied 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 vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool equal(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a scalar equal compared to every element.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value multiplied 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 vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> equal(const panic::tensor::matrix<T>& A, const T k);
|
||||
|
||||
/**
|
||||
* @brief Equal compared a matrix elementwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A First matrix.
|
||||
* @param B Second matrix. Must have size 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 vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool equal(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a matrix equal compaed elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param B Second matrix. Must have size 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 vector.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> equal(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Equal compared a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Matrix.
|
||||
* @param b Vector. Must have size of @p A.cols().
|
||||
* @param C Output matrix. Resized to match @p A.
|
||||
*
|
||||
* @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 equal_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Equal compared a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Matrix.
|
||||
* @param b Vector. Must have size of @p A.cols().
|
||||
*
|
||||
* @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> equal_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
/**
|
||||
* @brief Equal compared a vector colwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Matrix.
|
||||
* @param b Vector. Must have size of @p A.rows().
|
||||
* @param C Output matrix. Resized to match @p A.
|
||||
*
|
||||
* @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 equal_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Equal compared a vector colwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Matrix.
|
||||
* @param b Vector. Must have size of @p A.rows().
|
||||
*
|
||||
* @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> equal_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -99,7 +99,7 @@ bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
* @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);
|
||||
panic::tensor::vector<T> exp(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Calculates the expnential elementwise of a matrix
|
||||
|
||||
+18
-18
@@ -24,11 +24,11 @@
|
||||
* Module Name: math
|
||||
* File Name: log.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate the logorithem of numbers
|
||||
* Functions to calculate the natural logorithem of numbers
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
@@ -42,30 +42,30 @@ namespace math{
|
||||
|
||||
|
||||
/**
|
||||
* @brief calculates the exponential of a value.
|
||||
* @brief calculates the log of a value.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = exp(k)
|
||||
* result = log(k)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param k Value to take the exp of.
|
||||
* @param x Value to take the log of.
|
||||
*
|
||||
* @return The calculated value
|
||||
*
|
||||
* @note This function is omp-friendly.
|
||||
*/
|
||||
template <typename T>
|
||||
T exp(const T x);
|
||||
T log(const T x);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
* @brief Calculates the natrual logorithmic elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* c[i] = exp(a[i])
|
||||
* c[i] = log(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
@@ -79,14 +79,14 @@ T exp(const T x);
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
bool log(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
* @brief Calculates the natrual log elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[i] = exp(a[i])
|
||||
* result[i] = log(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
@@ -98,14 +98,14 @@ bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
* @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);
|
||||
panic::tensor::vector<T> log(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Calculates the expnential elementwise of a matrix
|
||||
* @brief Calculates the natrual log elementwise of a matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(i,j) = exp(A(i,j))
|
||||
* C(i,j) = log(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
@@ -119,14 +119,14 @@ panic::tensor::vector<T> add(const panic::tensor::vector<T>& a);
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& C);
|
||||
bool log(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns the calculated ecponential elementwise of the matrix
|
||||
* @brief Returns the calculated natrual log elementwise of the matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result(i,j) = exp(A(i,j))
|
||||
* result(i,j) = log(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
@@ -138,7 +138,7 @@ bool exp(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& C);
|
||||
* @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);
|
||||
panic::tensor::matrix<T> log(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -33,6 +33,7 @@
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <config/types.hpp>
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
@@ -60,7 +61,7 @@ namespace math{
|
||||
* @note This overload is convenient, but may allocate a new vector.
|
||||
*/
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::vector<T>& a);
|
||||
panic::types::real_t mean(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Returns the mean value of a matrix.
|
||||
@@ -78,7 +79,7 @@ T mean(const panic::tensor::vector<T>& a);
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
T mean(const panic::tensor::matrix<T>& A);
|
||||
panic::types::real_t mean(const panic::tensor::matrix<T>& A);
|
||||
|
||||
/**
|
||||
* @brief Find the mean values row-wise of a matrix.
|
||||
@@ -96,7 +97,7 @@ T mean(const panic::tensor::matrix<T>& A);
|
||||
* @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);
|
||||
bool mean_rowwise(const panic::tensor::real_matrix& A, panic::tensor::real_vector& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the mean row-wise elementwise.
|
||||
@@ -115,7 +116,7 @@ bool mean_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c
|
||||
* @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);
|
||||
panic::tensor::real_vector mean_rowwise(const panic::tensor::real_matrix& A);
|
||||
|
||||
|
||||
/**
|
||||
@@ -134,7 +135,7 @@ panic::tensor::vector<T> mean_rowwise(const panic::tensor::matrix<T>& A);
|
||||
* @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);
|
||||
bool mean_colwise(const panic::tensor::real_matrix& A, panic::tensor::real_vector& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the mean column-wise elementwise.
|
||||
@@ -153,7 +154,7 @@ bool mean_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c
|
||||
* @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);
|
||||
panic::tensor::real_vector mean_colwise(const panic::tensor::real_matrix& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,86 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: transpose.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to transpose a matrix;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <tensor/matrix.hpp> // for panic::real_matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
/**
|
||||
* @brief Transposes a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* B = transpose(A)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param B Output matrix with result.
|
||||
*
|
||||
* @return true if success
|
||||
* @return false if resizeing fails or dimention mismatch
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
bool transpose(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& B);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix of the transposed A.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = transpose(A)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty vector if the operation fails.
|
||||
*
|
||||
* @note This overload is convenient, but may allocate a new matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> transpose(const panic::tensor::matrix<T>& A);
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -77,7 +77,7 @@ struct activation_relu : public layer{
|
||||
*
|
||||
* @Note Calculates -> outputs = inputs * weights + biases
|
||||
*/
|
||||
bool forward(const panic::tensor::real_matrix& inputs);
|
||||
bool forward(const panic::tensor::real_matrix& input_data);
|
||||
|
||||
/**
|
||||
* @brief Backward function for layer
|
||||
@@ -86,7 +86,7 @@ struct activation_relu : public layer{
|
||||
*
|
||||
* @Note Calculates derivative of forward function.
|
||||
*/
|
||||
bool backward(const panic::tensor::real_matrix& dinpus);
|
||||
bool backward(const panic::tensor::real_matrix& dvalues);
|
||||
};
|
||||
|
||||
} // namespace tensor
|
||||
|
||||
@@ -77,7 +77,7 @@ struct activation_softmax : public layer{
|
||||
*
|
||||
* @Note Calculates -> outputs = inputs * weights + biases
|
||||
*/
|
||||
bool forward(const panic::tensor::real_matrix& inputs);
|
||||
bool forward(const panic::tensor::real_matrix& input_data);
|
||||
|
||||
/**
|
||||
* @brief Backward function for layer
|
||||
@@ -86,7 +86,7 @@ struct activation_softmax : public layer{
|
||||
*
|
||||
* @Note Calculates derivative of forward function.
|
||||
*/
|
||||
bool backward(const panic::tensor::real_matrix& dinpus);
|
||||
bool backward(const panic::tensor::real_matrix& dvalues);
|
||||
};
|
||||
|
||||
} // namespace tensor
|
||||
|
||||
@@ -56,6 +56,14 @@ namespace panic{
|
||||
*/
|
||||
struct layer{
|
||||
|
||||
/**
|
||||
* @brief Emphty output matrix to store layer input
|
||||
*
|
||||
* Output shape:
|
||||
* samples x neuron_count
|
||||
*/
|
||||
panic::tensor::real_matrix inputs;
|
||||
|
||||
/**
|
||||
* @brief Emphty output matrix to store layer output
|
||||
*
|
||||
@@ -64,6 +72,12 @@ struct layer{
|
||||
*/
|
||||
panic::tensor::real_matrix outputs;
|
||||
|
||||
/**
|
||||
* @brief Emphty matrix to store output for backward pass
|
||||
*
|
||||
*/
|
||||
panic::tensor::real_matrix dinputs;
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
*
|
||||
|
||||
@@ -58,6 +58,12 @@ namespace panic{
|
||||
*/
|
||||
struct layer_dense : public layer{
|
||||
|
||||
/**
|
||||
* @brief Emphty matrix to store input data
|
||||
*
|
||||
*/
|
||||
panic::tensor::real_matrix ipnuts;
|
||||
|
||||
/**
|
||||
* @brief Emphty weight matrix to store layer weights
|
||||
*
|
||||
@@ -65,6 +71,7 @@ struct layer_dense : public layer{
|
||||
* input_size x neuron_count
|
||||
*/
|
||||
panic::tensor::real_matrix weights;
|
||||
panic::tensor::real_matrix dweights;
|
||||
|
||||
/**
|
||||
* @brief Emphty bias vector to store layer bias
|
||||
@@ -73,6 +80,7 @@ struct layer_dense : public layer{
|
||||
* 1 x neuron_count
|
||||
*/
|
||||
panic::tensor::real_vector biases;
|
||||
panic::tensor::real_vector dbiases;
|
||||
|
||||
/**
|
||||
* @brief Empthy constructor
|
||||
@@ -102,7 +110,7 @@ struct layer_dense : public layer{
|
||||
*
|
||||
* @Note Calculates -> outputs = inputs * weights + biases
|
||||
*/
|
||||
bool forward(const panic::tensor::real_matrix& inputs);
|
||||
bool forward(const panic::tensor::real_matrix& input_data);
|
||||
|
||||
/**
|
||||
* @brief Backward function for layer
|
||||
@@ -111,7 +119,7 @@ struct layer_dense : public layer{
|
||||
*
|
||||
* @Note Calculates derivative of forward function.
|
||||
*/
|
||||
bool backward(const panic::tensor::real_matrix& dinpus);
|
||||
bool backward(const panic::tensor::real_matrix& dvalues);
|
||||
};
|
||||
|
||||
} // namespace tensor
|
||||
|
||||
@@ -68,6 +68,12 @@ struct loss{
|
||||
*/
|
||||
panic::types::real_t data_loss;
|
||||
|
||||
/**
|
||||
* @brief Matrix for backwards pass
|
||||
*/
|
||||
panic::tensor::real_matrix dinputs;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
*
|
||||
|
||||
@@ -82,9 +82,35 @@ struct loss_categorical_crossentropy: loss{
|
||||
const panic::tensor::real_matrix& y_pred,
|
||||
const panic::tensor::real_matrix& y_true);
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief backward function to calculate from losses
|
||||
*
|
||||
* @param y_pred Matrix of model predection.
|
||||
* @param y_true Vector of true label of data.
|
||||
*
|
||||
*/
|
||||
bool backward(
|
||||
const panic::tensor::real_matrix& dvalues,
|
||||
const panic::tensor::uint_vector& y_true);
|
||||
|
||||
/**
|
||||
* @brief backward function to calculate from 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 backward(
|
||||
const panic::tensor::real_matrix& dvalues,
|
||||
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: tensor
|
||||
* File Name: eye.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to generate eye matrices
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/matrix.hpp> // for panic::vector
|
||||
#include <config/types.hpp>
|
||||
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
/**
|
||||
* @brief Outputs an eye matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param size Number or rows and columns.
|
||||
* @param A Output matrix
|
||||
*
|
||||
* @return true if @p A was resized and filled successfully.
|
||||
* @return false if resizing @p A failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool eye(const panic::types::uint_t size, panic::tensor::matrix<T> A);
|
||||
|
||||
/**
|
||||
* @brief Returns a eye matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @param T Numeric element type.
|
||||
* @param size Number of rows and columns.
|
||||
*
|
||||
* @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::matrix<T> eye(const T size);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,91 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: tensor
|
||||
* File Name: one_hot.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 31-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to one_hot matrices
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp> // for panic::vector
|
||||
#include <config/types.hpp>
|
||||
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
/**
|
||||
* @brief Outputs an one_hot matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param size Number of columns.
|
||||
* @param a Input vector that contains here 1 should be
|
||||
* @param B Output matrix
|
||||
*
|
||||
* @return true if @p B was resized and filled successfully.
|
||||
* @return false if resizing @p B failed.
|
||||
*
|
||||
* @note This overload writes the result into an existing vector to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool one_hot(const panic::types::uint_t size, const panic::tensor::uint_vector& a, panic::tensor::matrix<T>& B);
|
||||
|
||||
/**
|
||||
* @brief Returns a one_hot matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* @endcode
|
||||
*
|
||||
* @param T Numeric element type.
|
||||
* @param size Number of columns.
|
||||
* @param a Input vector that contains here 1 should be
|
||||
*
|
||||
* @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::matrix<T> one_hot(const panic::types::uint_t size, const panic::tensor::vector<T>& a);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
Reference in New Issue
Block a user