forked from yusugomori/DeepLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdA.java
More file actions
230 lines (185 loc) · 6.69 KB
/
SdA.java
File metadata and controls
230 lines (185 loc) · 6.69 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import java.util.Random;
public class SdA {
public int N;
public int n_ins;
public int[] hidden_layer_sizes;
public int n_outs;
public int n_layers;
public HiddenLayer[] sigmoid_layers;
public dA[] dA_layers;
public LogisticRegression log_layer;
public Random rng;
public static double sigmoid(double x) {
return 1.0 / (1.0 + Math.pow(Math.E, -x));
}
public SdA(int N, int n_ins, int[] hidden_layer_sizes, int n_outs, int n_layers, Random rng) {
int input_size;
this.N = N;
this.n_ins = n_ins;
this.hidden_layer_sizes = hidden_layer_sizes;
this.n_outs = n_outs;
this.n_layers = n_layers;
this.sigmoid_layers = new HiddenLayer[n_layers];
this.dA_layers = new dA[n_layers];
if(rng == null) this.rng = new Random(1234);
else this.rng = rng;
// construct multi-layer
for(int i=0; i<this.n_layers; i++) {
if(i == 0) {
input_size = this.n_ins;
} else {
input_size = this.hidden_layer_sizes[i-1];
}
// construct sigmoid_layer
this.sigmoid_layers[i] = new HiddenLayer(this.N, input_size, this.hidden_layer_sizes[i], null, null, rng);
// construct dA_layer
this.dA_layers[i] = new dA(this.N, input_size, this.hidden_layer_sizes[i], this.sigmoid_layers[i].W, this.sigmoid_layers[i].b, null, rng);
}
// layer for output using LogisticRegression
this.log_layer = new LogisticRegression(this.N, this.hidden_layer_sizes[this.n_layers-1], this.n_outs);
}
public void pretrain(int[][] train_X, double lr, double corruption_level, int epochs) {
int[] layer_input = new int[0];
int prev_layer_input_size;
int[] prev_layer_input;
for(int i=0; i<n_layers; i++) { // layer-wise
for(int epoch=0; epoch<epochs; epoch++) { // training epochs
for(int n=0; n<N; n++) { // input x1...xN
// layer input
for(int l=0; l<=i; l++) {
if(l == 0) {
layer_input = new int[n_ins];
for(int j=0; j<n_ins; j++) layer_input[j] = train_X[n][j];
} else {
if(l == 1) prev_layer_input_size = n_ins;
else prev_layer_input_size = hidden_layer_sizes[l-2];
prev_layer_input = new int[prev_layer_input_size];
for(int j=0; j<prev_layer_input_size; j++) prev_layer_input[j] = layer_input[j];
layer_input = new int[hidden_layer_sizes[l-1]];
sigmoid_layers[l-1].sample_h_given_v(prev_layer_input, layer_input);
}
}
dA_layers[i].train(layer_input, lr, corruption_level);
}
}
}
}
public void finetune(int[][] train_X, int[][] train_Y, double lr, int epochs) {
int[] layer_input = new int[0];
// int prev_layer_input_size;
int[] prev_layer_input = new int[0];
for(int epoch=0; epoch<epochs; epoch++) {
for(int n=0; n<N; n++) {
// layer input
for(int i=0; i<n_layers; i++) {
if(i == 0) {
prev_layer_input = new int[n_ins];
for(int j=0; j<n_ins; j++) prev_layer_input[j] = train_X[n][j];
} else {
prev_layer_input = new int[hidden_layer_sizes[i-1]];
for(int j=0; j<hidden_layer_sizes[i-1]; j++) prev_layer_input[j] = layer_input[j];
}
layer_input = new int[hidden_layer_sizes[i]];
sigmoid_layers[i].sample_h_given_v(prev_layer_input, layer_input);
}
log_layer.train(layer_input, train_Y[n], lr);
}
// lr *= 0.95;
}
}
public void predict(int[] x, double[] y) {
double[] layer_input = new double[0];
// int prev_layer_input_size;
double[] prev_layer_input = new double[n_ins];
for(int j=0; j<n_ins; j++) prev_layer_input[j] = x[j];
double linear_output;
// layer activation
for(int i=0; i<n_layers; i++) {
layer_input = new double[sigmoid_layers[i].n_out];
for(int k=0; k<sigmoid_layers[i].n_out; k++) {
linear_output = 0.0;
for(int j=0; j<sigmoid_layers[i].n_in; j++) {
linear_output += sigmoid_layers[i].W[k][j] * prev_layer_input[j];
}
linear_output += sigmoid_layers[i].b[k];
layer_input[k] = sigmoid(linear_output);
}
if(i < n_layers-1) {
prev_layer_input = new double[sigmoid_layers[i].n_out];
for(int j=0; j<sigmoid_layers[i].n_out; j++) prev_layer_input[j] = layer_input[j];
}
}
for(int i=0; i<log_layer.n_out; i++) {
y[i] = 0;
for(int j=0; j<log_layer.n_in; j++) {
y[i] += log_layer.W[i][j] * layer_input[j];
}
y[i] += log_layer.b[i];
}
log_layer.softmax(y);
}
private static void test_sda() {
Random rng = new Random(123);
double pretrain_lr = 0.1;
double corruption_level = 0.3;
int pretraining_epochs = 1000;
double finetune_lr = 0.1;
int finetune_epochs = 500;
int train_N = 10;
int test_N = 4;
int n_ins = 28;
int n_outs = 2;
int[] hidden_layer_sizes = {15, 15};
int n_layers = hidden_layer_sizes.length;
// training data
int[][] train_X = {
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}
};
int[][] train_Y = {
{1, 0},
{1, 0},
{1, 0},
{1, 0},
{1, 0},
{0, 1},
{0, 1},
{0, 1},
{0, 1},
{0, 1}
};
// construct SdA
SdA sda = new SdA(train_N, n_ins, hidden_layer_sizes, n_outs, n_layers, rng);
// pretrain
sda.pretrain(train_X, pretrain_lr, corruption_level, pretraining_epochs);
// finetune
sda.finetune(train_X, train_Y, finetune_lr, finetune_epochs);
// test data
int[][] test_X = {
{1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}
};
double[][] test_Y = new double[test_N][n_outs];
// test
for(int i=0; i<test_N; i++) {
sda.predict(test_X[i], test_Y[i]);
for(int j=0; j<n_outs; j++) {
System.out.print(test_Y[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
test_sda();
}
}