C++ 实现激活函数

转载请注明作者和出处: http://blog.csdn.net/john_bh/

文章目录

    • 1. Sigmoid
    • 2. Tanh
    • 3. Softmax
    • 4. Argmax

1. Sigmoid

float sigmoid(float x)
{
    return (1 / (1 + exp(-x)));
}

float sigmoid_dy_dz(float x)
{
    return (x * (1.0 - x));
}

static inline float Sigmoid(float x) {
	return (1 / (1 + exp(-x)));
}

static inline float * Sigmoid(float array[], int32_t size)[] {
	float output_array[size];
	for (int32_t i = 0; i < size; i++) {
	    float x = array[i];
	    output_array[i] = (1 / (1 + exp(-x)));
	}
	return output_array;
}

static inline int32_t ActivationT(int32_t value, int32_t threshold) {
	return value > threshold ? 1 : 0;
}

2. Tanh

float tanh_dy_dz(float x)
{
    return (1.0 - x*x);
}

3. Softmax

//对每一行进行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

OpenCV DNN下实现softmax:

int softmax(const cv::Mat & src, cv::Mat & dst)
{
	float max = 0.0;
	float sum = 0.0;

	max = *max_element(src.begin<float>(), src.end<float>());
	cv::exp((src - max), dst);
	sum = cv::sum(dst)[0];
	dst /= sum;
	
	return 0;
}

4. Argmax

static inline int32_t Argmax(float array[], int32_t size) {
float max_value = 0.0f;
int32_t max_index = -1;
for (int32_t i = 0; i < size; i++) {
    if (array[i] > max_value) {
    max_value = array[i];
    max_index = i;
    }
}
return max_index;
}

你可能感兴趣的:(C++,C++,实现激活函数,C++,实现sigmoid,C++,实现,softmax,C++,实现tanh,C++)