Save
Maybe backward on activation+loss works
This commit is contained in:
+2
-82
@@ -59,19 +59,6 @@ struct activation_softmax_loss_categorical_crossentropy: loss{
|
|||||||
activation_softmax activation;
|
activation_softmax activation;
|
||||||
loss_categorical_crossentropy loss;
|
loss_categorical_crossentropy loss;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Emphty matrix to store input data for bacward pass
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
panic::tensor::real_matrix dinputs;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Emphty matrix to store output data
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
panic::tensor::real_matrix outputs;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Empthy constructor
|
* @brief Empthy constructor
|
||||||
@@ -92,7 +79,7 @@ struct activation_softmax_loss_categorical_crossentropy: loss{
|
|||||||
* @param y_true Vector of true label of data.
|
* @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) override;
|
bool forward(const panic::tensor::real_matrix& y_pred, const panic::tensor::uint_vector& y_true) override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief forward function to calculate losses
|
* @brief forward function to calculate losses
|
||||||
@@ -103,74 +90,7 @@ struct activation_softmax_loss_categorical_crossentropy: loss{
|
|||||||
* @Note Overloaded if one-shot endcoded
|
* @Note Overloaded if one-shot endcoded
|
||||||
* is used.
|
* is used.
|
||||||
*/
|
*/
|
||||||
bool calculate(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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief forward function to calculate losses
|
|
||||||
*
|
|
||||||
* @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 forward function to calculate 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 calculate(const panic::tensor::real_matrix& y_pred, const panic::tensor::real_matrix& y_true);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -112,6 +112,33 @@ struct loss{
|
|||||||
const panic::tensor::real_matrix& y_pred,
|
const panic::tensor::real_matrix& y_pred,
|
||||||
const panic::tensor::real_matrix& y_true);
|
const panic::tensor::real_matrix& y_true);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Virtual backward 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 backward(
|
||||||
|
const panic::tensor::real_matrix& y_pred,
|
||||||
|
const panic::tensor::uint_vector& y_true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Virtual backward 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 backward(
|
||||||
|
const panic::tensor::real_matrix& y_pred,
|
||||||
|
const panic::tensor::real_matrix& y_true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Virtual calculate function that calculates the loss
|
* @brief Virtual calculate function that calculates the loss
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ struct loss_categorical_crossentropy: loss{
|
|||||||
*/
|
*/
|
||||||
bool forward(
|
bool forward(
|
||||||
const panic::tensor::real_matrix& y_pred,
|
const panic::tensor::real_matrix& y_pred,
|
||||||
const panic::tensor::uint_vector& y_true);
|
const panic::tensor::uint_vector& y_true)override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief forward function to calculate losses
|
* @brief forward function to calculate losses
|
||||||
@@ -80,7 +80,7 @@ struct loss_categorical_crossentropy: loss{
|
|||||||
*/
|
*/
|
||||||
bool forward(
|
bool forward(
|
||||||
const panic::tensor::real_matrix& y_pred,
|
const panic::tensor::real_matrix& y_pred,
|
||||||
const panic::tensor::real_matrix& y_true);
|
const panic::tensor::real_matrix& y_true)override;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+24
-51
@@ -70,39 +70,14 @@ namespace panic{
|
|||||||
activation_softmax_loss_categorical_crossentropy::activation_softmax_loss_categorical_crossentropy() {
|
activation_softmax_loss_categorical_crossentropy::activation_softmax_loss_categorical_crossentropy() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.calculate
|
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.forward
|
||||||
//
|
//
|
||||||
// Description:
|
// Description:
|
||||||
// Calculated the calculate pass
|
// Calculated the forward pass
|
||||||
//--------------------------------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
bool activation_softmax_loss_categorical_crossentropy::calculate(const panic::tensor::real_matrix& y_pred, const panic::tensor::uint_vector& y_true){
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
data_loss = loss.data_loss;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------------------------------
|
|
||||||
// Function Name : panic::neural_network::activation_softmax_loss_categorical_crossentropy.calculate
|
|
||||||
//
|
|
||||||
// Description:
|
|
||||||
// Calculated the calculate pass
|
|
||||||
//--------------------------------------------------------------------------------------------------------------------------
|
|
||||||
bool activation_softmax_loss_categorical_crossentropy::calculate(const panic::tensor::real_matrix& y_pred, const panic::tensor::real_matrix& y_true){
|
|
||||||
|
|
||||||
// Output layers activation function
|
// Output layers activation function
|
||||||
if (!activation.forward(y_pred)){
|
if (!activation.forward(y_pred)){
|
||||||
@@ -115,34 +90,32 @@ bool activation_softmax_loss_categorical_crossentropy::calculate(const panic::te
|
|||||||
if (!loss.calculate(outputs, y_true)){
|
if (!loss.calculate(outputs, y_true)){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
data_loss = loss.data_loss;
|
|
||||||
|
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -136,6 +136,34 @@ bool loss::forward(
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
|
// Function Name : panic::neural_network::loss::backward
|
||||||
|
//
|
||||||
|
// Description:
|
||||||
|
// Default implementation. Derived classes can override it.
|
||||||
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
|
bool loss::backward(
|
||||||
|
const panic::tensor::real_matrix& y_pred,
|
||||||
|
const panic::tensor::uint_vector& y_true){
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
|
// Function Name : panic::neural_network::loss::backward
|
||||||
|
//
|
||||||
|
// Description:
|
||||||
|
// Default matrix-target implementation. Derived classes can override it.
|
||||||
|
//--------------------------------------------------------------------------------------------------------------------------
|
||||||
|
bool loss::backward(
|
||||||
|
const panic::tensor::real_matrix& y_pred,
|
||||||
|
const panic::tensor::real_matrix& y_true){
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace tensor
|
} // namespace tensor
|
||||||
} // namespace panic
|
} // namespace panic
|
||||||
|
|
||||||
|
|||||||
@@ -463,7 +463,7 @@ bool model::train(const panic::tensor::real_matrix& X_train,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
loss_function.backward(loss_function.outputs, y_train);
|
loss_function->backward(loss_function->outputs, y_train);
|
||||||
|
|
||||||
//backward();
|
//backward();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user