This repository was archived by the owner on Feb 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayers.h
146 lines (119 loc) · 4.18 KB
/
layers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef LAYERS_H
#define LAYERS_H
#include <stddef.h>
#include "volume.h"
// What follows are the different layers of the CNN. You will not have to
// understand what these layers are actually doing. In general, each layer has
// the following operations:
//
// make_* : Creates a new instance of the layer.
// *_forward : During this operation, the layer takes a set of input volumes
// and transforms them into a set of output volumes (one output for
// each input).
// *_load : Loads the layer's weights from an file (if applicable).
//
// NOTE: You will only have to make changes to the *_forward functions for each
// layer.
// Convolutional Layer Parameters
typedef struct conv_layer {
// Required
int input_depth;
int input_width;
int input_height;
int filter_width;
int filter_height;
int stride;
int pad;
int output_depth;
// Computed
int output_width;
int output_height;
double bias;
volume_t* biases;
volume_t** filters;
} conv_layer_t;
// Creates a convolutional layer with the following parameters.
conv_layer_t* make_conv_layer(int input_width, int input_height, int input_depth, int filter_width, int num_filters,
int stride, int pad);
// Computes the forward pass for a convolutional layer on the relevant inputs
// and stores the result into the relevant outputs.
void conv_forward(conv_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end);
// Loads the convolutional layer weights from a file.
void conv_load(conv_layer_t* l, const char* file_name);
// ReLU Layer Parameters
typedef struct relu_layer {
// Required
int input_depth;
int input_width;
int input_height;
// Computed
int output_depth;
int output_width;
int output_height;
} relu_layer_t;
// Creates a ReLU layer with the following parameters.
relu_layer_t* make_relu_layer(int input_width, int input_height, int input_depth);
// Computes the forward pass for a ReLU layer (for the relevant inputs) and
// stores the result into the relevant outputs.
void relu_forward(relu_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end);
// Pool Layer Parameters
typedef struct pool_layer {
// Required
int pool_width;
int input_depth;
int input_width;
int input_height;
// Computed
int pool_height;
int stride;
int pad;
int output_depth;
int output_width;
int output_height;
} pool_layer_t;
// Creates a max pool layer with the following parameters.
pool_layer_t* make_pool_layer(int input_width, int input_height, int input_depth, int pool_width,
int stride);
// Computes the forward pass for a max pool layer on the relevant inputs and
// stores the result into the relevant outputs.
void pool_forward(pool_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end);
// FC Layer Parameters
typedef struct fc_layer {
// Required
int output_depth;
int input_depth;
int input_width;
int input_height;
// Computed
int output_width;
int output_height;
int num_inputs;
double bias;
volume_t* biases;
volume_t** filters;
} fc_layer_t;
// Creates a fully-connected layer with the following parameters.
fc_layer_t* make_fc_layer(int input_width, int input_height, int input_depth, int num_neurons);
// Computes the forward pass for a fully-connected layer on the relevant inputs
// and stores the result into the relevant outputs.
void fc_forward(fc_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end);
// Loads the fully-connected layer weights from a file.
void fc_load(fc_layer_t* l, const char* filename);
// Softmax Layer Parameters
typedef struct softmax_layer {
// Required
int input_depth;
int input_width;
int input_height;
double* likelihoods;
// Computed
int output_depth;
int output_width;
int output_height;
} softmax_layer_t;
// Creates a softmax layer with the following parameters.
softmax_layer_t* make_softmax_layer(int input_width, int input_height, int input_depth);
// Computes the forward pass for a softmax layer on the relevant inputs and
// stores the result into the relevant outputs.
void softmax_forward(softmax_layer_t* l, volume_t** inputs, volume_t** outputs, int start, int end);
#endif