File: _logistic_sigmoid.pyx

package info (click to toggle)
scikit-learn 0.23.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,892 kB
  • sloc: python: 132,020; cpp: 5,765; javascript: 2,201; ansic: 831; makefile: 213; sh: 44
file content (33 lines) | stat: -rw-r--r-- 823 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#cython: boundscheck=False
#cython: cdivision=True
#cython: wraparound=False

from libc.math cimport log, exp

import numpy as np
cimport numpy as np

np.import_array()
ctypedef np.float64_t DTYPE_t


cdef inline DTYPE_t _inner_log_logistic_sigmoid(const DTYPE_t x):
    """Log of the logistic sigmoid function log(1 / (1 + e ** -x))"""
    if x > 0:
        return -log(1. + exp(-x))
    else:
        return x - log(1. + exp(x))


def _log_logistic_sigmoid(unsigned int n_samples,
                          unsigned int n_features,
                          DTYPE_t[:, :] X,
                          DTYPE_t[:, :] out):
    cdef:
        unsigned int i
        unsigned int j

    for i in range(n_samples):
        for j in range(n_features):
            out[i, j] = _inner_log_logistic_sigmoid(X[i, j])
    return out