42 for (
double weight : weights) {
43 penalty += std::abs(weight);
45 return lambda * penalty;
52 for (
double weight : weights) {
53 penalty += weight * weight;
55 return 0.5 * lambda * penalty;
59 const std::vector<double> &weights,
62 double l1_penalty = l1_regularization(weights, lambda1);
63 double l2_penalty = l2_regularization(weights, lambda2);
64 return l1_penalty + l2_penalty;
69 return 0.5 * dropout_rate * num_neurons;
73 double &best_val_loss,
76 if (current_val_loss < best_val_loss) {
77 best_val_loss = current_val_loss;
78 patience = epoch + patience;
80 if (epoch >= patience) {
88 const std::vector<std::vector<double>> &predictions) {
89 std::vector<double> ensemble;
90 if (!predictions.empty()) {
91 ensemble.resize(predictions.front().size(), 0.0);
92 for (
const auto &prediction : predictions) {
93 for (
size_t i = 0; i < prediction.size(); ++i) {
94 ensemble[i] += prediction[i];
97 for (
auto &val : ensemble) {
98 val /= predictions.size();
107 for (
double &weight : weights) {
108 norm += weight * weight;
111 if (norm > max_norm) {
112 double factor = max_norm / norm;
113 for (
double &weight : weights) {
120 std::vector<double> &weights,
122 for (
double &weight : weights) {
123 weight *= (1.0 - lambda);
128 const std::vector<std::vector<double>> &input_data,
132 std::vector<std::vector<double>> normalized_data;
133 for (
const auto &instance : input_data) {
135 for (
double val : instance) {
138 mean /= instance.size();
140 double variance = 0.0;
141 for (
double val : instance) {
142 variance += (val - mean) * (val - mean);
144 variance /= instance.size();
146 double std_dev = sqrt(variance + epsilon);
148 std::vector<double> normalized_instance;
149 for (
double val : instance) {
150 double normalized_val = scale * ((val - mean) / std_dev) + shift;
151 normalized_instance.push_back(normalized_val);
153 normalized_data.push_back(normalized_instance);
155 return normalized_data;
159 const std::vector<std::vector<double>> &input_data,
160 int augmentation_factor) {
161 std::vector<std::vector<double>> augmented_data;
162 std::random_device rd;
163 std::mt19937 gen(rd());
165 for (
const auto &instance : input_data) {
166 augmented_data.push_back(instance);
167 for (
int i = 1; i < augmentation_factor; ++i) {
168 std::vector<double> augmented_instance = instance;
169 std::shuffle(augmented_instance.begin(),
170 augmented_instance.end(),
172 augmented_data.push_back(augmented_instance);
175 return augmented_data;
static double l2_regularization(const std::vector< double > &weights, double lambda)
Computes L2 regularization penalty (Ridge regression)
static std::vector< double > ensemble_predictions(const std::vector< std::vector< double >> &predictions)
Combines predictions from multiple models using ensembling.
static void max_norm_regularization(std::vector< double > &weights, double max_norm)
Applies max norm regularization to the weights.
static std::vector< std::vector< double > > data_augmentation(const std::vector< std::vector< double >> &input_data, int augmentation_factor)
Applies data augmentation to the input data.
static std::vector< std::vector< double > > batch_normalization(const std::vector< std::vector< double >> &input_data, double epsilon=1e-5, double scale=1.0, double shift=0.0)
Applies batch normalization to the input data.
static double elastic_net_regularization(const std::vector< double > &weights, double lambda1, double lambda2)
Computes Elastic Net regularization penalty.
static double l1_regularization(const std::vector< double > &weights, double lambda)
Computes L1 regularization penalty (Lasso regression)
static double dropout_regularization(double dropout_rate, int num_neurons)
Computes Dropout regularization penalty.
static void weight_decay_regularization(std::vector< double > &weights, double lambda)
Applies weight decay regularization to the weights.
static bool early_stopping(double current_val_loss, double &best_val_loss, int patience, int epoch)
Performs early stopping based on validation loss.