-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmy_activate.m
More file actions
120 lines (85 loc) · 3.06 KB
/
my_activate.m
File metadata and controls
120 lines (85 loc) · 3.06 KB
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
function [y, dy] = my_activate( x, W, b, act_fn )
%{
FUNCTION DESCRIPTION:
---------------------------------------------------------------------------
Activation function.
Choose between: linear, sigmoid, Tanh, ReLU, leaky ReLU and ELU.
Returns:
- y = activate(z) = activate(Wx+b)
- dy = activate'(z) = activate'(Wx+b)
The ith component of y is activate((Wx+b)_i) and similarly for dy.
---------------------------------------------------------------------------
INPUTS:
---------------------------------------------------------------------------
x -- input vector {vector in R^{s}},
W -- weights {matrix in R^{r x s}},
b -- bias values {vector in R^{s}},
act_fn -- choice of activation functon {string}.
---------------------------------------------------------------------------
OUTPUTS:
---------------------------------------------------------------------------
y -- output vector {vector in R^{r}},
dy -- derivative of activation function at these points {vector in R^{r}}.
---------------------------------------------------------------------------
Written by: James Rynn
Last edited: 20/03/2020
%}
%% APPLY WEIGHTING AND BIAS:
% Input after weightings and bias applied.
z = W*x+b;
%% ACTIVATION FUNCTION:
% LINEAR:
% -------------------------------------------------------------------------
if(strcmp(act_fn,'linear'))
y = z; % Activation
dy = ones(size(z)); % Derivative
% -------------------------------------------------------------------------
% SIGMOID:
elseif(strcmp(act_fn,'sigmoid'))
y = 1./(1+exp(-z)); % Activation
dy = y.*(1-y); % Derivative
% -------------------------------------------------------------------------
% HYPERBOLIC TAN (Tanh):
% -------------------------------------------------------------------------
elseif(strcmp(act_fn,'hyp_tan'))
y = tanh(z); % Activation
dy = sech(z).^2; % Derivative
% RECTIFIED LINEAR UNIT (ReLU):
% -------------------------------------------------------------------------
elseif(strcmp(act_fn,'ReLU'))
% Activation.
y = max(0,z);
% Derivative.
dy = zeros(size(z));
dy(z>=0) = 1;
% LEAKY RELU:
% -------------------------------------------------------------------------
elseif(strcmp(act_fn,'leaky_ReLU'))
% Slope parameter.
s = 0.05;
% Activation.
y = zeros(size(z));
y(z>=0) = z(z>=0);
y(z<0) = s*z(z<0);
% Derivative.
dy = zeros(size(z));
dy(z>=0) = 1;
dy(z<0) = s;
% EXPONENTIAL LINEAR UNIT (ELU):
% -------------------------------------------------------------------------
elseif(strcmp(act_fn,'ELU'))
% Slope parameter.
s = 0.05;
% Activation.
y = zeros(size(z));
y(z>=0) = z(z>=0);
y(z<0) = s*(exp(z(z<0))-1);
% Derivative.
dy = zeros(size(z));
dy(z>=0) = 1;
dy(z<0) = s*exp(z(z<0));
% OTHER (NOT SUPPORTED):
% -------------------------------------------------------------------------
else
error(['Activation function type ' act_fn ' is not supported.'])
end