XCSF  1.4.7
XCSF learning classifier system
neural_layer_convolutional.c File Reference

An implementation of a 2D convolutional layer. More...

#include "neural_layer_convolutional.h"
#include "blas.h"
#include "image.h"
#include "neural_activations.h"
#include "sam.h"
#include "utils.h"
Include dependency graph for neural_layer_convolutional.c:

Go to the source code of this file.

Macros

#define N_MU   (6)
 Number of mutation rates applied to a convolutional layer. More...
 

Functions

static int get_out_height (const struct Layer *l)
 Returns the output height of a convolutional layer. More...
 
static int get_out_width (const struct Layer *l)
 Returns the output width of a convolutional layer. More...
 
static size_t get_workspace_size (const struct Layer *l)
 Returns the memory workspace size for a convolutional layer. More...
 
static void guard_malloc (const struct Layer *l)
 Check memory allocation is within bounds. More...
 
static void malloc_layer_arrays (struct Layer *l)
 Allocate memory used by a convolutional layer. More...
 
static void realloc_layer_arrays (struct Layer *l)
 Resize memory used by a convolutional layer. More...
 
void neural_layer_convolutional_free (const struct Layer *l)
 Free memory used by a convolutional layer. More...
 
void neural_layer_convolutional_init (struct Layer *l, const struct ArgsLayer *args)
 Initialises a 2D convolutional layer. More...
 
struct Layerneural_layer_convolutional_copy (const struct Layer *src)
 Initialises and copies one convolutional layer from another. More...
 
void neural_layer_convolutional_rand (struct Layer *l)
 Randomises the weights of a convolutional layer. More...
 
void neural_layer_convolutional_forward (const struct Layer *l, const struct Net *net, const double *input)
 Forward propagates a convolutional layer. More...
 
void neural_layer_convolutional_backward (const struct Layer *l, const struct Net *net, const double *input, double *delta)
 Backward propagates a convolutional layer. More...
 
void neural_layer_convolutional_update (const struct Layer *l)
 Updates the weights and biases of a convolutional layer. More...
 
void neural_layer_convolutional_resize (struct Layer *l, const struct Layer *prev)
 Resizes a convolutional layer if the previous layer has changed size. More...
 
static int neural_layer_convolutional_mutate_filter (const struct Layer *l, const double mu)
 Returns the number of kernel filters to add or remove from a layer. More...
 
static void neural_layer_convolutional_add_filters (struct Layer *l, const int N)
 Adds N filters to a layer. Negative N removes filters. More...
 
bool neural_layer_convolutional_mutate (struct Layer *l)
 Mutates a convolutional layer. More...
 
double * neural_layer_convolutional_output (const struct Layer *l)
 Returns the output from a convolutional layer. More...
 
void neural_layer_convolutional_print (const struct Layer *l, const bool print_weights)
 Prints a convolutional layer. More...
 
char * neural_layer_convolutional_json_export (const struct Layer *l, const bool return_weights)
 Returns a json formatted string of a convolutional layer. More...
 
size_t neural_layer_convolutional_save (const struct Layer *l, FILE *fp)
 Writes a convolutional layer to a file. More...
 
size_t neural_layer_convolutional_load (struct Layer *l, FILE *fp)
 Reads a convolutional layer from a file. More...
 

Variables

static const int MU_TYPE [(6)]
 Self-adaptation method for mutating a convolutional layer. More...
 

Detailed Description

An implementation of a 2D convolutional layer.

Author
Richard Preen rpree.nosp@m.n@gm.nosp@m.ail.c.nosp@m.om
Date
2016–2022.

Definition in file neural_layer_convolutional.c.

Macro Definition Documentation

◆ N_MU

#define N_MU   (6)

Number of mutation rates applied to a convolutional layer.

Definition at line 31 of file neural_layer_convolutional.c.

Function Documentation

◆ get_out_height()

static int get_out_height ( const struct Layer l)
static

Returns the output height of a convolutional layer.

Parameters
[in]lA convolutional layer.
Returns
The output height.

Definition at line 51 of file neural_layer_convolutional.c.

References Layer::height, Layer::pad, Layer::size, and Layer::stride.

Referenced by neural_layer_convolutional_init(), and neural_layer_convolutional_resize().

Here is the caller graph for this function:

◆ get_out_width()

static int get_out_width ( const struct Layer l)
static

Returns the output width of a convolutional layer.

Parameters
[in]lA convolutional layer.
Returns
The output width.

Definition at line 62 of file neural_layer_convolutional.c.

References Layer::pad, Layer::size, Layer::stride, and Layer::width.

Referenced by neural_layer_convolutional_init(), and neural_layer_convolutional_resize().

Here is the caller graph for this function:

◆ get_workspace_size()

static size_t get_workspace_size ( const struct Layer l)
static

Returns the memory workspace size for a convolutional layer.

Parameters
[in]lA convolutional layer.
Returns
The workspace size.

Definition at line 73 of file neural_layer_convolutional.c.

References Layer::channels, layer_print(), Layer::out_h, Layer::out_w, and Layer::size.

Referenced by malloc_layer_arrays(), and realloc_layer_arrays().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ guard_malloc()

static void guard_malloc ( const struct Layer l)
static

Check memory allocation is within bounds.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 90 of file neural_layer_convolutional.c.

References layer_guard_biases(), layer_guard_outputs(), and layer_guard_weights().

Referenced by malloc_layer_arrays(), and realloc_layer_arrays().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc_layer_arrays()

static void malloc_layer_arrays ( struct Layer l)
static

Allocate memory used by a convolutional layer.

Parameters
[in]lThe layer to be allocated memory.

Definition at line 102 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, Layer::delta, get_workspace_size(), guard_malloc(), Layer::mu, Layer::n_biases, N_MU, Layer::n_outputs, Layer::n_weights, Layer::output, Layer::state, Layer::temp, Layer::weight_active, Layer::weight_updates, and Layer::weights.

Referenced by neural_layer_convolutional_copy(), neural_layer_convolutional_init(), and neural_layer_convolutional_load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_convolutional_add_filters()

static void neural_layer_convolutional_add_filters ( struct Layer l,
const int  N 
)
static

Adds N filters to a layer. Negative N removes filters.

Precondition
N must be appropriately bounds checked for the layer.
Parameters
[in]lThe neural network layer to mutate.
[in]NThe number of filters to add.

Definition at line 418 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, Layer::channels, Layer::delta, Layer::i, layer_calc_n_active(), Layer::n_biases, Layer::n_filters, Layer::n_outputs, Layer::n_weights, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output, rand_normal(), realloc_layer_arrays(), Layer::size, Layer::state, Layer::weight_active, WEIGHT_SD, Layer::weight_updates, and Layer::weights.

Referenced by neural_layer_convolutional_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_convolutional_backward()

void neural_layer_convolutional_backward ( const struct Layer l,
const struct Net net,
const double *  input,
double *  delta 
)

Backward propagates a convolutional layer.

Parameters
[in]lThe layer to backward propagate.
[in]netNetwork containing the layer.
[in]inputThe input to the layer.
[out]deltaThe previous layer's error.

Definition at line 296 of file neural_layer_convolutional.c.

References Layer::bias_updates, blas_gemm(), blas_sum(), Layer::c, Layer::channels, col2im(), Layer::delta, Layer::function, Layer::height, Layer::i, im2col(), LAYER_SGD_WEIGHTS, Layer::n_biases, Layer::n_filters, Layer::n_outputs, neural_gradient_array(), Layer::options, Layer::out_h, Layer::out_w, Layer::pad, Layer::size, Layer::state, Layer::stride, Layer::temp, Layer::weight_updates, Layer::weights, and Layer::width.

Here is the call graph for this function:

◆ neural_layer_convolutional_copy()

struct Layer* neural_layer_convolutional_copy ( const struct Layer src)

Initialises and copies one convolutional layer from another.

Parameters
[in]srcThe source layer.
Returns
A pointer to the new layer.

Definition at line 203 of file neural_layer_convolutional.c.

References Layer::biases, Layer::channels, CONVOLUTIONAL, Layer::decay, Layer::eta, Layer::eta_max, Layer::eta_min, Layer::function, Layer::height, layer_defaults(), Layer::layer_vptr, malloc_layer_arrays(), Layer::max_neuron_grow, Layer::max_outputs, Layer::momentum, Layer::mu, Layer::n_active, Layer::n_biases, Layer::n_filters, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::n_weights, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::pad, Layer::size, Layer::stride, Layer::type, Layer::weight_active, Layer::weights, and Layer::width.

Here is the call graph for this function:

◆ neural_layer_convolutional_forward()

void neural_layer_convolutional_forward ( const struct Layer l,
const struct Net net,
const double *  input 
)

Forward propagates a convolutional layer.

Parameters
[in]lLayer to forward propagate.
[in]netNetwork containing the layer.
[in]inputThe input to the layer.

Definition at line 262 of file neural_layer_convolutional.c.

References Layer::biases, blas_gemm(), Layer::c, Layer::channels, Layer::function, Layer::height, Layer::i, im2col(), Layer::n_biases, Layer::n_filters, Layer::n_outputs, neural_activate_array(), Layer::out_h, Layer::out_w, Layer::output, Layer::pad, Layer::size, Layer::state, Layer::stride, Layer::temp, Layer::weights, and Layer::width.

Here is the call graph for this function:

◆ neural_layer_convolutional_free()

void neural_layer_convolutional_free ( const struct Layer l)

Free memory used by a convolutional layer.

Parameters
[in]lThe layer to be freed.

Definition at line 142 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, Layer::delta, Layer::mu, Layer::output, Layer::state, Layer::temp, Layer::weight_active, Layer::weight_updates, and Layer::weights.

◆ neural_layer_convolutional_init()

◆ neural_layer_convolutional_json_export()

char* neural_layer_convolutional_json_export ( const struct Layer l,
const bool  return_weights 
)

Returns a json formatted string of a convolutional layer.

Parameters
[in]lThe layer to return.
[in]return_weightsWhether to return the values of weights and biases.
Returns
String encoded in json format.

Definition at line 514 of file neural_layer_convolutional.c.

References Layer::eta, Layer::function, layer_weight_json(), Layer::mu, Layer::n_filters, Layer::n_inputs, N_MU, Layer::n_outputs, neural_activation_string(), Layer::pad, Layer::size, Layer::stride, and Layer::weights.

Referenced by neural_layer_convolutional_print().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_convolutional_load()

size_t neural_layer_convolutional_load ( struct Layer l,
FILE *  fp 
)

Reads a convolutional layer from a file.

Parameters
[in]lThe layer to load.
[in]fpPointer to the file to be read.
Returns
The number of elements read.

Definition at line 589 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, Layer::channels, Layer::decay, Layer::eta, Layer::eta_max, Layer::eta_min, Layer::function, Layer::height, malloc_layer_arrays(), Layer::max_neuron_grow, Layer::max_outputs, Layer::momentum, Layer::mu, Layer::n_active, Layer::n_biases, Layer::n_filters, Layer::n_inputs, N_MU, Layer::n_outputs, Layer::n_weights, Layer::options, Layer::out_c, Layer::out_h, Layer::out_w, Layer::pad, Layer::size, Layer::stride, Layer::weight_active, Layer::weight_updates, Layer::weights, and Layer::width.

Here is the call graph for this function:

◆ neural_layer_convolutional_mutate()

bool neural_layer_convolutional_mutate ( struct Layer l)

Mutates a convolutional layer.

Parameters
[in]lThe layer to mutate.
Returns
Whether any alterations were made.

Definition at line 452 of file neural_layer_convolutional.c.

References LAYER_EVOLVE_CONNECT, LAYER_EVOLVE_ETA, LAYER_EVOLVE_FUNCTIONS, LAYER_EVOLVE_NEURONS, LAYER_EVOLVE_WEIGHTS, layer_mutate_connectivity(), layer_mutate_eta(), layer_mutate_functions(), layer_mutate_weights(), Layer::mu, MU_TYPE, N_MU, neural_layer_convolutional_add_filters(), neural_layer_convolutional_mutate_filter(), Layer::options, and sam_adapt().

Here is the call graph for this function:

◆ neural_layer_convolutional_mutate_filter()

static int neural_layer_convolutional_mutate_filter ( const struct Layer l,
const double  mu 
)
static

Returns the number of kernel filters to add or remove from a layer.

Parameters
[in]lThe neural network layer to mutate.
[in]muThe rate of mutation.
Returns
The number of filters to be added or removed.

Definition at line 394 of file neural_layer_convolutional.c.

References clamp(), Layer::max_neuron_grow, Layer::max_outputs, Layer::mu, Layer::n_filters, rand_normal(), and rand_uniform().

Referenced by neural_layer_convolutional_mutate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ neural_layer_convolutional_output()

double* neural_layer_convolutional_output ( const struct Layer l)

Returns the output from a convolutional layer.

Parameters
[in]lThe layer whose output to return.
Returns
The layer output.

Definition at line 487 of file neural_layer_convolutional.c.

References Layer::output.

◆ neural_layer_convolutional_print()

void neural_layer_convolutional_print ( const struct Layer l,
const bool  print_weights 
)

Prints a convolutional layer.

Parameters
[in]lThe layer to print.
[in]print_weightsWhether to print the values of weights and biases.

Definition at line 498 of file neural_layer_convolutional.c.

References neural_layer_convolutional_json_export().

Here is the call graph for this function:

◆ neural_layer_convolutional_rand()

void neural_layer_convolutional_rand ( struct Layer l)

Randomises the weights of a convolutional layer.

Parameters
[in]lThe layer to randomise.

Definition at line 250 of file neural_layer_convolutional.c.

References layer_weight_rand().

Here is the call graph for this function:

◆ neural_layer_convolutional_resize()

void neural_layer_convolutional_resize ( struct Layer l,
const struct Layer prev 
)

Resizes a convolutional layer if the previous layer has changed size.

Parameters
[in]lThe layer to resize.
[in]prevThe layer previous to the one being resized.

Definition at line 361 of file neural_layer_convolutional.c.

References Layer::channels, Layer::delta, get_out_height(), get_out_width(), Layer::height, Layer::i, layer_calc_n_active(), Layer::n_filters, Layer::n_inputs, Layer::n_outputs, Layer::n_weights, Layer::out_c, Layer::out_h, Layer::out_w, Layer::output, rand_normal(), realloc_layer_arrays(), Layer::size, Layer::state, Layer::weight_active, WEIGHT_SD, Layer::weight_updates, Layer::weights, and Layer::width.

Here is the call graph for this function:

◆ neural_layer_convolutional_save()

size_t neural_layer_convolutional_save ( const struct Layer l,
FILE *  fp 
)

◆ neural_layer_convolutional_update()

void neural_layer_convolutional_update ( const struct Layer l)

Updates the weights and biases of a convolutional layer.

Parameters
[in]lThe layer to update.

Definition at line 340 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, blas_axpy(), blas_scal(), Layer::decay, Layer::eta, LAYER_SGD_WEIGHTS, layer_weight_clamp(), Layer::momentum, Layer::n_biases, Layer::n_weights, Layer::options, Layer::weight_updates, and Layer::weights.

Here is the call graph for this function:

◆ realloc_layer_arrays()

static void realloc_layer_arrays ( struct Layer l)
static

Resize memory used by a convolutional layer.

Parameters
[in]lThe layer to be reallocated memory.

Definition at line 122 of file neural_layer_convolutional.c.

References Layer::bias_updates, Layer::biases, Layer::delta, get_workspace_size(), guard_malloc(), Layer::n_biases, Layer::n_outputs, Layer::n_weights, Layer::output, Layer::state, Layer::temp, Layer::weight_active, Layer::weight_updates, and Layer::weights.

Referenced by neural_layer_convolutional_add_filters(), and neural_layer_convolutional_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ MU_TYPE

const int MU_TYPE[(6)]
static
Initial value:
= {
(1) ,
(1) ,
(1) ,
(1) ,
(1) ,
(1)
}

Self-adaptation method for mutating a convolutional layer.

Definition at line 36 of file neural_layer_convolutional.c.

Referenced by neural_layer_convolutional_init(), and neural_layer_convolutional_mutate().