I am trying to implement a convolutional neural netwrok and I don't understand why using im2col operation is more efficient. It basically stores the input to be multiplied by filter in separate columns. But why shouldn't loops be used directly to calculate convolution instead of first performing im2col ?
相关问题
- neural network does not learn (loss stays the same
- Convolutional Neural Network seems to be randomly
- How to convert Onnx model (.onnx) to Tensorflow (.
- XOR Java Neural Network
- Training with dropout
相关文章
- how to flatten input in `nn.Sequential` in Pytorch
- Looping through training data in Neural Networks B
- Why does this Keras model require over 6GB of memo
- How to measure overfitting when train and validati
- Create image of Neural Network structure
- Normalized Cross-Correlation in Python
- Why does different batch-sizes give different accu
- Neural Network – Predicting Values of Multiple Var
Well, you are thinking in the right way, In Alex Net almost 95% of the GPU time and 89% on CPU time is spent on the Convolutional Layer and Fully Connected Layer.
The Convolutional Layer and Fully Connected Layer are implemented using GEMM that stands for General Matrix to Matrix Multiplication.
So basically in GEMM, we convert the convolution operation to a Matrix Multiplication operation by using a function called
im2col()
which arranges the data in a way that the convolution output can be achieved by Matrix Multiplication.Now, you may have a question instead of directly doing element wise convolution, why are we adding a step in between to arrange the data in a different way and then use GEMM.
The answer to this is, scientific programmers, have spent decades optimizing code to perform large matrix to matrix multiplications, and the benefits from the very regular patterns of memory access outweigh any other losses. We have an optimized CUDA GEMM API in cuBLAS library, Intel MKL has an optimized CPU GEMM while ciBLAS's GEMM API can be used for devices supporting OpenCL.
Element wise convolution performs badly because of the irregular memory accesses involved in it.
In turn,
Im2col()
arranges the data in a way that the memory accesses are regular for Matrix Multiplication.Im2col()
function adds a lot of data redundancy though, but the performance benefit of using Gemm outweigh this data redundancy.This is the reason for using
Im2col()
operation in Neural Nets.This link explains how
Im2col()
arranges the data for GEMM: https://petewarden.com/2015/04/20/why-gemm-is-at-the-heart-of-deep-learning/