-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKitNET.cpp
More file actions
131 lines (115 loc) · 4.05 KB
/
Copy pathKitNET.cpp
File metadata and controls
131 lines (115 loc) · 4.05 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
#include "KitNET.h"
#include <iostream>
#include <numeric>
KitNET::KitNET(size_t n, size_t max_autoencoder_size, size_t FM_grace_period,
size_t AD_grace_period, double learning_rate,
double hidden_ratio,
const std::vector<std::vector<size_t>> *feature_map)
: AD_grace_period(AD_grace_period), n(n), lr(learning_rate),
hr(hidden_ratio), n_trained(0), n_executed(0) {
// 设置FM宽限期
if (FM_grace_period == 0) {
this->FM_grace_period = AD_grace_period;
} else {
this->FM_grace_period = FM_grace_period;
}
// 设置最大自动编码器大小
if (max_autoencoder_size <= 0) {
this->m = 1;
} else {
this->m = max_autoencoder_size;
}
// 设置特征映射
if (feature_map != nullptr) {
this->v = *feature_map;
this->__createAD__();
std::cout
<< "Feature-Mapper: execute-mode, Anomaly-Detector: train-mode"
<< std::endl;
} else {
std::cout << "Feature-Mapper: train-mode, Anomaly-Detector: off-mode"
<< std::endl;
}
// 初始化特征映射聚类器
this->FM = std::make_unique<corClust>(this->n);
}
double KitNET::process(const std::vector<double> &x) {
if (this->n_trained > this->FM_grace_period + this->AD_grace_period) {
// 如果FM和AD都处于执行模式
return this->execute(x);
} else {
this->train(x);
return 0.0;
}
}
double KitNET::train(const std::vector<double> &x) {
if (this->n_trained <= this->FM_grace_period && this->v.empty()) {
// 如果FM处于训练模式,且用户未提供特征映射
// 更新增量相关矩阵
this->FM->update(x);
if (this->n_trained == this->FM_grace_period) {
// 如果应该实例化特征映射
this->v = this->FM->cluster(this->m);
this->__createAD__();
std::cout << "The Feature-Mapper found a mapping: " << this->n
<< " features to " << this->v.size() << " autoencoders."
<< std::endl;
std::cout
<< "Feature-Mapper: execute-mode, Anomaly-Detector: train-mode"
<< std::endl;
}
} else {
// 训练
// 集成层
std::vector<double> S_l1(this->ensembleLayer.size(), 0.0);
for (size_t a = 0; a < this->ensembleLayer.size(); a++) {
// 为自动编码器'a'创建子实例
std::vector<double> xi;
for (const auto &idx : this->v[a]) {
xi.push_back(x[idx]);
}
S_l1[a] = this->ensembleLayer[a]->train(xi);
}
// 输出层
this->outputLayer->train(S_l1);
if (this->n_trained == this->AD_grace_period + this->FM_grace_period) {
std::cout << "Feature-Mapper: execute-mode, Anomaly-Detector: "
"execute-mode"
<< std::endl;
}
}
this->n_trained++;
return 0.0;
}
double KitNET::execute(const std::vector<double> &x) {
if (this->v.empty()) {
throw std::runtime_error(
"KitNET Cannot execute x, because a feature mapping has not yet "
"been learned or provided. Try running process(x) instead.");
} else {
this->n_executed++;
// 集成层
std::vector<double> S_l1(this->ensembleLayer.size(), 0.0);
for (size_t a = 0; a < this->ensembleLayer.size(); a++) {
// 创建子实例
std::vector<double> xi;
for (const auto &idx : this->v[a]) {
xi.push_back(x[idx]);
}
S_l1[a] = this->ensembleLayer[a]->execute(xi);
}
// 输出层
return this->outputLayer->execute(S_l1);
}
}
void KitNET::__createAD__() {
// 构建集成层
this->ensembleLayer.clear();
for (const auto &map : this->v) {
dA_params params(map.size(), 0, this->lr, 0.0, 0, this->hr);
this->ensembleLayer.push_back(std::make_unique<dA>(params));
}
// 构建输出层
dA_params params(this->v.size(), 0, this->lr, 0.0, 0, this->hr);
this->outputLayer = std::make_unique<dA>(params);
}