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
+82 -42
View File
@@ -44,6 +44,7 @@
#include <neural_network/layer/layer_dense.hpp> // fully connected dense layer
#include <neural_network/loss/loss.hpp>
#include <neural_network/activation_loss/activation_softmax_loss_categorical_crossentropy.hpp>
#include <neural_network/optimizers/optimizer.hpp>
#include <neural_network/optimizers/optimizer_sgd.hpp>
@@ -71,44 +72,77 @@ namespace panic{
struct model{
/**
* @brief a pointer to a pointer of layers
* @brief Array of pointers to all model layers.
*
* An example:
* layers[0] points to a layer_dense
* layers[1] points to an actication function
* layers[2] points to another layer_dense
*
* @note The model owns these layers and deletes them in clear().
*
* Example:
* layers[0] points to a layer_dense
* layers[1] points to an activation_ReLU
* layers[2] points to another layer_dense
*
* @note The model owns every object referenced by this array.
* clear() deletes each layer and then deletes the array.
*/
layer** layers;
/**
* @brief Number of layers currently stored in layers.
*/
panic::types::uint_t layer_count;
/**
* @brief Array of pointers to the trainable layers.
*
* @note These pointers refer to objects already owned through layers.
* Do not delete the individual objects through this array.
* Only the pointer array itself is owned separately.
*/
trainable_layer** trainable_layers;
/**
* @brief Number of trainable-layer pointers.
*/
panic::types::uint_t trainable_layer_count;
/**
* @brief Configured loss function.
*
* @note The model owns this object and deletes it in clear().
*/
loss* loss_function;
/**
* @brief Configured optimizer.
*
* @note The model owns this object and deletes it in clear().
*/
optimizer* optimizer_function;
/**
* @brief a pointer the loss function
* @brief Optimized backward helper for the combination of
* Softmax and categorical cross-entropy.
*
*
* @note The model owns these layers and deletes them in clear().
*
* This is a normal member object, not a dynamically allocated object.
*/
loss* loss_function;
activation_softmax_loss_categorical_crossentropy softmax_classifier_output;
// Number of layers currently stored in the model.
/**
* @brief Stores the number of layers
*
* @brief Whether the optimized Softmax + categorical
* cross-entropy backward path should be used.
*/
panic::types::uint_t layer_count;
bool use_softmax_classifier_output;
// model output (may be deleted and also used for debug)
/**
* @brief Output of the final model layer.
*/
panic::tensor::real_matrix outputs;
// model dinputs (may be deleted and also used for debug)
/**
* @brief Gradient with respect to the model input.
*/
panic::tensor::real_matrix dinputs;
/**
@@ -231,16 +265,32 @@ struct model{
*
* Computes:
* @code
* model.bacward(dvalues_data_matrix)
* model.backward(model_output, y_true_values)
* @endcode
*
* @param dvalues diput data.
* @param output Model output.
* @param y_true True values for data.
*
* @return true looped over every layer.
*
*
*/
bool backward(const panic::tensor::real_matrix& dvalues);
bool backward(const panic::tensor::real_matrix& output, const panic::tensor::uint_vector& y_true);
/**
* @brief Loops over all layers backward function
*
* Computes:
* @code
* model.backward(model_output, y_true_values)
* @endcode
*
* @param output Model output.
* @param y_true True values for data.
*
* @return true looped over every layer.
*
*/
bool backward(const panic::tensor::real_matrix& output, const panic::tensor::real_matrix& y_true);
/**
@@ -259,24 +309,6 @@ struct model{
bool add_loss_categorical_crossentropy();
/**
* @brief Adds activation softmax AND loss for categorical crossentropy.
*
* Computes:
* @code
* model.activation_softmax_loss_categorical_crossentropy();
* @endcode
*
*
* @return true if activation and loss is added
*
* @note This function is convenient, but it allocates a new layer.
*/
bool activation_softmax_loss_categorical_crossentropy();
/**
* @brief Adds optimizer_sgd to the model
*
@@ -295,7 +327,15 @@ struct model{
/**
* @brief Finalizes the model configuration.
*
* Detects whether the model can use the optimized
* Softmax + categorical-cross-entropy backward pass.
*
* @return true if the model configuration is valid.
*/
bool finalize();
/**
* @brief Trains the model with input data