Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
neural_chunking::NeuralChunking< T > Class Template Reference

Class implementing neural network-based chunking. More...

#include <neural_chunking.hpp>

+ Collaboration diagram for neural_chunking::NeuralChunking< T >:

Public Member Functions

 NeuralChunking (size_t window_size=8, double threshold=0.5)
 
std::vector< std::vector< T > > chunk (const std::vector< T > &data) const
 
std::string get_activation () const
 Get the current activation function type.
 
size_t get_batch_size () const
 Get the current batch size.
 
size_t get_epochs () const
 Get the current number of training epochs.
 
double get_learning_rate () const
 Get the current learning rate.
 
double get_threshold () const
 
size_t get_window_size () const
 
void set_activation (const std::string &activation)
 Set the activation function type.
 
void set_batch_size (size_t size)
 Set the batch size for training.
 
void set_epochs (size_t num_epochs)
 Set the number of training epochs.
 
void set_learning_rate (double rate)
 Set the learning rate for neural network training.
 
void set_threshold (double threshold)
 
void set_window_size (size_t size)
 
std::vector< double > train (const std::vector< T > &data)
 Train the neural network on the provided data.
 

Private Member Functions

double activation_derivative (double x) const
 
double apply_activation (double x) const
 
template<typename U >
double compute_feature (const U &arr) const
 
std::vector< double > prepare_batch (const std::vector< T > &data, size_t start_idx) const
 

Private Attributes

std::string activation_
 
size_t batch_size_
 
size_t epochs_
 
double learning_rate_
 
double threshold_
 
size_t window_size_
 

Detailed Description

template<typename T>
class neural_chunking::NeuralChunking< T >

Class implementing neural network-based chunking.

Template Parameters
TData type of elements to chunk

Definition at line 81 of file neural_chunking.hpp.

Constructor & Destructor Documentation

◆ NeuralChunking()

template<typename T >
neural_chunking::NeuralChunking< T >::NeuralChunking ( size_t  window_size = 8,
double  threshold = 0.5 
)
inline

Member Function Documentation

◆ activation_derivative()

template<typename T >
double neural_chunking::NeuralChunking< T >::activation_derivative ( double  x) const
inlineprivate

Definition at line 101 of file neural_chunking.hpp.

101 {
102 if (activation_ == "relu") {
103 return x > 0 ? 1 : 0;
104 } else if (activation_ == "sigmoid") {
105 double sig = apply_activation(x);
106 return sig * (1 - sig);
107 } else { // tanh
108 double tanh_x = std::tanh(x);
109 return 1 - tanh_x * tanh_x;
110 }
111 }
double apply_activation(double x) const

◆ apply_activation()

template<typename T >
double neural_chunking::NeuralChunking< T >::apply_activation ( double  x) const
inlineprivate

Definition at line 91 of file neural_chunking.hpp.

91 {
92 if (activation_ == "relu") {
93 return x > 0 ? x : 0;
94 } else if (activation_ == "sigmoid") {
95 return 1.0 / (1.0 + std::exp(-x));
96 } else { // tanh
97 return std::tanh(x);
98 }
99 }

◆ chunk()

template<typename T >
std::vector< std::vector< T > > neural_chunking::NeuralChunking< T >::chunk ( const std::vector< T > &  data) const
inline

Definition at line 167 of file neural_chunking.hpp.

167 {
168 if (data.empty()) {
169 return {};
170 }
171
172 // Handle case where data is smaller than window size
173 if (data.size() <= window_size_) {
174 return {data};
175 }
176
177 std::vector<std::vector<T>> result;
178 std::vector<T> current_chunk;
179
180 for (const auto& value : data) {
182 double feature = compute_feature(value);
183 if (!current_chunk.empty() &&
184 std::abs(feature - compute_feature(current_chunk.back())) > threshold_) {
185 result.push_back(current_chunk);
186 current_chunk.clear();
187 }
188 } else {
189 // Single-dimension logic
190 if (!current_chunk.empty() &&
191 std::abs(static_cast<double>(value - current_chunk.back())) > threshold_) {
192 result.push_back(current_chunk);
193 current_chunk.clear();
194 }
195 }
196 current_chunk.push_back(value);
197 }
198
199 if (!current_chunk.empty()) {
200 result.push_back(current_chunk);
201 }
202
203 return result;
204 }
double compute_feature(const U &arr) const

Referenced by main(), and TEST_F().

◆ compute_feature()

template<typename T >
template<typename U >
double neural_chunking::NeuralChunking< T >::compute_feature ( const U &  arr) const
inlineprivate

Definition at line 129 of file neural_chunking.hpp.

129 {
132 // Handle 2D arrays
133 double sum = 0.0;
134 for (const auto& inner : arr) {
135 sum += compute_feature(inner);
136 }
137 return sum / arr.size();
138 } else {
139 // Handle 1D arrays
140 return std::accumulate(arr.begin(), arr.end(), 0.0) / arr.size();
141 }
142 } else {
143 // Handle scalar values
144 return static_cast<double>(arr);
145 }
146 }

◆ get_activation()

template<typename T >
std::string neural_chunking::NeuralChunking< T >::get_activation ( ) const
inline

Get the current activation function type.

Returns
Current activation function name

Definition at line 260 of file neural_chunking.hpp.

260 {
261 return activation_;
262 }

◆ get_batch_size()

template<typename T >
size_t neural_chunking::NeuralChunking< T >::get_batch_size ( ) const
inline

Get the current batch size.

Returns
Current batch size

Definition at line 240 of file neural_chunking.hpp.

240 {
241 return batch_size_;
242 }

◆ get_epochs()

template<typename T >
size_t neural_chunking::NeuralChunking< T >::get_epochs ( ) const
inline

Get the current number of training epochs.

Returns
Current number of epochs

Definition at line 279 of file neural_chunking.hpp.

279 {
280 return epochs_;
281 }

◆ get_learning_rate()

template<typename T >
double neural_chunking::NeuralChunking< T >::get_learning_rate ( ) const
inline

Get the current learning rate.

Returns
Current learning rate

Definition at line 221 of file neural_chunking.hpp.

221 {
222 return learning_rate_;
223 }

◆ get_threshold()

template<typename T >
double neural_chunking::NeuralChunking< T >::get_threshold ( ) const
inline

Definition at line 163 of file neural_chunking.hpp.

163 {
164 return threshold_;
165 }

◆ get_window_size()

template<typename T >
size_t neural_chunking::NeuralChunking< T >::get_window_size ( ) const
inline

Definition at line 160 of file neural_chunking.hpp.

160 {
161 return window_size_;
162 }

Referenced by main().

◆ prepare_batch()

template<typename T >
std::vector< double > neural_chunking::NeuralChunking< T >::prepare_batch ( const std::vector< T > &  data,
size_t  start_idx 
) const
inlineprivate

Definition at line 114 of file neural_chunking.hpp.

114 {
115 std::vector<double> batch;
116 batch.reserve(std::min(batch_size_, data.size() - start_idx));
117
118 for (size_t i = 0; i < batch_size_ && (start_idx + i) < data.size(); ++i) {
120 batch.push_back(compute_feature(data[start_idx + i]));
121 } else {
122 batch.push_back(static_cast<double>(data[start_idx + i]));
123 }
124 }
125 return batch;
126 }

◆ set_activation()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_activation ( const std::string &  activation)
inline

Set the activation function type.

Parameters
activationActivation function name ("relu", "sigmoid", or "tanh")

Definition at line 248 of file neural_chunking.hpp.

248 {
249 if (activation != "relu" && activation != "sigmoid" && activation != "tanh") {
250 throw std::invalid_argument(
251 "Invalid activation function. Supported: relu, sigmoid, tanh");
252 }
253 activation_ = activation;
254 }

◆ set_batch_size()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_batch_size ( size_t  size)
inline

Set the batch size for training.

Parameters
sizeBatch size (must be positive)

Definition at line 229 of file neural_chunking.hpp.

229 {
230 if (size == 0) {
231 throw std::invalid_argument("Batch size must be positive");
232 }
233 batch_size_ = size;
234 }

◆ set_epochs()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_epochs ( size_t  num_epochs)
inline

Set the number of training epochs.

Parameters
num_epochsNumber of epochs (must be positive)

Definition at line 268 of file neural_chunking.hpp.

268 {
269 if (num_epochs == 0) {
270 throw std::invalid_argument("Number of epochs must be positive");
271 }
272 epochs_ = num_epochs;
273 }

◆ set_learning_rate()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_learning_rate ( double  rate)
inline

Set the learning rate for neural network training.

Parameters
rateLearning rate value (must be positive)

Definition at line 210 of file neural_chunking.hpp.

210 {
211 if (rate <= 0.0) {
212 throw std::invalid_argument("Learning rate must be positive");
213 }
214 learning_rate_ = rate;
215 }

◆ set_threshold()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_threshold ( double  threshold)
inline

Definition at line 156 of file neural_chunking.hpp.

156 {
157 threshold_ = threshold;
158 }

Referenced by main().

◆ set_window_size()

template<typename T >
void neural_chunking::NeuralChunking< T >::set_window_size ( size_t  size)
inline

Definition at line 153 of file neural_chunking.hpp.

153 {
154 window_size_ = size;
155 }

◆ train()

template<typename T >
std::vector< double > neural_chunking::NeuralChunking< T >::train ( const std::vector< T > &  data)
inline

Train the neural network on the provided data.

Parameters
dataTraining data
Returns
Vector of loss values for each epoch

Definition at line 288 of file neural_chunking.hpp.

288 {
289 if (data.size() < window_size_) {
290 throw std::invalid_argument("Training data size must be larger than window size");
291 }
292
293 // Initialize neural network layers
294 Layer<double> input_layer(window_size_, window_size_);
295 Layer<double> hidden_layer(window_size_, 1);
296
297 std::vector<double> epoch_losses;
298 epoch_losses.reserve(epochs_);
299
300 // Training loop
301 for (size_t epoch = 0; epoch < epochs_; ++epoch) {
302 double epoch_loss = 0.0;
303 size_t num_batches = (data.size() + batch_size_ - 1) / batch_size_;
304
305 for (size_t batch = 0; batch < num_batches; ++batch) {
306 size_t start_idx = batch * batch_size_;
307 auto batch_data = prepare_batch(data, start_idx);
308 if (batch_data.size() < window_size_)
309 break;
310
311 // Forward pass
312 auto hidden = input_layer.forward(batch_data);
313 for (auto& h : hidden)
314 h = apply_activation(h);
315 auto output = hidden_layer.forward(hidden);
316
317 // Compute loss
318 double target = batch_data.back();
319 double prediction = output[0];
320 double loss = 0.5 * (prediction - target) * (prediction - target);
321 epoch_loss += loss;
322
323 // Backward pass and update weights (simplified)
324 double error = prediction - target;
325 double delta = error * activation_derivative(prediction);
326
327 // Update weights (simplified backpropagation)
328 for (size_t i = 0; i < window_size_; ++i) {
329 hidden[i] -= learning_rate_ * delta * batch_data[i];
330 }
331 }
332
333 epoch_losses.push_back(epoch_loss / num_batches);
334 }
335
336 return epoch_losses;
337 }
double activation_derivative(double x) const
std::vector< double > prepare_batch(const std::vector< T > &data, size_t start_idx) const

References neural_chunking::Layer< T >::forward().

Member Data Documentation

◆ activation_

template<typename T >
std::string neural_chunking::NeuralChunking< T >::activation_
private

Definition at line 87 of file neural_chunking.hpp.

◆ batch_size_

template<typename T >
size_t neural_chunking::NeuralChunking< T >::batch_size_
private

Definition at line 86 of file neural_chunking.hpp.

◆ epochs_

template<typename T >
size_t neural_chunking::NeuralChunking< T >::epochs_
private

Definition at line 88 of file neural_chunking.hpp.

◆ learning_rate_

template<typename T >
double neural_chunking::NeuralChunking< T >::learning_rate_
private

Definition at line 85 of file neural_chunking.hpp.

◆ threshold_

template<typename T >
double neural_chunking::NeuralChunking< T >::threshold_
private

Definition at line 84 of file neural_chunking.hpp.

◆ window_size_

template<typename T >
size_t neural_chunking::NeuralChunking< T >::window_size_
private

Definition at line 83 of file neural_chunking.hpp.


The documentation for this class was generated from the following file: