Derivative

Compute activation function derivative values and write them to the passed vector/matrix

bool vector::Derivative(
  vector&                   vect_out,      // vector to get values
  ENUM_ACTIVATION_FUNCTION  activation,    // activation function
   ...                                     // additional parameters
   );
 
 
bool matrix::Derivative(
  matrix&                   matrix_out,    // matrix to get values
  ENUM_ACTIVATION_FUNCTION  activation,    // activation function
   );
 
 
bool matrix::Derivative(
  matrix&                   matrix_out,    // matrix to get values
  ENUM_ACTIVATION_FUNCTION  activation,    // activation function
  ENUM_MATRIX_AXIS          axis,          // axis
   ...                                     // additional parameters
   );

Parameters

vect_out/matrix_out

 [out]  Vector or matrix to get the computed values of the derivative of the activation function.

activation

[in]  Activation function from the ENUM_ACTIVATION_FUNCTION enumeration.

axis

[in] ENUM_MATRIX_AXIS enumeration value (AXIS_HORZ — horizontal axis, AXIS_VERT — vertical axis).

...

[in]  Additional parameters are the same as that of the activation functions. Only some activation functions accept additional parameters. If no parameters are specified, default values are used.

Return Value

Returns true if successful, otherwise - false.

Note

Function derivatives enable an efficient update of model parameters based on the error received in learning during the error backpropagation.

Unlike most activation functions, Softmax is a vector function: each output element depends on all input vector elements. Therefore, its full derivative is a Jacobian matrix containing both diagonal and cross derivatives.

Derivative(AF_SOFTMAX) calculates only the diagonal elements of the Jacobian matrix using the y_i*(1-y_i) formula. These values cannot be used as a universal element-wise Softmax derivative when backpropagating an arbitrary gradient, because they do not account for mutual influence between vector elements. To calculate the full derivative matrix from already computed Softmax values, use the following function:

The function receives a vector of values produced by Softmax and returns an N x N square Jacobian matrix, where N is the number of input vector elements.

matrix DerivativeSoftmax(const vector& softmax)
 {
 matrix jacobian(softmax.Size(),softmax.Size());
 
//--- calculate Jacobian matrix
 for(ulong i=0; i<softmax.Size(); i++)
 {
 double yi=softmax[i];
 
 for(ulong j=0; j<softmax.Size(); j++)
 {
 double yj=softmax[j];
 
 jacobian[i][j]=-yi*yj;
 if(i==j)
 jacobian[i][j]+=yi;
 }
 }
 
 return(jacobian);
 }

For error backpropagation, the full Jacobian matrix does not have to be built explicitly. The product of this matrix by the incoming gradient vector can be calculated in linear time, which is especially important when working with large vectors.