-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivationFunction.java
More file actions
64 lines (56 loc) · 2.2 KB
/
Copy pathActivationFunction.java
File metadata and controls
64 lines (56 loc) · 2.2 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
public class ActivationFunction {
// Returns the Heaviside function of x.
public static double heaviside(double x) {
if (Double.isNaN(x)) x = Double.NaN;
else if (x < 0) x = 0;
else if (x > 0) x = 1;
else x = 0.5;
return x;
}
// Returns the sigmoid function of x.
public static double sigmoid(double x) {
if (Double.isNaN(x)) return Double.NaN;
else return 1 / (1 + (Math.exp(-x)));
}
// Returns the hyperbolic tangent of x.
public static double tanh(double x) {
if (Double.isNaN(x)) return Double.NaN;
else if (x >= 20.0) return 1.0;
else if (x <= -20.0) return -1.0;
else return (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
}
// Returns the softsign function of x.
public static double softsign(double x) {
if (Double.isNaN(x))
return Double.NaN;
else if (x == Double.POSITIVE_INFINITY)
return 1;
else if (x == Double.NEGATIVE_INFINITY)
return -1;
else
return x / (1 + Math.abs(x));
}
// Returns the square nonlinearity function of x.
public static double sqnl(double x) {
if (Double.isNaN(x)) x = Double.NaN;
else if (x <= -2) x = -1;
else if (x > -2 && x < 0) x = x + ((x * x) / 4);
else if (x >= 0 && x < 2) x = x - ((x * x) / 4);
else if (x >= 2) x = 1;
return x;
}
// Takes a double command-line argument x and prints each activation
public static void main(String[] args) {
double x = Double.parseDouble(args[0]);
System.out.println("heaviside(" + x + ") = " + heaviside(x));
System.out.println(" sigmoid(" + x + ") = " + sigmoid(x));
System.out.println(" tanh(" + x + ") = " + tanh(x));
System.out.println(" softsign(" + x + ") = " + softsign(x));
System.out.println(" sqnl(" + x + ") = " + sqnl(x));
}
}