[math] Adding sigmoid and softmax functions
This commit is contained in:
36
src/logistic.c
Normal file
36
src/logistic.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include "logistic.h"
|
||||||
|
#include "vector_math.h"
|
||||||
|
|
||||||
|
inline double sigmoid(double x) {
|
||||||
|
return 1.0/(1.0 + exp(-x));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void sigmoid_vector(double *x, size_t n) {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
x[i] = sigmoid(x[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void softmax_vector(double *x, size_t n) {
|
||||||
|
int i;
|
||||||
|
double sum = 0.0;
|
||||||
|
|
||||||
|
double denom = double_array_log_sum_exp(x, n);
|
||||||
|
|
||||||
|
for (i = 0; i < n; i++) {
|
||||||
|
x[i] = exp(x[i] - denom);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void softmax_matrix(matrix_t *matrix) {
|
||||||
|
size_t num_rows = matrix->m;
|
||||||
|
size_t num_cols = matrix->n;
|
||||||
|
|
||||||
|
double *values = matrix->values;
|
||||||
|
|
||||||
|
for (int i = 0; i < num_rows; i++) {
|
||||||
|
softmax_vector(values, num_cols);
|
||||||
|
values += num_cols;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/logistic.h
Normal file
14
src/logistic.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef LOGISTIC_H
|
||||||
|
#define LOGISTIC_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
double sigmoid(double x);
|
||||||
|
void sigmoid_vector(double *x, size_t n);
|
||||||
|
void softmax_vector(double *x, size_t n);
|
||||||
|
void softmax_matrix(matrix_t *matrix);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user