AIfES 2  2.0.0
aifes_core.h
Go to the documentation of this file.
1 
28 #ifndef AIFES_CORE
29 #define AIFES_CORE
30 
31 #include "aifes_math.h"
32 #include "aifes_config.h"
33 
34 #define TRUE 1
35 #define FALSE 0
36 
37 // ToDo: Make enum from the values
38 #define AILAYER_RESULT_LOWER_BOUND 0
39 #define AILAYER_RESULT_UPPER_BOUND 1
40 #define AILAYER_DELTAS_LOWER_BOUND 2
41 #define AILAYER_DELTAS_UPPER_BOUND 3
42 
43 typedef struct ailayer ailayer_t;
44 typedef struct ailoss ailoss_t;
45 typedef struct aimodel aimodel_t;
46 typedef struct aiopti aiopti_t;
47 
49 typedef struct aicore_losstype aicore_losstype_t;
50 typedef struct aicore_optitype aicore_optitype_t;
51 
52 
80  const char *name;
89  void (*print_specs)(const ailayer_t *self);
90 };
91 
119  const char *name;
128  void (*print_specs)(const ailoss_t *self);
129 };
130 
158  const char *name;
167  void (*print_specs)(const aiopti_t *self);
168 };
169 
178 struct aimodel {
182  uint16_t layer_count;
186 };
187 
188 
249 struct ailayer {
257  ailayer_t *input_layer;
258  //ailayer_t *brother_input_layer; /**< (NOT_IN_USE) Chained list if multiple input layer are present else NULL. */
259 
260  ailayer_t *output_layer;
261  //ailayer_t *brother_output_layer; /**< (NOT_IN_USE) Chained list if multiple output layer are present else NULL. */
263 
268  ailayer_t *next_scheduled;
269  ailayer_t *prev_scheduled;
271 
280  void (*calc_result_shape)(ailayer_t *self);
281 
290 
295  void (*forward)(ailayer_t *self);
296 
297  // Maybe for later purpose
298  //void (*sizeof_infmem)(struct aifes_layer_t *, void *);
299  //void (*set_infmem)(struct aifes_layer_t *, void *);
300 
301  // ------------------ Used for training only: -----------------------
302 
314  void **optimem;
316 
321  void (*backward)(ailayer_t *self);
322 
331  uint32_t (*sizeof_paramem)(const ailayer_t *self);
332  void (*set_paramem)(ailayer_t *self, void* memory_ptr);
334 
342  uint32_t (*sizeof_trainmem)(const ailayer_t *self);
343  void (*set_trainmem)(ailayer_t *self, void* memory_ptr);
345 };
346 
347 
355 struct ailoss {
367  void (*calc_loss)(ailoss_t *self, const aitensor_t *target_data, void *result);
368 
374  void (*calc_delta)(ailoss_t *self, const aitensor_t *target_data);
375 };
376 
377 
408 struct aiopti {
420  uint32_t (*sizeof_optimem)(aiopti_t *self, const aitensor_t *params);
421 
429  void (*init_optimem)(aiopti_t *self, const aitensor_t *params, const aitensor_t *gradients, void *optimem);
430 
436  void (*zero_gradients)(aiopti_t *self, aitensor_t *gradients);
437 
442  void (*begin_step)(aiopti_t *self);
443 
451  void (*update_params)(aiopti_t *self, aitensor_t *params, const aitensor_t *gradients, void *optimem);
452 
457  void (*end_step)(aiopti_t *self);
458 };
459 
460 
461 #endif // AIFES_CORE
AIfES 2 math interface.
Type indicator of the layer.
Definition: aifes_core.h:79
void(* print_specs)(const ailayer_t *self)
Set a function to print specs of the layer (for example size, constants)
Definition: aifes_core.h:89
const char * name
Name of the layer type (for example "Dense")
Definition: aifes_core.h:80
Type indicator of the loss to check for the loss type.
Definition: aifes_core.h:118
void(* print_specs)(const ailoss_t *self)
Set a function to print specs of the loss.
Definition: aifes_core.h:128
const char * name
Name of the loss type (for example "Mean Squared Error")
Definition: aifes_core.h:119
Type indicator of the optimizer to check for the optimizer type.
Definition: aifes_core.h:157
void(* print_specs)(const aiopti_t *self)
Set a function to print specs of the optimizer.
Definition: aifes_core.h:167
const char * name
Name of the optimizer type (for example "ADAM")
Definition: aifes_core.h:158
AIfES layer interface.
Definition: aifes_core.h:249
void ** optimem
Array of memory pointers with length trainable_params_count.
Definition: aifes_core.h:314
aitensor_t result
The result of the forward function is stored here.
Definition: aifes_core.h:272
void(* set_paramem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:332
void(* backward)(ailayer_t *self)
Calculate the backward pass and write the result to the deltas tensor.
Definition: aifes_core.h:321
const aicore_layertype_t * layer_type
Type of the layer (for example ailayer_dense_type)
Definition: aifes_core.h:250
void * layer_configuration
Layer specific configurations (back-link from abstract layer class to implementation)
Definition: aifes_core.h:251
uint8_t trainable_params_count
Number of trainable parameter tensors.
Definition: aifes_core.h:311
void(* set_trainmem)(ailayer_t *self, void *memory_ptr)
Set and distribute the memory block internally.
Definition: aifes_core.h:343
void(* forward)(ailayer_t *self)
Calculate the forward pass and write the result to the result tensor.
Definition: aifes_core.h:295
void(* calc_result_shape)(ailayer_t *self)
Calculate and write the shape to the result tensor.
Definition: aifes_core.h:280
aitensor_t deltas
The result of the backward function is stored here.
Definition: aifes_core.h:303
aitensor_t ** trainable_params
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:312
void(* calc_result_tensor_params)(ailayer_t *self)
If available, calculate and set the tensor_params of the result tensor.
Definition: aifes_core.h:289
uint32_t(* sizeof_paramem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:331
aitensor_t ** gradients
Array of tensor pointers with length trainable_params_count.
Definition: aifes_core.h:313
uint32_t(* sizeof_trainmem)(const ailayer_t *self)
Size of required memory (in bytes).
Definition: aifes_core.h:342
AIfES loss interface.
Definition: aifes_core.h:355
void * loss_configuration
Loss specific configurations (back-link from abstract loss class to implementation)
Definition: aifes_core.h:357
const aicore_losstype_t * loss_type
Type of the loss (for example ailoss_mse_type)
Definition: aifes_core.h:356
ailayer_t connection_layer
Dummy layer for docking to the layer structure.
Definition: aifes_core.h:359
void(* calc_delta)(ailoss_t *self, const aitensor_t *target_data)
Calculate the error on the target data and write it to the deltas tensor of connection layer.
Definition: aifes_core.h:374
void(* calc_loss)(ailoss_t *self, const aitensor_t *target_data, void *result)
Calculate the loss / cost for the model on the given targets.
Definition: aifes_core.h:367
Indicator for the used datatype.
Definition: aifes_math.h:44
AIfES artificial neural network model.
Definition: aifes_core.h:178
uint16_t trainable_params_count
Total number of trainable parameter tensors.
Definition: aifes_core.h:183
uint16_t layer_count
Total number of layers of the model (usually autogenerated).
Definition: aifes_core.h:182
ailayer_t * input_layer
Input layer of the model that gets the input data.
Definition: aifes_core.h:179
ailayer_t * output_layer
Output layer of the model.
Definition: aifes_core.h:180
ailoss_t * loss
The loss or cost function of the model (only for training).
Definition: aifes_core.h:185
AIfES optimizer interface.
Definition: aifes_core.h:408
void * optimizer_configuration
Optimizer specific configurations (back-link from abstract aiopti class to implementation)
Definition: aifes_core.h:410
void(* init_optimem)(aiopti_t *self, const aitensor_t *params, const aitensor_t *gradients, void *optimem)
Initialize the optimization memory for a trainable parameter tensor.
Definition: aifes_core.h:429
void(* begin_step)(aiopti_t *self)
Called in the beginning of every model optimization step for parameter initialization.
Definition: aifes_core.h:442
const aicore_optitype_t * optimizer_type
Type of the optimizer (for example aiopti_sgd_type)
Definition: aifes_core.h:409
void(* update_params)(aiopti_t *self, aitensor_t *params, const aitensor_t *gradients, void *optimem)
Performs an optimization step on the given tensor.
Definition: aifes_core.h:451
uint32_t(* sizeof_optimem)(aiopti_t *self, const aitensor_t *params)
Calculates the optimization memory size for a trainable parameter tensor.
Definition: aifes_core.h:420
void * learning_rate
The learning rate configures the training speed.
Definition: aifes_core.h:413
const aimath_dtype_t * dtype
The data-type of the parameter that the optimizer can optimize and the learning rate.
Definition: aifes_core.h:411
void(* zero_gradients)(aiopti_t *self, aitensor_t *gradients)
Set the gradient tensor to zero.
Definition: aifes_core.h:436
void(* end_step)(aiopti_t *self)
Called in the end of every model optimization step.
Definition: aifes_core.h:457
A tensor in AIfES.
Definition: aifes_math.h:89