forked from yusugomori/DeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdA.java
More file actions
150 lines (124 loc) · 3.14 KB
/
dA.java
File metadata and controls
150 lines (124 loc) · 3.14 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
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
import java.util.Random;
public class dA {
public int N;
public int n_visible;
public int n_hidden;
public double[][] W;
public double[] hbias;
public double[] vbias;
public Random rng;
public double uniform(double min, double max) {
return rng.nextDouble() * (max - min) + min;
}
public int binomial(int n, double p) {
if(p < 0 || p > 1) return 0;
int c = 0;
double r;
for(int i=0; i<n; i++) {
r = rng.nextDouble();
if (r < p) c++;
}
return c;
}
public static double sigmoid(double x) {
return 1.0 / (1.0 + Math.pow(Math.E, -x));
}
public dA(int N, int n_visible, int n_hidden,
double[][] W, double[] hbias, double[] vbias, Random rng) {
this.N = N;
this.n_visible = n_visible;
this.n_hidden = n_hidden;
if(rng == null) this.rng = new Random(1234);
else this.rng = rng;
if(W == null) {
this.W = new double[this.n_hidden][this.n_visible];
double a = 1.0 / this.n_visible;
for(int i=0; i<this.n_hidden; i++) {
for(int j=0; j<this.n_visible; j++) {
this.W[i][j] = uniform(-a, a);
}
}
} else {
this.W = W;
}
if(hbias == null) {
this.hbias = new double[this.n_hidden];
for(int i=0; i<this.n_hidden; i++) this.hbias[i] = 0;
} else {
this.hbias = hbias;
}
if(vbias == null) {
this.vbias = new double[this.n_visible];
for(int i=0; i<this.n_visible; i++) this.vbias[i] = 0;
} else {
this.vbias = vbias;
}
}
public void get_corrupted_input(int[] x, int[] tilde_x, double p) {
for(int i=0; i<n_visible; i++) {
if(x[i] == 0) {
tilde_x[i] = 0;
} else {
tilde_x[i] = binomial(1, p);
}
}
}
// Encode
public void get_hidden_values(int[] x, double[] y) {
for(int i=0; i<n_hidden; i++) {
y[i] = 0;
for(int j=0; j<n_visible; j++) {
y[i] += W[i][j] * x[j];
}
y[i] += hbias[i];
y[i] = sigmoid(y[i]);
}
}
// Decode
public void get_reconstructed_input(double[] y, double[] z) {
for(int i=0; i<n_visible; i++) {
z[i] = 0;
for(int j=0; j<n_hidden; j++) {
z[i] += W[j][i] * y[j];
}
z[i] += vbias[i];
z[i] = sigmoid(z[i]);
}
}
public void train(int[] x, double lr, double corruption_level) {
int[] tilde_x = new int[n_visible];
double[] y = new double[n_hidden];
double[] z = new double[n_visible];
double[] L_vbias = new double[n_visible];
double[] L_hbias = new double[n_hidden];
double p = 1 - corruption_level;
get_corrupted_input(x, tilde_x, p);
get_hidden_values(tilde_x, y);
get_reconstructed_input(y, z);
// vbias
for(int i=0; i<n_visible; i++) {
L_vbias[i] = x[i] - z[i];
vbias[i] += lr * L_vbias[i] / N;
}
// hbias
for(int i=0; i<n_hidden; i++) {
L_hbias[i] = 0;
for(int j=0; j<n_visible; j++) {
L_hbias[i] += W[i][j] * L_vbias[j];
}
L_hbias[i] *= y[i] * (1 - y[i]);
hbias[i] += lr * L_hbias[i] / N;
}
// W
for(int i=0; i<n_hidden; i++) {
for(int j=0; j<n_visible; j++) {
W[i][j] += lr * (L_hbias[i] * tilde_x[j] + L_vbias[j] * y[i]) / N;
}
}
}
public void reconstruct(int[] x, double[] z) {
double[] y = new double[n_hidden];
get_hidden_values(x, y);
get_reconstructed_input(y, z);
}
}