CNN Backprop in C
unknown
c_cpp
3 years ago
2.3 kB
11
Indexable
// Forward propagation void forward_propagation(float input_data[][][], float filters[][][][], float biases[], float output_data[][][], int n_layers, int n_filters[], int filter_size[], int stride[], int padding[]) { int curr_input_dim, curr_output_dim; int n_channels, curr_filter, curr_stride, curr_padding; // Loop through the layers for (int layer = 0; layer < n_layers; layer++) { // Get the current layer's filter and stride size, as well as the number of channels in the input data curr_filter = filter_size[layer]; curr_stride = stride[layer]; n_channels = input_data[layer].length; curr_padding = padding[layer]; // Calculate the dimensions of the input and output data for this layer curr_input_dim = (input_data[layer].length - curr_filter + 2 * curr_padding) / curr_stride + 1; curr_output_dim = (curr_input_dim - curr_filter + 2 * curr_padding) / curr_stride + 1; // Loop through the filters in this layer for (int f = 0; f < n_filters[layer]; f++) { // Loop through the channels in the input data for (int c = 0; c < n_channels; c++) { // Loop through the rows of the input data for (int i = 0; i < curr_input_dim; i++) { // Loop through the columns of the input data for (int j = 0; j < curr_input_dim; j++) { // Convolve the filter with the input data and add the bias output_data[layer][f][i][j] = biases[layer][f]; for (int fi = 0; fi < curr_filter; fi++) { for (int fj = 0; fj < curr_filter; fj++) { output_data[layer][f][i][j] += input_data[layer][c][i + fi][j + fj] * filters[layer][f][c][fi][fj]; } } // Apply the activation function output_data[layer][f][i][j] = sigmoid(output_data[layer][f][i][j]); } } } } // Set the input data for the next layer to be the output of this layer input_data[layer+1] = output_data[layer]; } } // Backward propagation void backward_propagation(float input_data[][][], float filters[][][][], float biases[], float output_data[][][], float d_output_data[][][][], int n_layers, int n_filters[], int filter_size[], int stride[],
Editor is loading...