-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextreme_learning_machine_classifier.m
78 lines (65 loc) · 2.72 KB
/
extreme_learning_machine_classifier.m
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
classdef extreme_learning_machine_classifier
%EXTREME_LEARNING_MACHINE_CLASSIFIER Builds a classification extreme
%learning machine.
properties
Win = [];
activation = {};
Wout = [];
functional = 0;
end
methods
function obj = extreme_learning_machine_classifier(X,y,varargin)
%EXTREME_LEARNING_MACHINE_CLASSIFIER Trains an ELM for
%classification.
% Inputs:
% X - Input data
% y - output data
% hidden - the number of hidden units (default: 2*size(X,2))
% activation - activation function (default: 'sigmoid')
% other values: 'tanh', 'relu', 'rbf', 'linear'
% functional - functional link between input and output
% (default: 0 [false]) other values: 1 [true]
% Output:
% obj - Trained ELM object
p = inputParser;
addRequired(p, 'X', @ismatrix);
addRequired(p, 'y', @ismatrix);
addParameter(p, 'hidden', 2*size(X,2), @isnumeric);
addParameter(p, 'activation', 'sigmoid', @(x)any(validatestring(x,{'sigmoid','tanh','rbf','relu'})));
addParameter(p, 'functional', 0, @isnumeric);
parse(p,1,1,varargin{:});
obj.functional = p.Results.functional;
switch p.Results.activation
case 'sigmoid'
obj.activation = @(x)(1 ./ (1 + exp(-x)));
case 'tanh'
obj.activation = @(x)(tanh(x));
case 'relu'
obj.activation = @(x)(max(0,x));
case 'rbf'
obj.activation = @(x)(radbas(x));
case 'linear'
obj.activation = @(x)(x);
end
hidden = p.Results.hidden;
obj.Win = rand(size(X,2)+1, hidden) * 2 - 1;
H = obj.activation([X, ones(size(X,1),1)] * obj.Win);
if obj.functional; H = [H,X]; end
obj.Wout = pinv([H, ones(size(H,1),1)]) * y;
end
function [y, p] = predict(obj, X)
%PREDICT Predicts the output of the trained model for new input
%data
% Inputs:
% obj - trained ELM
% X - Input data
% Output:
% y - Binary output
% p - Numeric output (~probability)
H = obj.activation([X, ones(size(X,1),1)] * obj.Win);
if obj.functional; H = [H, X]; end
p = [H, ones(size(H,1),1)] * obj.Wout;
y = p >= 0.5;
end
end
end