Repository navigation
Expand file tree
/
Copy pathKAN.cpp
More file actions
58 lines (49 loc) · 1.39 KB
/
Copy pathKAN.cpp
File metadata and controls
58 lines (49 loc) · 1.39 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
#include "KAN.h"
KANImpl::KANImpl(
std::vector<int64_t> layers_hidden,
int64_t grid_size,
int64_t spline_order,
double scale_noise,
double scale_base,
double scale_spline,
torch::nn::SiLU base_activation,
double grid_eps,
std::pair<double, double> grid_range)
:
grid_size(grid_size),
spline_order(spline_order) {
for (size_t i = 0; i < layers_hidden.size() - 1; ++i) {
layers.push_back(
register_module( "layer" + std::to_string(i),
KANLinear(
layers_hidden[i],
layers_hidden[i + 1],
grid_size,
spline_order,
scale_noise,
scale_base,
scale_spline,
true,
base_activation,
grid_eps,
grid_range)
)
);
}
}
torch::Tensor KANImpl::forward(torch::Tensor x, bool update_grid) {
for (auto& layer : layers) {
if (update_grid) {
layer->update_grid(x);
}
x = layer->forward(x);
}
return x;
}
torch::Tensor KANImpl::regularization_loss(double regularize_activation, double regularize_entropy) {
torch::Tensor loss = torch::zeros({1});
for (auto& layer : layers) {
loss += layer->regularization_loss(regularize_activation, regularize_entropy);
}
return loss;
}