Softmax + Categorical Crossentropy

I fixed the activation+loss function, you can't select it directly, it automaticly uses it if it can. I also fixed one-hot generator. Still haven't tested the activation + loss nor any other backward function. I'll do that when I get to the optimizers which is next.
This commit is contained in:
2026-08-06 09:53:03 +02:00
parent fecb4c70b1
commit 642bba1198
18 changed files with 627 additions and 378 deletions
+22 -11
View File
@@ -44,6 +44,11 @@ namespace panic{
namespace neural_network{
enum struct loss_type {
unknown,
categorical_crossentropy
};
/**
* @brief Base loss for the rest of the neural network library to use
@@ -58,7 +63,7 @@ namespace panic{
struct loss{
/**
* @brief Emphty vector to store sample losses
* @brief Emphty vector to store sample losses for each sample in the batch.
*
*/
panic::tensor::real_vector sample_losses;
@@ -66,19 +71,15 @@ struct loss{
/**
* @brief Mean loss over the entire batch.
*/
panic::types::real_t data_loss;
panic::types::real_t data_loss = 0;
/**
* @brief Matrix for backwards pass
* @brief Gradient with respect to the loss input.
*
* This will be used later during the backward pass.
*/
panic::tensor::real_matrix dinputs;
/**
* @brief Matrix for output of loss function
*/
panic::tensor::real_matrix outputs;
/**
* @brief Default de-constructor
*
@@ -86,6 +87,16 @@ struct loss{
virtual ~loss() = default;
/**
* @brief Virtual loss_type function for derivative losses
*
* @Note This returns the type of loss it is
* unknown be default
*/
virtual loss_type get_type() const {
return loss_type::unknown;
}
/**
* @brief Virtual forward function for derivative loss functions
*
@@ -146,7 +157,7 @@ struct loss{
* @param y_true Vector of true label of data.
*
*/
virtual bool calculate(
bool calculate(
const panic::tensor::real_matrix& y_pred,
const panic::tensor::uint_vector& y_true);
@@ -157,7 +168,7 @@ struct loss{
* @param y_true Matrix of true label of data.
*
*/
virtual bool calculate(
bool calculate(
const panic::tensor::real_matrix& y_pred,
const panic::tensor::real_matrix& y_true);
@@ -53,10 +53,17 @@ namespace panic{
*
* The struct is used for PANIC neural_network library.
*/
struct loss_categorical_crossentropy: loss{
struct loss_categorical_crossentropy: public loss{
/**
* @brief get_type function for loss
*
* @returns the loss type
*
*/
loss_type get_type() const override {
return loss_type::categorical_crossentropy;
}
/**
* @brief forward function to calculate losses
@@ -65,9 +72,7 @@ struct loss_categorical_crossentropy: loss{
* @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)override;
bool forward(const panic::tensor::real_matrix& y_pred, const panic::tensor::uint_vector& y_true) override;
/**
* @brief forward function to calculate losses
@@ -78,9 +83,7 @@ struct loss_categorical_crossentropy: loss{
* @Note Overloaded if one-shot endcoded
* is used.
*/
bool forward(
const panic::tensor::real_matrix& y_pred,
const panic::tensor::real_matrix& y_true)override;
bool forward(const panic::tensor::real_matrix& y_pred, const panic::tensor::real_matrix& y_true) override;
/**
@@ -90,9 +93,7 @@ struct loss_categorical_crossentropy: loss{
* @param y_true Vector of true label of data.
*
*/
bool backward(
const panic::tensor::real_matrix& dvalues,
const panic::tensor::uint_vector& y_true) override;
bool backward(const panic::tensor::real_matrix& dvalues, const panic::tensor::uint_vector& y_true) override;
/**
* @brief backward function to calculate from losses
@@ -103,9 +104,7 @@ struct loss_categorical_crossentropy: loss{
* @Note Overloaded if one-shot endcoded
* is used.
*/
bool backward(
const panic::tensor::real_matrix& dvalues,
const panic::tensor::real_matrix& y_true) override;
bool backward(const panic::tensor::real_matrix& dvalues, const panic::tensor::real_matrix& y_true) override;
};