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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// 持续学习与增量学习(v44.0.0)
// 持续学习、增量学习、终身学习、灾难性遗忘、知识蒸馏、弹性权重巩固
///|
/// 持续学习策略类型
pub(all) enum ContinualLearningStrategy {
RegularizationBased
ReplayBased
ArchitectureBased
MetaLearningBased
Hybrid
Unknown
} derive(Eq)
///|
/// 正则化方法类型
pub(all) enum RegularizationMethod {
EWC
SI
MAS
RWalk
LwF
LFL
PackNet
HAT
Unknown
} derive(Eq)
///|
/// 回放方法类型
pub(all) enum ReplayMethod {
ExperienceReplay
GenerativeReplay
DarkExperienceReplay
GradientEpisodeMemory
MaximalInterferedRetrieval
Unknown
} derive(Eq)
///|
/// 增量学习场景类型
pub(all) enum IncrementalLearningScenario {
TaskIncremental
DomainIncremental
ClassIncremental
TimeIncremental
DataIncremental
Unknown
} derive(Eq)
///|
/// 学习任务
pub(all) struct LearningTask {
task_id : Int
task_name : String
num_classes : Int
num_samples : Int
data_distribution : Array[Double]
difficulty : Double
is_completed : Bool
}
///|
/// 持续学习配置
pub(all) struct ContinualLearningConfig {
strategy : ContinualLearningStrategy
regularization_method : RegularizationMethod
replay_method : ReplayMethod
scenario : IncrementalLearningScenario
num_tasks : Int
ewc_lambda : Double
memory_size : Int
replay_ratio : Double
learning_rate : Double
batch_size : Int
num_epochs : Int
use_knowledge_distillation : Bool
distillation_temperature : Double
distillation_alpha : Double
use_dynamic_architecture : Bool
architecture_growth_rate : Double
}
///|
/// 弹性权重巩固配置
pub(all) struct EWCConfig {
lambda : Double
fisher_information : Array[Double]
optimal_params : Array[Double]
num_params : Int
online : Bool
gamma : Double
}
///|
/// 记忆缓冲区
pub(all) struct MemoryBuffer {
buffer_size : Int
current_size : Int
samples : Array[Array[Double]]
labels : Array[Int]
task_ids : Array[Int]
sampling_strategy : String
}
///|
/// 持续学习性能指标
pub(all) struct ContinualLearningMetrics {
average_accuracy : Double
final_accuracy : Double
backward_transfer : Double
forward_transfer : Double
forgetting_measure : Double
learning_accuracy : Array[Double]
task_accuracies : Array[Double]
num_tasks_completed : Int
memory_usage_mb : Double
training_time_s : Double
}
///|
/// 知识蒸馏配置
pub(all) struct KnowledgeDistillationConfigCL {
temperature : Double
alpha : Double
beta : Double
use_hint_loss : Bool
use_attention_transfer : Bool
teacher_task_ids : Array[Int]
}
///|
/// 创建默认持续学习配置
pub fn create_default_continual_config() -> ContinualLearningConfig {
ContinualLearningConfig::{
strategy: ContinualLearningStrategy::RegularizationBased,
regularization_method: RegularizationMethod::EWC,
replay_method: ReplayMethod::ExperienceReplay,
scenario: IncrementalLearningScenario::ClassIncremental,
num_tasks: 10,
ewc_lambda: 100.0,
memory_size: 1000,
replay_ratio: 0.1,
learning_rate: 0.01,
batch_size: 32,
num_epochs: 10,
use_knowledge_distillation: true,
distillation_temperature: 2.0,
distillation_alpha: 0.5,
use_dynamic_architecture: false,
architecture_growth_rate: 0.1,
}
}
///|
/// 创建默认EWC配置
pub fn create_default_ewc_config(num_params : Int) -> EWCConfig {
let fisher : Array[Double] = []
let optimal : Array[Double] = []
for _ in 0..<num_params {
fisher.push(0.0)
optimal.push(0.0)
}
EWCConfig::{
lambda: 100.0,
fisher_information: fisher,
optimal_params: optimal,
num_params,
online: false,
gamma: 0.9,
}
}
///|
/// 创建记忆缓冲区
pub fn create_memory_buffer(
buffer_size : Int,
sampling_strategy : String,
) -> MemoryBuffer {
let samples : Array[Array[Double]] = []
let labels : Array[Int] = []
let task_ids : Array[Int] = []
MemoryBuffer::{
buffer_size,
current_size: 0,
samples,
labels,
task_ids,
sampling_strategy,
}
}
///|
/// 创建学习任务序列
pub fn create_task_sequence(
num_tasks : Int,
classes_per_task : Int,
samples_per_task : Int,
) -> Array[LearningTask] {
let tasks : Array[LearningTask] = []
for i in 0..<num_tasks {
let distribution : Array[Double] = []
for _ in 0..<classes_per_task {
distribution.push(1.0 / classes_per_task.to_double())
}
let difficulty = 0.3 + (i % 5).to_double() * 0.1
tasks.push(LearningTask::{
task_id: i,
task_name: "task_" + i.to_string(),
num_classes: classes_per_task,
num_samples: samples_per_task,
data_distribution: distribution,
difficulty,
is_completed: false,
})
}
tasks
}
///|
/// 计算EWC正则化损失(简化版)
pub fn compute_ewc_loss(
current_params : Array[Double],
ewc_config : EWCConfig,
) -> Double {
let mut loss = 0.0
for i in 0..<current_params.length() {
if i < ewc_config.num_params {
let diff = current_params[i] - ewc_config.optimal_params[i]
loss += ewc_config.fisher_information[i] * diff * diff
}
}
0.5 * ewc_config.lambda * loss
}
///|
/// 更新Fisher信息矩阵(简化版)
pub fn update_fisher_information(
ewc_config : EWCConfig,
gradients : Array[Double],
) -> EWCConfig {
let new_fisher : Array[Double] = []
for i in 0..<ewc_config.num_params {
let grad_sq = if i < gradients.length() {
gradients[i] * gradients[i]
} else {
0.0
}
let updated = if ewc_config.online {
ewc_config.gamma * ewc_config.fisher_information[i] +
(1.0 - ewc_config.gamma) * grad_sq
} else {
ewc_config.fisher_information[i] + grad_sq
}
new_fisher.push(updated)
}
EWCConfig::{
lambda: ewc_config.lambda,
fisher_information: new_fisher,
optimal_params: ewc_config.optimal_params,
num_params: ewc_config.num_params,
online: ewc_config.online,
gamma: ewc_config.gamma,
}
}
///|
/// 从记忆缓冲区采样(简化版)
pub fn sample_from_memory(
buffer : MemoryBuffer,
num_samples : Int,
) -> (Array[Array[Double]], Array[Int], Array[Int]) {
let sampled_samples : Array[Array[Double]] = []
let sampled_labels : Array[Int] = []
let sampled_task_ids : Array[Int] = []
let actual_samples = if num_samples < buffer.current_size {
num_samples
} else {
buffer.current_size
}
for i in 0..<actual_samples {
// 简化的随机采样
let idx = (i * 7 + 13) % buffer.current_size
sampled_samples.push(buffer.samples[idx])
sampled_labels.push(buffer.labels[idx])
sampled_task_ids.push(buffer.task_ids[idx])
}
(sampled_samples, sampled_labels, sampled_task_ids)
}
///|
/// 添加样本到记忆缓冲区(简化版)
pub fn add_to_memory(
buffer : MemoryBuffer,
sample : Array[Double],
label : Int,
task_id : Int,
) -> MemoryBuffer {
if buffer.current_size < buffer.buffer_size {
let new_samples = buffer.samples
let new_labels = buffer.labels
let new_task_ids = buffer.task_ids
new_samples.push(sample)
new_labels.push(label)
new_task_ids.push(task_id)
MemoryBuffer::{
buffer_size: buffer.buffer_size,
current_size: buffer.current_size + 1,
samples: new_samples,
labels: new_labels,
task_ids: new_task_ids,
sampling_strategy: buffer.sampling_strategy,
}
} else {
// 缓冲区满,替换最旧的样本
let idx = 0
buffer.samples[idx] = sample
buffer.labels[idx] = label
buffer.task_ids[idx] = task_id
buffer
}
}
///|
/// 模拟持续学习训练(简化版)
pub fn simulate_continual_learning(
tasks : Array[LearningTask],
config : ContinualLearningConfig,
initial_accuracy : Double,
) -> ContinualLearningMetrics {
let num_tasks = tasks.length()
let task_accuracies : Array[Double] = []
let learning_accuracy : Array[Double] = []
let mut final_accuracy = initial_accuracy
let mut total_accuracy = 0.0
let mut forgetting = 0.0
for task_idx in 0..<num_tasks {
let task = tasks[task_idx]
// 模拟任务学习
let task_accuracy = initial_accuracy +
(1.0 - initial_accuracy) * (1.0 - task.difficulty * 0.5)
task_accuracies.push(task_accuracy)
learning_accuracy.push(task_accuracy)
// 模拟对之前任务的遗忘
if task_idx > 0 {
let forgetting_rate = match config.regularization_method {
RegularizationMethod::EWC => 0.02
RegularizationMethod::SI => 0.03
RegularizationMethod::MAS => 0.025
RegularizationMethod::LwF => 0.04
_ => 0.05
}
for prev_idx in 0..<task_idx {
let prev_accuracy = task_accuracies[prev_idx]
let new_accuracy = prev_accuracy * (1.0 - forgetting_rate)
task_accuracies[prev_idx] = new_accuracy
forgetting += prev_accuracy - new_accuracy
}
}
// 计算平均精度
let mut sum = 0.0
for i in 0..<(task_idx + 1) {
sum += task_accuracies[i]
}
final_accuracy = sum / (task_idx + 1).to_double()
total_accuracy += final_accuracy
}
let average_accuracy = total_accuracy / num_tasks.to_double()
let backward_transfer = -forgetting / (num_tasks - 1).to_double()
let forward_transfer = 0.05
let forgetting_measure = forgetting / (num_tasks - 1).to_double()
ContinualLearningMetrics::{
average_accuracy,
final_accuracy,
backward_transfer,
forward_transfer,
forgetting_measure,
learning_accuracy,
task_accuracies,
num_tasks_completed: num_tasks,
memory_usage_mb: config.memory_size.to_double() * 0.001,
training_time_s: num_tasks.to_double() *
config.num_epochs.to_double() *
10.0,
}
}
///|
/// 生成持续学习报告
pub fn continual_learning_report_to_string(
metrics : ContinualLearningMetrics,
config : ContinualLearningConfig,
) -> String {
let mut report = "## 持续学习与增量学习报告\n\n"
report = report + "### 配置信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 策略 | " +
(match config.strategy {
ContinualLearningStrategy::RegularizationBased => "基于正则化"
ContinualLearningStrategy::ReplayBased => "基于回放"
ContinualLearningStrategy::ArchitectureBased => "基于架构"
ContinualLearningStrategy::MetaLearningBased => "基于元学习"
_ => "混合"
}) +
" |\n"
report = report +
"| 正则化方法 | " +
(match config.regularization_method {
RegularizationMethod::EWC => "EWC"
RegularizationMethod::SI => "SI"
RegularizationMethod::MAS => "MAS"
RegularizationMethod::LwF => "LwF"
_ => "未知"
}) +
" |\n"
report = report +
"| 场景 | " +
(match config.scenario {
IncrementalLearningScenario::TaskIncremental => "任务增量"
IncrementalLearningScenario::DomainIncremental => "域增量"
IncrementalLearningScenario::ClassIncremental => "类增量"
_ => "未知"
}) +
" |\n"
report = report + "| 任务数 | " + config.num_tasks.to_string() + " |\n"
report = report + "| EWC Lambda | " + config.ewc_lambda.to_string() + " |\n"
report = report +
"| 记忆大小 | " +
config.memory_size.to_string() +
" |\n"
report = report + "\n### 性能指标\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 平均精度 | " +
(metrics.average_accuracy * 100.0).to_string() +
"% |\n"
report = report +
"| 最终精度 | " +
(metrics.final_accuracy * 100.0).to_string() +
"% |\n"
report = report +
"| 反向迁移 | " +
(metrics.backward_transfer * 100.0).to_string() +
"% |\n"
report = report +
"| 正向迁移 | " +
(metrics.forward_transfer * 100.0).to_string() +
"% |\n"
report = report +
"| 遗忘度量 | " +
(metrics.forgetting_measure * 100.0).to_string() +
"% |\n"
report = report +
"| 完成任务数 | " +
metrics.num_tasks_completed.to_string() +
" |\n"
report = report +
"| 内存使用 | " +
metrics.memory_usage_mb.to_string() +
" MB |\n"
report = report +
"| 训练时间 | " +
metrics.training_time_s.to_string() +
" s |\n"
report = report + "\n### 各任务精度\n\n"
report = report + "| 任务 | 精度 |\n|------|------|\n"
for i in 0..<metrics.task_accuracies.length() {
report = report +
"| 任务 " +
(i + 1).to_string() +
" | " +
(metrics.task_accuracies[i] * 100.0).to_string() +
"% |\n"
}
report
}