-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmodel.py
104 lines (71 loc) · 2.51 KB
/
model.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import time
def _mean_squared_error(y, pred):
return 0.5 * np.mean((y - pred) ** 2)
def _mean_abs_error(y, pred):
return np.mean(np.abs(y, pred))
def _sigmoid(x):
return 1. / (1. + np.exp(-x))
def _fourier(x):
return np.sin(x)
def _hardlimit(x):
return (x >= 0).astype(int)
def _identity(x):
return x
def getActivation(name):
return {
'sigmoid': _sigmoid,
'fourier': _fourier,
'hardlimit': _hardlimit
}[name]
def getLoss(name):
return {
'mse': _mean_squared_error,
'mae': _mean_abs_error
}[name]
class ELM:
def __init__(self, num_input_nodes, num_hidden_units, num_out_units, activation='sigmoid',
loss='mse', beta_init=None, w_init=None, bias_init=None):
self._num_input_nodes = num_input_nodes
self._num_hidden_units = num_hidden_units
self._num_out_units = num_out_units
self._activation = getActivation(activation)
self._loss = getLoss(loss)
if isinstance(beta_init, np.ndarray):
self._beta = beta_init
else:
self._beta = np.random.uniform(-1., 1., size=(self._num_hidden_units, self._num_out_units))
if isinstance(w_init, np.ndarray):
self._w = w_init
else:
self._w = np.random.uniform(-1, 1, size=(self._num_input_nodes, self._num_hidden_units))
if isinstance(bias_init, np.ndarray):
self._bias = bias_init
else:
self._bias = np.zeros(shape=(self._num_hidden_units,))
print('Bias shape:', self._bias.shape)
print('W shape:', self._w.shape)
print('Beta shape:', self._beta.shape)
def fit(self, X, Y, display_time=False):
H = self._activation(X.dot(self._w) + self._bias)
# Moore–Penrose pseudo inverse
if display_time:
start = time.time()
H_pinv = np.linalg.pinv(H)
if display_time:
stop = time.time()
print(f'Train time: {stop-start}')
self._beta = H_pinv.dot(Y)
# print('Fit Beta shape:', self._beta.shape)
def __call__(self, X):
H = self._activation(X.dot(self._w) + self._bias)
return H.dot(self._beta)
def evaluate(self, X, Y):
pred = self(X)
# Loss (base on model setting)
loss = self._loss(Y, pred)
# Accuracy
acc = np.sum(np.argmax(pred, axis=-1) == np.argmax(Y, axis=-1)) / len(Y)
# Unweighted Average Recall
# TODO
return loss, acc