Models in tom
use the model
structure. A simple network can be made like so:
struct model m;
int main(void) {
model_init(&m, 100);
model_add_layer(&m, LAYER_DENSE, 10, 200);
model_add_layer(&m, LAYER_RELU, 200, 200);
model_set_loss(&m, LOSS_MSE);
model_finalize(&m);
model_free(&m);
}
The model object.
struct model {
// First and last layers.
struct layer *first, *last;
// Number of layers.
int n_layers;
// Number of samples.
int n_samples;
// Loss object.
struct loss loss;
// We store the input, output and y matrices.
struct matrix *input, *output, *y;
// Loss output.
struct matrix *loss_output;
// Store the last gradient.
struct matrix *last_gradient;
};
Initialize an empty model object.
Free a model object. Free all the layers, optimizers, and matrices, along with the loss.
struct layer* model_add_layer(struct model *obj, enum layer_type type, int input_size, int output_size)
Add and initialize a layer on the model. Returns the layer if successful.
struct layer* model_add_conv2d_layer(struct model* obj, int input_channels, int input_height, int input_width, int n_filters, int filter_size, int stride)
Add a conv 2D layer without initializing it. Returns the layer if successful.
struct layer* model_add_maxpool2d_layer(struct model* obj, int input_channels, int input_height, int input_width, int pool_size, int stride)
Add a max pooling 2D layer without initializing it. Returns the layer if successful.
struct layer* model_add_padding2d_layer(struct model* obj, int input_channels, int input_height, int input_width, int padding_x, int padding_y)
Add a padding 2D layer without initializing it. Returns the layer if successful.
Set the model's loss.
Finalize and initialize the model.
Initialize optimizers on the model.
Predict. Takes an input and output matrix with any number of samples.
Calculate model loss.
Train the model.
Perform a forward pass on the model.
Perform a backward pass on the model.
Update each trainable layer in the model.