-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathsigmoidal.py
62 lines (53 loc) · 1.64 KB
/
sigmoidal.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
class SigmoidalFeature(object):
"""
Sigmoidal features
1 / (1 + exp((m - x) @ c)
"""
def __init__(self, mean, coef=1):
"""
construct sigmoidal features
Parameters
----------
mean : (n_features, ndim) or (n_features,) ndarray
center of sigmoid function
coef : (ndim,) ndarray or int or float
coefficient to be multplied with the distance
"""
if mean.ndim == 1:
mean = mean[:, None]
else:
assert mean.ndim == 2
if isinstance(coef, int) or isinstance(coef, float):
if np.size(mean, 1) == 1:
coef = np.array([coef])
else:
raise ValueError("mismatch of dimension")
else:
assert coef.ndim == 1
assert np.size(mean, 1) == len(coef)
self.mean = mean
self.coef = coef
def _sigmoid(self, x, mean):
return np.tanh((x - mean) @ self.coef * 0.5) * 0.5 + 0.5
def transform(self, x):
"""
transform input array with sigmoidal features
Parameters
----------
x : (sample_size, ndim) or (sample_size,) ndarray
input array
Returns
-------
output : (sample_size, n_features) ndarray
sigmoidal features
"""
if x.ndim == 1:
x = x[:, None]
else:
assert x.ndim == 2
assert np.size(x, 1) == np.size(self.mean, 1)
basis = [np.ones(len(x))]
for m in self.mean:
basis.append(self._sigmoid(x, m))
return np.asarray(basis).transpose()