61 const std::vector<int64_t> &layers,
62 const std::vector<std::vector<std::any>> &activations,
64 assert(layers.size() == activations.size() + 1);
67 for (
auto i = 0; i < layers.size() - 1; ++i) {
69 register_module(
"layer[" + std::to_string(i) +
"]",
70 torch::nn::Linear(layers[i], layers[i + 1])));
71 layers_.back()->to(options.device(), options.dtype(),
true);
73 torch::nn::init::xavier_uniform_(
layers_.back()->weight);
74 torch::nn::init::constant_(
layers_.back()->bias, 0.0);
78 for (
const auto &a : activations)
87 throw std::runtime_error(
"Invalid number of parameters");
96 std::any_cast<torch::Tensor>(a[1]),
97 std::any_cast<torch::Tensor>(a[2]),
98 std::any_cast<torch::Tensor>(a[3]),
99 std::any_cast<torch::Tensor>(a[4]), std::any_cast<double>(a[5]),
100 std::any_cast<double>(a[6]), std::any_cast<bool>(a[7])});
104 std::any_cast<torch::Tensor>(a[1]),
105 std::any_cast<torch::Tensor>(a[2]),
106 std::any_cast<torch::Tensor>(a[3]),
107 std::any_cast<torch::Tensor>(a[4]), std::any_cast<double>(a[5]),
108 std::any_cast<double>(a[6])});
112 std::any_cast<torch::Tensor>(a[1]),
113 std::any_cast<torch::Tensor>(a[2]),
114 std::any_cast<torch::nn::functional::BatchNormFuncOptions>(
119 new BatchNorm{std::any_cast<torch::Tensor>(a[1]),
120 std::any_cast<torch::Tensor>(a[2])});
123 throw std::runtime_error(
"Invalid number of parameters");
132 new CELU{std::any_cast<double>(a[1]), std::any_cast<bool>(a[2])});
137 std::any_cast<torch::nn::functional::CELUFuncOptions>(a[1])});
139 activations_.emplace_back(
new CELU{std::any_cast<double>(a[1])});
146 throw std::runtime_error(
"Invalid number of parameters");
155 new ELU{std::any_cast<double>(a[1]), std::any_cast<bool>(a[2])});
160 std::any_cast<torch::nn::functional::ELUFuncOptions>(a[1])});
162 activations_.emplace_back(
new ELU{std::any_cast<double>(a[1])});
169 throw std::runtime_error(
"Invalid number of parameters");
180 throw std::runtime_error(
"Invalid number of parameters");
190 std::any_cast<torch::nn::functional::GLUFuncOptions>(a[1])});
192 activations_.emplace_back(
new GLU{std::any_cast<int64_t>(a[1])});
199 throw std::runtime_error(
"Invalid number of parameters");
208 std::any_cast<int64_t>(a[1]), std::any_cast<torch::Tensor>(a[2]),
209 std::any_cast<torch::Tensor>(a[3]), std::any_cast<double>(a[4])});
214 std::any_cast<torch::nn::functional::GroupNormFuncOptions>(
218 new GroupNorm{std::any_cast<int64_t>(a[1])});
222 throw std::runtime_error(
"Invalid number of parameters");
231 std::any_cast<double>(a[1]), std::any_cast<int>(a[2]),
232 std::any_cast<bool>(a[3])});
236 std::any_cast<torch::nn::functional::GumbelSoftmaxFuncOptions>(
243 throw std::runtime_error(
"Invalid number of parameters");
253 std::any_cast<torch::nn::functional::HardshrinkFuncOptions>(
257 new Hardshrink{std::any_cast<double>(a[1])});
264 throw std::runtime_error(
"Invalid number of parameters");
275 throw std::runtime_error(
"Invalid number of parameters");
286 throw std::runtime_error(
"Invalid number of parameters");
294 activations_.emplace_back(
new Hardtanh{std::any_cast<double>(a[1]),
295 std::any_cast<double>(a[2]),
296 std::any_cast<bool>(a[3])});
299 activations_.emplace_back(
new Hardtanh{std::any_cast<double>(a[1]),
300 std::any_cast<double>(a[2])});
304 std::any_cast<torch::nn::functional::HardtanhFuncOptions>(a[1])});
310 throw std::runtime_error(
"Invalid number of parameters");
319 std::any_cast<torch::Tensor>(a[1]),
320 std::any_cast<torch::Tensor>(a[2]),
321 std::any_cast<torch::Tensor>(a[3]),
322 std::any_cast<torch::Tensor>(a[4]), std::any_cast<double>(a[5]),
323 std::any_cast<double>(a[6]), std::any_cast<bool>(a[7])});
327 std::any_cast<torch::Tensor>(a[1]),
328 std::any_cast<torch::Tensor>(a[2]),
329 std::any_cast<torch::Tensor>(a[3]),
330 std::any_cast<torch::Tensor>(a[4]), std::any_cast<double>(a[5]),
331 std::any_cast<double>(a[6])});
335 std::any_cast<torch::nn::functional::InstanceNormFuncOptions>(
342 throw std::runtime_error(
"Invalid number of parameters");
351 std::any_cast<std::vector<int64_t>>(a[1]),
352 std::any_cast<torch::Tensor>(a[2]),
353 std::any_cast<torch::Tensor>(a[3]), std::any_cast<double>(a[4])});
358 std::any_cast<torch::nn::functional::LayerNormFuncOptions>(
362 new LayerNorm{std::any_cast<std::vector<int64_t>>(a[1])});
366 throw std::runtime_error(
"Invalid number of parameters");
374 activations_.emplace_back(
new LeakyReLU{std::any_cast<double>(a[1]),
375 std::any_cast<bool>(a[2])});
380 std::any_cast<torch::nn::functional::LeakyReLUFuncOptions>(
384 new LeakyReLU{std::any_cast<double>(a[1])});
391 throw std::runtime_error(
"Invalid number of parameters");
400 std::any_cast<int64_t>(a[1]), std::any_cast<double>(a[2]),
401 std::any_cast<double>(a[3]), std::any_cast<double>(a[4])});
405 activations_.emplace_back(
new LocalResponseNorm{std::any_cast<
406 torch::nn::functional::LocalResponseNormFuncOptions>(a[1])});
409 new LocalResponseNorm{std::any_cast<int64_t>(a[1])});
413 throw std::runtime_error(
"Invalid number of parameters");
424 throw std::runtime_error(
"Invalid number of parameters");
434 std::any_cast<torch::nn::functional::LogSoftmaxFuncOptions>(
438 new LogSoftmax{std::any_cast<int64_t>(a[1])});
442 throw std::runtime_error(
"Invalid number of parameters");
453 throw std::runtime_error(
"Invalid number of parameters");
462 std::any_cast<double>(a[1]), std::any_cast<double>(a[2]),
463 std::any_cast<int64_t>(a[3])});
467 std::any_cast<torch::nn::functional::NormalizeFuncOptions>(
474 throw std::runtime_error(
"Invalid number of parameters");
483 new PReLU{std::any_cast<torch::Tensor>(a[1])});
486 throw std::runtime_error(
"Invalid number of parameters");
496 std::any_cast<torch::nn::functional::ReLUFuncOptions>(a[1])});
498 activations_.emplace_back(
new ReLU{std::any_cast<bool>(a[1])});
505 throw std::runtime_error(
"Invalid number of parameters");
515 std::any_cast<torch::nn::functional::ReLU6FuncOptions>(a[1])});
517 activations_.emplace_back(
new ReLU6{std::any_cast<bool>(a[1])});
524 throw std::runtime_error(
"Invalid number of parameters");
532 activations_.emplace_back(
new RReLU{std::any_cast<double>(a[1]),
533 std::any_cast<double>(a[2]),
534 std::any_cast<bool>(a[3])});
537 activations_.emplace_back(
new RReLU{std::any_cast<double>(a[1]),
538 std::any_cast<double>(a[2])});
542 std::any_cast<torch::nn::functional::RReLUFuncOptions>(a[1])});
548 throw std::runtime_error(
"Invalid number of parameters");
558 std::any_cast<torch::nn::functional::SELUFuncOptions>(a[1])});
560 activations_.emplace_back(
new SELU{std::any_cast<bool>(a[1])});
567 throw std::runtime_error(
"Invalid number of parameters");
578 throw std::runtime_error(
"Invalid number of parameters");
589 throw std::runtime_error(
"Invalid number of parameters");
599 std::any_cast<torch::nn::functional::SoftmaxFuncOptions>(
603 new Softmax{std::any_cast<int64_t>(a[1])});
607 throw std::runtime_error(
"Invalid number of parameters");
617 std::any_cast<torch::nn::functional::SoftminFuncOptions>(
621 new Softmin{std::any_cast<int64_t>(a[1])});
625 throw std::runtime_error(
"Invalid number of parameters");
633 activations_.emplace_back(
new Softplus{std::any_cast<double>(a[1]),
634 std::any_cast<double>(a[2])});
638 std::any_cast<torch::nn::functional::SoftplusFuncOptions>(a[1])});
644 throw std::runtime_error(
"Invalid number of parameters");
654 std::any_cast<torch::nn::functional::SoftshrinkFuncOptions>(
658 new Softshrink{std::any_cast<double>(a[1])});
665 throw std::runtime_error(
"Invalid number of parameters");
676 throw std::runtime_error(
"Invalid number of parameters");
687 throw std::runtime_error(
"Invalid number of parameters");
698 throw std::runtime_error(
"Invalid number of parameters");
706 activations_.emplace_back(
new Threshold{std::any_cast<double>(a[1]),
707 std::any_cast<double>(a[2]),
708 std::any_cast<bool>(a[3])});
711 activations_.emplace_back(
new Threshold{std::any_cast<double>(a[1]),
712 std::any_cast<double>(a[2])});
716 std::any_cast<torch::nn::functional::ThresholdFuncOptions>(
720 throw std::runtime_error(
"Invalid number of parameters");
725 throw std::runtime_error(
"Invalid activation function");