1. 程式人生 > >sigmoid和softmax函式的C++實現

sigmoid和softmax函式的C++實現

float sigmoid(float x)
{
    return (1 / (1 + exp(-x)));
}
float sigmoid_dy_dz(float x)
{
    return (x * (1.0 - x));
}
float tanh_dy_dz(float x)
{
    return (1.0 - x*x);
}
//對每一行進行softmax
void softmax(float *x, int row, int column)
{
    for (int j = 0; j < row; ++j)
    {
        float max
= 0.0; float sum = 0.0; for (int k = 0; k < column; ++k) if (max < x[k + j*column]) max = x[k + j*column]; for (int k = 0; k < column; ++k) { x[k + j*column] = exp(x[k + j*column] - max); // prevent data overflow sum += x[k + j*column]; } for
(int k = 0; k < column; ++k) x[k + j*column] /= sum; } } //row*column