Save
Trying do make the backward to work
This commit is contained in:
+2
-2
@@ -92,7 +92,7 @@ struct activation_softmax_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);
|
||||
bool calculate(const panic::tensor::real_matrix& y_pred, const panic::tensor::uint_vector& y_true) override;
|
||||
|
||||
/**
|
||||
* @brief forward function to calculate losses
|
||||
@@ -103,7 +103,7 @@ struct activation_softmax_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);
|
||||
bool calculate(const panic::tensor::real_matrix& y_pred, const panic::tensor::real_matrix& y_true) override;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -73,6 +73,11 @@ struct loss{
|
||||
*/
|
||||
panic::tensor::real_matrix dinputs;
|
||||
|
||||
/**
|
||||
* @brief Matrix for output of loss function
|
||||
*/
|
||||
panic::tensor::real_matrix outputs;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Default de-constructor
|
||||
|
||||
@@ -92,7 +92,7 @@ struct loss_categorical_crossentropy: loss{
|
||||
*/
|
||||
bool backward(
|
||||
const panic::tensor::real_matrix& dvalues,
|
||||
const panic::tensor::uint_vector& y_true);
|
||||
const panic::tensor::uint_vector& y_true) override;
|
||||
|
||||
/**
|
||||
* @brief backward function to calculate from losses
|
||||
@@ -105,7 +105,7 @@ struct loss_categorical_crossentropy: loss{
|
||||
*/
|
||||
bool backward(
|
||||
const panic::tensor::real_matrix& dvalues,
|
||||
const panic::tensor::real_matrix& y_true);
|
||||
const panic::tensor::real_matrix& y_true) override;
|
||||
|
||||
|
||||
};
|
||||
|
||||
-59
@@ -70,65 +70,6 @@ namespace panic{
|
||||
activation_softmax_loss_categorical_crossentropy::activation_softmax_loss_categorical_crossentropy() {
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.forward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the forward pass
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_softmax_loss_categorical_crossentropy::forward(const panic::tensor::real_matrix& y_pred, const panic::tensor::uint_vector& y_true){
|
||||
|
||||
// Output layers activation function
|
||||
if (!activation.forward(y_pred)){
|
||||
return false;
|
||||
}
|
||||
// Set the output
|
||||
outputs = activation.outputs;
|
||||
|
||||
// calculate the loss value.
|
||||
if (!loss.calculate(outputs, y_true)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.forward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the forward pass
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_softmax_loss_categorical_crossentropy::forward(const panic::tensor::real_matrix& y_pred, const panic::tensor::real_matrix& y_true){
|
||||
|
||||
// Output layers activation function
|
||||
if (!activation.forward(y_pred)){
|
||||
return false;
|
||||
}
|
||||
// Set the output
|
||||
outputs = activation.outputs;
|
||||
|
||||
// calculate the loss value.
|
||||
if (!loss.calculate(outputs, y_true)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.calculate
|
||||
//
|
||||
|
||||
@@ -77,7 +77,10 @@ bool loss::calculate(
|
||||
// Calculate mean loss
|
||||
data_loss = panic::math::mean(sample_losses);
|
||||
|
||||
//return calculate_mean();
|
||||
// This is so the model can use every loss functions
|
||||
// It's a problem to get the output when useing an activation+loss function
|
||||
outputs = y_pred;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -97,6 +100,11 @@ bool loss::calculate(
|
||||
|
||||
data_loss = panic::math::mean(sample_losses);
|
||||
|
||||
|
||||
// This is so the model can use every loss functions
|
||||
// It's a problem to get the output when useing an activation+loss function
|
||||
outputs = y_pred;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -127,38 +135,6 @@ bool loss::forward(
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::loss::calculate_mean
|
||||
//
|
||||
// Description:
|
||||
// Calculates the average of all sample loss values.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool loss::calculate_mean(){
|
||||
|
||||
if (sample_losses.size() == 0){
|
||||
return false;
|
||||
}
|
||||
|
||||
panic::types::real_t sum =
|
||||
static_cast<panic::types::real_t>(0);
|
||||
|
||||
for (
|
||||
panic::types::uint_t i = 0;
|
||||
i < sample_losses.size();
|
||||
++i
|
||||
){
|
||||
sum += sample_losses[i];
|
||||
}
|
||||
|
||||
data_loss =
|
||||
sum /
|
||||
static_cast<panic::types::real_t>(sample_losses.size());
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
@@ -450,7 +450,7 @@ bool model::train(const panic::tensor::real_matrix& X_train,
|
||||
}
|
||||
|
||||
|
||||
prediction = panic::math::argmax_rowwise(outputs);
|
||||
prediction = panic::math::argmax_rowwise(loss_function->outputs);
|
||||
|
||||
comparisons = panic::math::equal(prediction, y_train);
|
||||
|
||||
@@ -463,6 +463,8 @@ bool model::train(const panic::tensor::real_matrix& X_train,
|
||||
}
|
||||
|
||||
|
||||
loss_function.backward(loss_function.outputs, y_train);
|
||||
|
||||
//backward();
|
||||
|
||||
//optimize();
|
||||
|
||||
Reference in New Issue
Block a user