-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrain_fast_pcd.jl
More file actions
373 lines (331 loc) · 12.6 KB
/
Copy pathtrain_fast_pcd.jl
File metadata and controls
373 lines (331 loc) · 12.6 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
function fast_persistent_contrastive_divergence!(
rbm::AbstractRBM,
x,
mini_batches::Vector{UnitRange{Int}},
fantasy_data::Vector{FantasyData};
learning_rate::Float64 = 0.1,
fast_learning_rate::Float64 = 0.1,
)
total_t_sample, total_t_gibbs, total_t_update = 0.0, 0.0, 0.0
W_fast = zeros(num_visible_nodes(rbm), num_hidden_nodes(rbm))
a_fast = zeros(num_visible_nodes(rbm))
b_fast = zeros(num_hidden_nodes(rbm))
for mini_batch in mini_batches
batch_index = 1
for sample in x[mini_batch]
t_sample = time()
v_data = sample # training visible
h_data = conditional_prob_h(rbm, v_data) # hidden from training visible
total_t_sample += time() - t_sample
# Update hyperparameter
t_update = time()
update_rbm!(
rbm,
v_data,
h_data,
fantasy_data[batch_index].v,
fantasy_data[batch_index].h,
(learning_rate / length(mini_batch)),
)
W_fast =
W_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .*
(v_data * h_data' .- fantasy_data[batch_index].v * fantasy_data[batch_index].h')
a_fast =
a_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .* (v_data .- fantasy_data[batch_index].v)
b_fast =
b_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .* (h_data .- fantasy_data[batch_index].h)
total_t_update += time() - t_update
batch_index += 1
end
# Update fantasy data
t_gibbs = time()
_update_fantasy_data!(rbm, fantasy_data, W_fast, a_fast, b_fast)
total_t_gibbs += time() - t_gibbs
end
return total_t_sample, total_t_gibbs, total_t_update
end
function fast_persistent_contrastive_divergence!(
rbm::GRBMClassifier,
x,
label,
mini_batches::Vector{UnitRange{Int}},
fantasy_data::Vector{FantasyDataClassifier};
learning_rate::Float64 = 0.1,
label_learning_rate::Float64 = 0.1,
fast_learning_rate::Float64 = 0.1,
fast_label_learning_rate::Float64 = 0.1,
)
total_t_sample, total_t_gibbs, total_t_update = 0.0, 0.0, 0.0
W_fast = zeros(num_visible_nodes(rbm), num_hidden_nodes(rbm))
U_fast = zeros(num_label_nodes(rbm), num_hidden_nodes(rbm))
a_fast = zeros(num_visible_nodes(rbm))
b_fast = zeros(num_hidden_nodes(rbm))
c_fast = zeros(num_label_nodes(rbm))
for mini_batch in mini_batches
batch_index = 1
for sample_i in mini_batch
t_sample = time()
v_data = x[sample_i]
y_data = label[sample_i]
h_data = conditional_prob_h(rbm, v_data, y_data)
total_t_sample += time() - t_sample
# Update hyperparameter
t_update = time()
update_rbm!(
rbm,
v_data,
h_data,
y_data,
fantasy_data[batch_index].v,
fantasy_data[batch_index].h,
fantasy_data[batch_index].y,
(learning_rate / length(mini_batch)),
(label_learning_rate / length(mini_batch)),
)
W_fast =
W_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .*
(v_data * h_data' .- fantasy_data[batch_index].v * fantasy_data[batch_index].h')
U_fast =
U_fast .* (19 / 20) +
(fast_label_learning_rate / length(mini_batch)) .*
(y_data * h_data' .- fantasy_data[batch_index].y * fantasy_data[batch_index].h')
a_fast =
a_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .* (v_data .- fantasy_data[batch_index].v)
b_fast =
b_fast .* (19 / 20) +
(fast_learning_rate / length(mini_batch)) .* (h_data .- fantasy_data[batch_index].h)
c_fast =
c_fast .* (19 / 20) +
(fast_label_learning_rate / length(mini_batch)) .* (y_data .- fantasy_data[batch_index].y)
total_t_update += time() - t_update
end
# Update fantasy data
t_gibbs = time()
_update_fantasy_data!(rbm, fantasy_data, W_fast, U_fast, a_fast, b_fast, c_fast)
total_t_gibbs += time() - t_gibbs
end
return total_t_sample, total_t_gibbs, total_t_update
end
"""
train!(
rbm::AbstractRBM,
x_train,
::Type{FastPCD};
n_epochs::Int,
batch_size::Int,
learning_rate::Vector{Float64},
fast_learning_rate::Float64,
metrics::Vector{<:DataType} = [MeanSquaredError],
early_stopping::Bool = false,
store_best_rbm::Bool = true,
patience::Int = 10,
stopping_metric::Type{<:EvaluationMethod} = MeanSquaredError,
x_test_dataset = nothing,
file_path = "fast_pcd_metrics.csv",
)
Train an RBM using Fast Persistent Contrastive Divergence (FastPCD) algorithm.
Tieleman and Hinton (2009) "Using fast weights to improve persistent contrastive divergence"
### Arguments
- `rbm::AbstractRBM`: The RBM to train.
- `x_train`: The training data.
- `n_epochs::Int`: The number of epochs to train the RBM.
- `batch_size::Int`: The size of the mini-batches.
- `learning_rate::Vector{Float64}`: The learning rate for each epoch.
- `fast_learning_rate::Float64`: The fast learning rate.
- `metrics::Vector{<:EvaluationMethod}`: The evaluation metrics to use.
- `early_stopping::Bool`: Whether to use early stopping.
- `stopping_metric::Type{<:EvaluationMethod}`: The metric to use for early stopping.
- `store_best_rbm::Bool`: Whether to store the rbm with the best `stopping_metric`.
- `patience::Int`: The number of epochs to wait before stopping.
- `x_test_dataset`: The test data.
- `file_path`: The file path to save the metrics.
"""
function train!(
rbm::AbstractRBM,
x_train,
::Type{FastPCD};
n_epochs::Int,
batch_size::Int,
learning_rate::Vector{Float64},
fast_learning_rate::Float64,
metrics::Vector{<:DataType} = [MeanSquaredError],
early_stopping::Bool = false,
store_best_rbm::Bool = true,
patience::Int = 10,
stopping_metric::Type{<:EvaluationMethod} = MeanSquaredError,
x_test_dataset = nothing,
file_path = "fast_pcd_metrics.csv",
)
best_rbm = copy_rbm(rbm)
metrics_dict = _initialize_metrics(metrics)
initial_patience = patience
total_t_sample, total_t_gibbs, total_t_update = 0.0, 0.0, 0.0
println("Setting mini-batches")
mini_batches = _set_mini_batches(length(x_train), batch_size)
fantasy_data = _init_fantasy_data(rbm, batch_size)
println("Starting training")
for epoch in 1:n_epochs
for key in keys(metrics_dict)
push!(metrics_dict[key], 0.0)
end
t_sample, t_gibbs, t_update = fast_persistent_contrastive_divergence!(
rbm,
x_train,
mini_batches,
fantasy_data;
learning_rate = learning_rate[epoch],
fast_learning_rate = fast_learning_rate,
)
total_t_sample += t_sample
total_t_gibbs += t_gibbs
total_t_update += t_update
if !isnothing(x_test_dataset)
evaluate(rbm, metrics, x_test_dataset, metrics_dict, epoch)
else
evaluate(rbm, metrics, x_train, metrics_dict, epoch)
end
if _diverged(metrics_dict, epoch, stopping_metric)
if early_stopping
if patience == 0
println("Early stopping at epoch $epoch")
break
end
patience -= 1
end
else
patience = initial_patience
if store_best_rbm
copy_rbm!(rbm, best_rbm)
end
end
_log_epoch(epoch, t_sample, t_gibbs, t_update, total_t_sample + total_t_gibbs + total_t_update)
_log_metrics(metrics_dict, epoch)
end
if store_best_rbm
copy_rbm!(best_rbm, rbm)
end
CSV.write(file_path, DataFrame(metrics_dict))
_log_finish(n_epochs, total_t_sample, total_t_gibbs, total_t_update)
return
end
"""
train!(
rbm::Union{RBMClassifier, GRBMClassifier},
x_train,
label_train,
::Type{FastPCD};
n_epochs::Int,
batch_size::Int,
learning_rate::Vector{Float64},
fast_learning_rate::Float64,
label_learning_rate::Vector{Float64},
metrics::Vector{<:DataType} = [Accuracy],
early_stopping::Bool = false,
store_best_rbm::Bool = true,
patience::Int = 10,
stopping_metric::Type{<:EvaluationMethod} = Accuracy,
x_test_dataset = nothing,
y_test_dataset = nothing,
file_path = "fast_pcd_classifier_metrics.csv",
)
Train an RBM using Fast Persistent Contrastive Divergence (FastPCD) algorithm.
Tieleman and Hinton (2009) "Using fast weights to improve persistent contrastive divergence"
### Arguments
- `rbm::RBMClassifier`: The RBM to train.
- `x_train`: The training data.
- `label_train`: The training labels.
- `n_epochs::Int`: The number of epochs to train the RBM.
- `batch_size::Int`: The size of the mini-batches.
- `learning_rate::Vector{Float64}`: The learning rate for each epoch.
- `fast_learning_rate::Float64`: The fast learning rate.
- `label_learning_rate::Vector{Float64}`: The label learning rate for each epoch.
- `fast_label_learning_rate::Float64`: The fast label learning rate.
- `metrics::Vector{<:EvaluationMethod}`: The evaluation metrics to use.
- `early_stopping::Bool`: Whether to use early stopping.
- `stopping_metric::Type{<:EvaluationMethod}`: The metric to use for early stopping.
- `store_best_rbm::Bool`: Whether to store the rbm with the best `stopping_metric`.
- `patience::Int`: The number of epochs to wait before stopping.
- `x_test_dataset`: The test data.
- `y_test_dataset`: The test labels.
- `file_path`: The file path to save the metrics.
"""
function train!(
rbm::GRBMClassifier,
x_train,
label_train,
::Type{FastPCD};
n_epochs::Int,
batch_size::Int,
learning_rate::Vector{Float64},
fast_learning_rate::Float64 = 0.1,
label_learning_rate::Vector{Float64},
fast_label_learning_rate::Float64 = 0.1,
metrics::Vector{<:DataType} = [Accuracy],
early_stopping::Bool = false,
store_best_rbm::Bool = true,
patience::Int = 10,
stopping_metric::Type{<:EvaluationMethod} = Accuracy,
x_test_dataset = nothing,
y_test_dataset = nothing,
file_path = "fast_pcd_classifier_metrics.csv",
)
best_rbm = copy_rbm(rbm)
metrics_dict = _initialize_metrics(metrics)
initial_patience = patience
total_t_sample, total_t_gibbs, total_t_update = 0.0, 0.0, 0.0
println("Setting mini-batches")
mini_batches = _set_mini_batches(length(x_train), batch_size)
fantasy_data = _init_fantasy_data(rbm, batch_size)
println("Starting training")
for epoch in 1:n_epochs
for key in keys(metrics_dict)
push!(metrics_dict[key], 0.0)
end
t_sample, t_gibbs, t_update = fast_persistent_contrastive_divergence!(
rbm,
x_train,
label_train,
mini_batches,
fantasy_data;
learning_rate = learning_rate[epoch],
label_learning_rate = label_learning_rate[epoch],
fast_learning_rate = fast_learning_rate,
fast_label_learning_rate = fast_label_learning_rate,
)
total_t_sample += t_sample
total_t_gibbs += t_gibbs
total_t_update += t_update
if !isnothing(x_test_dataset)
evaluate(rbm, metrics, x_test_dataset, y_test_dataset, metrics_dict, epoch)
else
evaluate(rbm, metrics, x_train, label_train, metrics_dict, epoch)
end
if _diverged(metrics_dict, epoch, stopping_metric)
if early_stopping
if patience == 0
println("Early stopping at epoch $epoch")
break
end
patience -= 1
end
else
patience = initial_patience
if store_best_rbm
copy_rbm!(rbm, best_rbm)
end
end
_log_epoch(epoch, t_sample, t_gibbs, t_update, total_t_sample + total_t_gibbs + total_t_update)
_log_metrics(metrics_dict, epoch)
end
if store_best_rbm
copy_rbm!(best_rbm, rbm)
end
CSV.write(file_path, DataFrame(metrics_dict))
_log_finish(n_epochs, total_t_sample, total_t_gibbs, total_t_update)
return
end