machine-learning
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    7
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: machine-learning --- # Convolutional Neural Network with Numpy (Slow) <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/0.png" width="80%"> </div> > In this post, we are going to see how to implement a Convolutional Neural Network using only Numpy. > > The main goal here is not only to give a boilerplate code but rather to have an in-depth explanation of the underlying mechanisms through illustrations, especially during the backward propagation where things get trickier. > > However, some knowledge about Convolutional Neural Networks building blocs are required. > > To see the full implementation, please refer to my [repository](https://github.com/3outeille/CNNumpy). > ><a href="https://hackmd.io/@machine-learning/blog-post-cnnumpy-fast" style="color:red"> For the more advanced, here is another post</a> **where we implement a faster CNN using im2col/col2im methods**. > > Also, if you want to read some of my blog posts, feel free to check them at my [blog](https://3outeille.github.io/deep-learning/). # I) Architecture We are going to implement the LeNet-5 architecture. <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/1.png"> </div> <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/2.png"> </div> # II) Forward propagation - An image is of shape (h, w, c) where: - h: image height. - w: image width. - c: channel. - Here is an RGB image (3 channels): <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/3.png" width="70%"> </div> ## 1) Convolutional layer - A convolution operation is defined as follow: $$\boxed{Conv(Input, Kernel) = Output}$$ :::info - In theory, a convolution operation is a cross-correlation operation with its kernels flipped by 180°. - In practice, we don't really care whether or not a convolution or a cross-correlation was used since the main goal is to learn the kernels (Indeed, if you were to learn for example, english irregular verbs. It doesn't matter if you learn them from top to bottom or bottom to top. The most important thing is you learn them !). - However, if you decide to use a convolution, you will have to apply a **rotation during the forward and backward propagation**. - For your information, Pytorch **nn.Conv2d()** uses cross-correlation. - **To make it easier in this blog post, convolution will refer to cross-correlation**. ::: - We are going to explain: - How to get the output shape after a convolution. - How the content of the output is generated after a convolution. --- - At the begining of our architecture, we want to make a convolution between an input of shape (32,32,1) and 6 kernels of shape (5,5,1). - In order to perform a convolution operation, **both Input and Kernel must have the exact same number of channels** (in our case 1). - The output will be a new image of shape (28,28,6). - ==Since we have here 6 kernels, we are going to perform 6 convolution operations which will produce 6 outputs (feature maps).== - ==The 6 feature maps will then be stacked to form the 6 channels of the new image.== - During the convolution operation, the following formula is used to get the (28,28) shape: $$O = \left \lfloor \frac{I + 2p - K}{s} + 1 \right \rfloor$$ - O: Output shape. - I: Input shape. - p: padding = 0 (default value). - K: Kernel shape. - s: stride = 1 (default value). - $\left \lfloor ... \right \rfloor$: floor function. <br> - Using the formula above for our example, we have: \begin{align*} O &= \left \lfloor \frac{32 + 2*0 - 5}{1} + 1 \right \rfloor \\ &= \left \lfloor \frac{27}{1} + 1 \right \rfloor \\ &= 28 \end{align*} --- - Now that we know where does the output shape come from, let's see how the content of the output image is generated. - ==During the convolution operation, **the kernel is sliding over the whole input**.== - In our following example, we perform a convolution between a (5,5,3) input and 1 kernel of size (3,3,3) to get an (3,3,1) image. - ==**At each slide, we perform an element-wise multiplication and sum everything to get a single value**.== <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/4.gif"> </div> <br> - If we perform a convolution between an (5,5,3) input and 6 kernels of shape (3,3,3). We will have to repeat the convolution operation 6 times on each of the (3,3,3) kernel. - This will result in an output of shape (3,3,6). - Here is an implementation of what we have seen so far. ```python= def forward_conv(self, X): """ Performs a forward convolution. Parameters: - X : Last conv layer of shape (m, n_C_prev, n_H_prev, n_W_prev). Returns: - out: output of convolution. """ self.cache = X m, n_C_prev, n_H_prev, n_W_prev = X.shape # Define output size. n_C = self.n_F n_H = int((n_H_prev + 2 * self.p - self.f)/ self.s) + 1 n_W = int((n_W_prev + 2 * self.p - self.f)/ self.s) + 1 out = np.zeros((m, n_C, n_H, n_W)) for i in range(m): # For each image. for c in range(n_C): # For each channel. for h in range(n_H): # Slide the filter vertically. h_start = h * self.s h_end = h_start + self.f for w in range(n_W): # Slide the filter horizontally. w_start = w * self.s w_end = w_start + self.f # Element wise multiplication + sum. out[i, c, h, w] = np.sum(X[i, :, h_start:h_end, w_start:w_end] * self.W['val'][c, ...]) + self.b['val'][c] return out ``` ## 2) Pooling layer - Our architecture use **average pooling** layer. Another common pooling layer is the max pooling layer. - **The goal of the average pooling is to reduce the height and width of our image but not the number of channels** by using a stride > 1. <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/5-a.gif" width="80%"> </div> - Here is the implementation. ```python= def forward_pool(self, X): """ Apply average pooling. Parameters: - X: Output of activation function. Returns: - A_pool: X after average pooling layer. """ m, n_C_prev, n_H_prev, n_W_prev = X.shape # Define output size. n_C = n_C_prev n_H = int((n_H_prev - self.f)/ self.s) + 1 n_W = int((n_W_prev - self.f)/ self.s) + 1 A_pool = np.zeros((m, n_C, n_H, n_W)) for i in range(m): for c in range(n_C): for h in range(n_H): h_start = h * self.s h_end = h_start + self.f for w in range(n_W): w_start = w * self.s w_end = w_start + self.f A_pool[i, c, h, w] = np.mean(X[i, c, h_start:h_end, w_start:w_end]) self.cache = X return A_pool ``` ## 3) Multilayer perceptron - ==To go from an output of convolutional/pooling layers to a MLP part, we have to flatten the output.== - For example, the last convolutional layer gives an output of shape (5,5,16). - After flattening it, we get a (5x5x16) = (400). - Then, we just perform a weighted sum. - For more information about it, please refer to my other [blog post](https://hackmd.io/@machine-learning/SkXPHwL8L#II-Forward-propagation) about forward propagation in MLP. - Here is the implementation. ```python= def forward_mlp(self, fc): """ Performs a forward propagation between 2 fully connected layers. Parameters: - fc: fully connected layer. Returns: - A_fc: new fully connected layer. """ self.cache = fc A_fc = np.dot(fc, self.W['val'].T) + self.b['val'] return A_fc ``` # III) Backward propagation Here comes the tricky part. Most of the tutorials I have read so far only say that the backward propagation is the same as in a MLP (which is true). However, we will see that it's not that straightforward to implement especially at the convolution layer. <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/5-b.png" width="80%"> </div> ## 1) Multilayer perceptron - To compute the loss gradient, we first need to compute the errors $\delta$. - The error $\delta$ is computed differently when you are at: - The last layer $L$ of the network (softmax level). - Every other layers $l$. - For more details, please refer to one of my [blog post](https://hackmd.io/1I0ij9HAQM-ocZIQu6aCWQ?view#IV-Backward-propagation). --- - At the last layer $L$, the formula to compute the error is: $$\boxed{\delta^{(L)} = (a^{(L)} - y)}$$ - $\delta^{(L)}$: error at last layer. - $a^{(L)}$: activation function output at last layer. - $y$: ground truth label. <br> - At every other layers $l$, the formula to compute the error is: $$\boxed{\delta^{(l)} = ((\Theta^{(l+1)})^T \delta^{(l+1)})\ .*\ a'(z^{(l)})}$$ - $\delta^{(l)}$: error at layer $l$. - $\Theta^{(l+1)}$: Weight matrix at layer $l+1$. - $\delta^{(l+1)}$: error at layer $l+1$. - $a'(z^{(l)})$: derivative of activation function at layer $l$. <br> - We can then compute the loss gradient at each layer with the following formula: $$\boxed{\dfrac{\partial \mathcal{L}}{\partial \Theta_{j,k}^{(l)}} = \frac{1}{m}\sum_{t=1}^m a_j^{(t)(l)} {\delta}_k^{(t)(l+1)}}$$ - This will then be used to update your weigths: $$\boxed{\Theta^{(l)} \leftarrow \Theta^{(l)} - \alpha \frac{\mathrm{\partial \mathcal{L}} }{\mathrm{\partial}\Theta^{(l)}}}$$ - Here is the implementation: ```python= def backward(self, deltaL): """ Returns the error of the current layer and compute gradients. Parameters: - deltaL: error at last layer. Returns: - new_deltaL: error at current layer. """ fc = self.cache m = fc.shape[0] # Compute gradient. self.W['grad'] = (1/m) * np.dot(deltaL.T, fc) self.b['grad'] = (1/m) * np.sum(deltaL, axis = 0) # Compute error. # We still need to multiply new_deltaL by the derivative of the activation # function which is done in TanH.backward(). new_deltaL = np.dot(deltaL, self.W['val']) return new_deltaL, self.W['grad'], self.b['grad'] ``` ## 2) Pooling layer - During forward propagation, we were averaging value of the input within the pooling window size. - ==During backward propagation, we need to **proportionally** back-propagate the error to the input.== - ==**Remember, no weights gradient are computed here! We only compute the layer gradient.**== <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/6.gif"> </div> - Here is the implementation: ```python= def backward(self, dout): """ Distributes error through pooling layer. Parameters: - dout: Previous layer with the error. Returns: - dX: Conv layer updated with error. """ X = self.cache # Define output size. m, n_C, n_H, n_W = dout.shape dX = np.zeros(X.shape) for i in range(m): # For each image. for c in range(n_C): # For each channel. for h in range(n_H): # Slide the filter vertically. h_start = h * self.s h_end = h_start + self.f for w in range(n_W): # Slide the filter horizontally. w_start = w * self.s w_end = w_start + self.f average = dout[i, c, h, w] / (self.f * self.f) filter_average = np.full((self.f, self.f), average) dX[i, c, h_start:h_end, w_start:w_end] += filter_average return dX ``` ## 3) Convolutional layer - Let's come back to the part of the architecture where we have to perform a convolution between an (14,14,6) input and 16 kernels of shape (5,5,6). - This will output us an (10,10,16) image. <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/7.png" width="40%"> </div> - The idea of backward propagation is to back-propagate the gradient from lower layers to upper layers. - In order to perform backward propagation in the example above, we have to do 2 things: - ==a) Compute the layer gradients $\frac{\partial L}{\partial I}$ at layer (10, 10, 16).== - ==b) Compute the kernel gradients $\frac{\partial L}{\partial K}$ at layer (10,10,16).== - The optimizer (SGD, Adam, RMSprop) will then update the kernels value. - In the following, I will be using 2 formulas. For more details, feel free to take a look at this [blog post](https://www.jefkine.com/general/2016/09/05/backpropagation-in-convolutional-neural-networks/) and at this [note](http://people.csail.mit.edu/jvb/pubs/papers/cnn_tutorial.pdf). --- ### ==a) Compute Layer gradient== - The formula to compute the layer gradient is: $$\boxed{\frac{\partial L}{\partial I} = Conv(K, \frac{\partial L}{\partial O})}$$ - $\frac{\partial L}{\partial I}$: Input gradient. - $K$: Kernels. - $\frac{\partial L}{\partial O}$: Output gradient. - $Conv$: Convolution operation. <br> - However, there is a little problem when we actually want to implement it. - ==The formula asks us to perform a convolution operation between **16** kernels $K$ of shape **(5,5,6)** and $\frac{\partial L}{\partial O}$ of shape **(10, 10, 16)**.== - ==But we know that in order to perform a convolution operation, **both arguments of the convolution operation need to have the same exact number of channels** which is not the case here (6 != 16).== - During forward propagation, in order to get the (10,10, 16) output, 16 convolutions were performed between the input (14,14,6) and 16 kernels of shape (5,5,6). - ==During backward propagation, the **16 channels (feature maps) of the (10, 10) output now contain the gradient** that need to be **back-propagate to the input layer (14, 14, 6)**.== - ==Thus, **we need to "broadcast" the gradient in each feature map of the (10,10) to its associate filter which will then be used to compute the input (14,14,6)**.== <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/8.gif"> </div> - As you can see the sliding of kernels over the (14,14,6) input is in fact a **convolution** ! It was less obvious to notice it though. <br> --- ### ==a) Compute Kernel gradient== - The formula to compute the kernel gradient is: $$\boxed{\frac{\partial L}{\partial K} = Conv(I, \frac{\partial L}{\partial O})}$$ - $\frac{\partial L}{\partial K}$: Kernels gradient. - $I$: Input image. - $\frac{\partial L}{\partial O}$: Output gradient. - $Conv$: Convolution operation. <br> - Same problem than before, performing a convolution is again not straightforward because of channels mismatch (6 != 16). - During forward propagation, we perform convolution between the input (14,14,6) and 16 kernels of shape (5,5,6) which output us an (10,10,16) image. - Thus, each feature map were made by a convolution between the input and each kernel. - ==Then, during backward propagation, **each feature map of the output contains the gradient** that needs to be back-propagate to each filter.== - ==It makes sense that we need to **"broadcast" the gradient in each feature map of the (10,10) to each "slide" we did during forward propagation over the input and add it to its associate filter.**== <div style="text-align: center"> <img src="https://raw.githubusercontent.com/valoxe/image-storage-1/master/blog-deep-learning/cnnumpy-naive/9.gif"> </div> --- - Here is the implementation for the above steps: ```python= def backward(self, dout): """ Distributes error from previous layer to convolutional layer and compute error for the current convolutional layer. Parameters: - dout: error from previous layer. Returns: - deltaL: error of the current convolutional layer. """ X = self.cache m, n_C, n_H, n_W = X.shape m, n_C_dout, n_H_dout, n_W_dout = dout.shape dX = np.zeros(X.shape) #Compute dW. for i in range(m): # For each example. for c in range(n_C_dout): # For each channel. for h in range(n_H_dout): # Slide the filter vertically. h_start = h * self.s h_end = h_start + self.f for w in range(n_W_dout): # Slide the filter horizontally. w_start = w * self.s w_end = w_start + self.f self.W['grad'][c, ...] += dout[i, c, h, w] * X[i, :, h_start:h_end, w_start:w_end] dX[i, :, h_start:h_end, w_start:w_end] += dout[i, c, h, w] * self.W['val'][c, ...] #Compute db. for c in range(self.n_F): self.b['grad'][c, ...] = np.sum(dout[:, c, ...]) return dX, self.W['grad'], self.b['grad'] ``` :::info - At this point of the post, I hope you now understand how to build a Convolutional Neural Network from scratch in a naive way. - However, the naive implementation takes a lot of time to train (mainly due to the nested for loops). - As an example, it takes around 4 hours to perform a single epoch on the MNIST dataset. - In the following <a href="https://hackmd.io/@bouteille/B1Cmns09I" style="color:red">post</a>, we are going to see how to implement a faster CNN using im2col/col2im methods. :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully