-
Notifications
You must be signed in to change notification settings - Fork 0
/
elm.py
196 lines (177 loc) · 7.28 KB
/
elm.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Final edited date: 2018.3.7
# Author: Li Xudong, from NSSC.CAS Beijing
# Version: 1.04
# Updated By: Hafiz Zaini - particularly to run Cable Fault detection Algorithm
# Description: Extreme Learning Machine (ELM) class
# Methods:
# fit(algorithm)
# predict(x)
# score(x, y)
import numpy as np
from scipy.linalg import pinv, inv
import time
class elm():
'''
Function: elm class init
-------------------
Parameters:
shape: list, shape[hidden units, output units]
numbers of hidden units and output units
activation_function: str, 'sigmoid', 'relu', 'sin', 'tanh' or 'leaky_relu'
Activation function of neurals
x: array, shape[samples, features]
train data
y: array, shape[samples, ]
labels
C: float
regularization parameter
elm_type: str, 'clf' or 'reg'
'clf' means ELM solve classification problems, 'reg' means ELM solve regression problems.
one_hot: bool, Ture or False, default True
The parameter is useful only when elm_type == 'clf'. If the labels need to transformed to
one_hot, this parameter is set to be True
random_type: str, 'uniform' or 'normal', default:'normal'
Weight initialization method
'''
def __init__(self, hidden_units, activation_function, x, y, C, elm_type, one_hot=True, random_type='normal'):
self.hidden_units = hidden_units
self.activation_function = activation_function
self.random_type = random_type
self.x = x
self.y = y
self.C = C
self.class_num = np.unique(self.y).shape[0]
self.beta = np.zeros((self.hidden_units, self.class_num))
self.elm_type = elm_type
self.one_hot = one_hot
# if classification problem and one_hot == True
if elm_type == 'clf' and self.one_hot:
self.one_hot_label = np.zeros((self.y.shape[0], self.class_num))
for i in range(self.y.shape[0]):
self.one_hot_label[i, int(self.y[i])] = 1
# Randomly generate the weight matrix and bias vector from input to hidden layer
# 'uniform': uniform distribution
# 'normal': normal distribution
if self.random_type == 'uniform':
self.W = np.random.uniform(low=0, high=1, size=(self.hidden_units, self.x.shape[1]))
self.b = np.random.uniform(low=0, high=1, size=(self.hidden_units, 1))
if self.random_type == 'normal':
self.W = np.random.normal(loc=0, scale=0.5, size=(self.hidden_units, self.x.shape[1]))
self.b = np.random.normal(loc=0, scale=0.5, size=(self.hidden_units, 1))
# compute the output of hidden layer according to different activation function
def __input2hidden(self, x):
self.temH = np.dot(self.W, x.T) + self.b
if self.activation_function == 'sigmoid':
self.H = 1/(1 + np.exp(- self.temH))
if self.activation_function == 'relu':
self.H = self.temH * (self.temH > 0)
if self.activation_function == 'sin':
self.H = np.sin(self.temH)
if self.activation_function == 'tanh':
self.H = (np.exp(self.temH) - np.exp(-self.temH))/(np.exp(self.temH) + np.exp(-self.temH))
if self.activation_function == 'leaky_relu':
self.H = np.maximum(0, self.temH) + 0.1 * np.minimum(0, self.temH)
return self.H
# compute the output
def __hidden2output(self, H):
self.output = np.dot(H.T, self.beta)
return self.output
'''
Function: Train the model, compute beta matrix, the weight matrix from hidden layer to output layer
------------------
Parameter:
algorithm: str, 'no_re', 'solution1' or 'solution2'
The algorithm to compute beta matrix
------------------
Return:
beta: array
the weight matrix from hidden layer to output layer
train_score: float
the accuracy or RMSE
train_time: str
time of computing beta
'''
def fit(self, algorithm):
#self.time1 = time.clock() # compute running time
#self.time1 = time.perf_counter
self.time1 = time.process_time()
self.H = self.__input2hidden(self.x)
if self.elm_type == 'clf':
if self.one_hot:
self.y_temp = self.one_hot_label
else:
self.y_temp = self.y
if self.elm_type == 'reg':
self.y_temp = self.y
# no regularization
if algorithm == 'no_re':
self.beta = np.dot(pinv(self.H.T), self.y_temp)
# faster algorithm 1
if algorithm == 'solution1':
self.tmp1 = inv(np.eye(self.H.shape[0])/self.C + np.dot(self.H, self.H.T))
self.tmp2 = np.dot(self.tmp1, self.H)
self.beta = np.dot(self.tmp2, self.y_temp)
# faster algorithm 2
if algorithm == 'solution2':
self.tmp1 = inv(np.eye(self.H.shape[0])/self.C + np.dot(self.H, self.H.T))
self.tmp2 = np.dot(self.H.T, self.tmp1)
self.beta = np.dot(self.tmp2.T, self.y_temp)
#self.time2 = time.clock()
#self.time2 = time.perf_counter()
self.time2 = time.process_time()
# compute the results
self.result = self.__hidden2output(self.H)
# If the problem if classification problem, the output is softmax
if self.elm_type == 'clf':
self.result = np.exp(self.result)/np.sum(np.exp(self.result), axis=1).reshape(-1, 1)
# Evaluate training results
# If problem is classification, compute the accuracy
# If problem is regression, compute the RMSE
if self.elm_type == 'clf':
self.y_ = np.where(self.result == np.max(self.result, axis=1).reshape(-1, 1))[1]
self.correct = 0
for i in range(self.y.shape[0]):
if self.y_[i] == self.y[i]:
self.correct += 1
self.train_score = self.correct/self.y.shape[0]
if self.elm_type == 'reg':
self.train_score = np.sqrt(np.sum((self.result - self.y) * (self.result - self.y))/self.y.shape[0])
train_time = str(self.time2 - self.time1)
return self.beta, self.train_score, train_time
'''
Function: compute the result given data
---------------
Parameters:
x: array, shape[samples, features]
---------------
Return:
y_: array
predicted results
'''
def predict(self, x):
self.H = self.__input2hidden(x)
self.y_ = self.__hidden2output(self.H)
if self.elm_type == 'clf':
self.y_ = np.where(self.y_ == np.max(self.y_, axis=1).reshape(-1, 1))[1]
return self.y_
'''
Function: compute accuracy or RMSE given data and labels
-------------
Parameters:
x: array, shape[samples, features]
y: array, shape[samples, ]
-------------
Return:
test_score: float, accuracy or RMSE
'''
def score(self, x, y):
self.prediction = self.predict(x)
if self.elm_type == 'clf':
self.correct = 0
for i in range(y.shape[0]):
if self.prediction[i] == y[i]:
self.correct += 1
self.test_score = self.correct/y.shape[0]
if self.elm_type == 'reg':
self.test_score = np.sqrt(np.sum((self.result - self.y) * (self.result - self.y))/self.y.shape[0])
return self.test_score