## 4 Core Algorithm and Implementation
### Structure Design of Convolution Layer  
As mentioned above, the LeNet-3.3 convolutional neural network in this paper has six network layers, including three convolutional layers and three fully connected layers. Therefore, the structure of these two network layers can be optimized in the design of a neural network accelerator. The first three network layers are convolutional layers implemented in the same way. In this design, the convolution operations of forward inference and gradient descent in network training will be considered comprehensively to improve the readability and maintainability of the code. The forward and backward propagation paths of the single-layer convolution are shown in Figure 6.  
Forward propagation of the convolution layer is expressed as:  
\[Z^{l}(x,y)=\sum_{a}\sum_{b}Z^{l-1}(x+a,y+b)*w^{l}(a,b) \tag{1}\]  
where \(w^{l}(a,b)\) is the element whose corresponding coordinate is \((a,b)\) in the convolution kernel of the \(l\)-th layer convolution, and \(z^{l}(x,y)\) is the element whose corresponding coordinate is \((x,y)\) in the output of the \(l\)-th layer convolution. Equation (1) is the relation of forward convolution propagation. The convolution operation of the LeNet-3.3 network does not include bias, pooling, and activation. The forward propagation algorithm of the convolution layer is implemented as shown in Algorithm 1. Figure 6: Data propagation in the convolution layer. layer (Equation (1)), it can be seen that the \(\delta\) loss of the \(l\)-th layer is the convolution of the \(\delta\) loss of the \(l+1\)-th layer, and the rotation of the convolution kernel of the \(l+1\)-th layer by \(180^{\circ}\). However, considering the dimensions of the input and output matrices in the above convolution operation, the convolution kernel dimension is k, the dimension of the loss \(\delta^{l}(x,y)\) of the \(l\)-th layer is equal to the dimension i of the input matrix \(Z^{l}\) of the convolution layer, and the dimension of the loss \(\delta^{l+1}(x,y)\) of the \(l+1\)-th layer is equal to the dimension j of the output matrix \(Z^{l+1}\) of the convolution layer. From the forward propagation of the convolution, we can see that j = i - k + 1, so zero filling of the loss \(\delta^{l+1}(x,y)\) of the \(l+1\)-th layer is required in the backpropagation, and filling (k - 1) on each side makes \(\delta^{l+1}(x,y)\) the dimension j + 2 \(\times\) (k - 1). After the convolution operation, the output dimension is j + 2 \(\times\) (k - 1) - k + 1 = j + k - 1 = i. Matrix dimension i comforms to the loss \(\delta^{l}(x,y)\) of the \(l\)-th layer. In summary, given the output loss of the \(l+1\)-th layer convolution, it is necessary to calculate the output loss of the \(l\)-th layer convolution in three steps: the first step is to rotate the convolution kernel \(w^{l}\)\(180^{\circ}\); in the second step, each side of the loss \(\delta^{l+1}(x,y)\) of the \(l+1\)-th layer is filled with the zero value of (k - 1); the third step is to use the above convolution algorithm convolution for operations. The rotating convolution kernel operation is implemented as shown in Algorithm 2. ```
k: Convolutional kernel dimension input_matrix: Convolution kernel before rotation out_matrix: Rotated convolution kernel 1.for(int i = 0; i < k; i++)
2.for(int j = 0; j < k; j++)
3. output_matrix[(k - 1 - i) \(\times\) k + (k - 1 - j)] = input_matrix[i \(\times\) k + j];
```  
**Algorithm 2** OverturnKernel. The implementation process of matrix zero-value filling is as shown in Algorithm 3.