---
date-modified: "`r Sys.Date()`"
date: 20/Nov/2024
title: "Convolutional Neural Nets"
order: 185
summary: 
tags:
- Neural Nets
- Convolutional Neural Nets

---


```{r setup, include=FALSE}
library(blogdown)
library(tidyverse)
library(ggformula)
library(mosaicCalc) # Analytic Calculus
library(ambient) # Noise generation in R
library(plot3D) # 3D plots for explanation
library(caracas)
library(downloadthis)
library(knitr)
library(kableExtra)
## Markdown boiler plate stuff!!
# ![An Elephant](elephant.png){#fig-elephant}
# This is illustrated well by @fig-elephant.
# 
# ### Figure Panel Divs
#     ::: {#fig-elephants layout-ncol=2}
#     ![Surus](surus.png){#fig-surus}
#     ![Hanno](hanno.png){#fig-hanno}
#      Famous Elephants
#     :::
#     Adding download buttons
#     data that has been read in
#     {{< downloadthis ../../../../materials/Data/housing_train.csv dname="house_prices" label="Download the House Prices Dataset" icon="database-fill-down" type="info" >}}
#    existing file
#    {{< downloadthis Orange/grouped-summaries.ows dname="grouped_summaries" label="Download the Orange Workflow" icon="database-fill-down" type="info" >}} 

```

```{r}
#| label: Extra Pedagogical Packages
#| echo: false
#| message: false

library(checkdown)
library(epoxy)
library(grateful)
library(MKdescr)
library(shinylive) # To create a Shiny app in a Quarto HTML doc
# Will not work if webr is also used in the SAME Quarto doc!
library(sysfonts)
library(gfonts)
library(kableExtra)
# library(conflicted)
# conflicted::conflicts_prefer(dplyr::filter, dplyr::count, dplyr::last, dplyr::glimpse, base::max)
library(downloadthis)
#devtools::install_github("mccarthy-m-g/embedr")
library(embedr) # Embed multimedia in HTML files
```

```{r}
#| label: Plot Sizing and theming
#| echo: false
#| message: false
#| results: hide

# https://stackoverflow.com/questions/74491138/ggplot-custom-fonts-not-working-in-quarto

# Chunk options
knitr::opts_chunk$set(
 fig.width = 7,
 fig.asp = 0.618, # Golden Ratio
 #out.width = "80%",
 fig.align = "center"
)
### Ggplot Theme
### https://rpubs.com/mclaire19/ggplot2-custom-themes

theme_custom <- function(){ 
    font <- "Roboto Condensed"   #assign font family up front
    
    theme_classic(base_size = 14) %+replace%    #replace elements we want to change
    
    theme(
      panel.grid.minor = element_blank(),    #strip minor gridlines
      text = element_text(family = font),
      #text elements
      plot.title = element_text(             #title
                   family = font,            #set font family
                   #size = 20,               #set font size
                   face = 'bold',            #bold typeface
                   hjust = 0,                #left align
                   #vjust = 2                #raise slightly
                   margin=margin(0,0,10,0)
),               
      
      plot.subtitle = element_text(          #subtitle
                   family = font,            #font family
                   #size = 14,                #font size
                   hjust = 0,
                   margin=margin(2,0,5,0)
),               
      
      plot.caption = element_text(           #caption
                   family = font,            #font family
                   size = 8,                 #font size
                   hjust = 1),               #right align
      
      axis.title = element_text(             #axis titles
                   family = font,            #font family
                   size = 10                 #font size
),
      
      axis.text = element_text(              #axis text
                   family = font,            #axis family
                   size = 8)               #font size
    )
}

# Set graph theme
theme_set(new = theme_custom())
#
```


```{r,echo = FALSE, eval = FALSE, fig.alt="Petr Slováček on Unsplash", fig.align='center'}
knitr::include_graphics("featured.jpg")

```

## {{< iconify icons8 idea >}} Inspiration


## Convolutional Neural Networks

#### What is Convolution?

Consider that you life in a high rise apartment complex. Have you heard an ambulance go by? How does the sound of the siren change as the ambulance approaches towards your dwelling and then goes past it to get lost amidst the surrounding buildings again?

The siren's **emitted** sound is always the **same**. It is the local surroundings and the *geometry of the echoes* that brings the **same** sound to your ears again and again, but in altered form. The sound from the ambulance goes all around, hits on or other of the buildings, reflects, and comes back to your ears **after a delay** and **weighted** by the *strength* of the echo geometry.

You might consider that a CNN has several such *echo mechanisms* operating. Each pixel value (if you are dealing with image input) goes through a series of such *delayed weightings* which multiply the pixel input. The output of each pixel contributes in such fashion to the *activation* of that unit/layer of the CNN. 


{{< video https://youtu.be/vJiZqZRkIg8 >}}


#### How does a CNN Structure use "Convolution"?

Let us contemplate the structure of a CNN. 


## Videos

{{< video https://youtu.be/qPKsVAI_W6M >}}





## Convolutional Neural Nets in Code

::: {.panel-tabset .nav-pills style="background: whitesmoke;"}

### Using p5.js


### Using R
Using `torch`.

:::


## {{< iconify mingcute thought-line >}} Wait, But Why?


## {{< iconify ooui references-ltr >}} References

1. *Digit Recognition with CNNs*. Interactive! <https://transcranial.github.io/keras-js/#/mnist-cnn>
1. *CNN Convoluter*. <https://pwwang.github.io/cnn-convoluter/>
1. *CNN Explainer: Learn Convolutional Neural Network (CNN) in your browser!*. <https://poloclub.github.io/cnn-explainer/>
1. Deep Lizard. *Understanding Convolution Operations in Neural Networks*. <https://deeplizard.com/resource/pavq7noze2>
1. Colah's Blog.(Apr 6, 2014). *Neural Networks, Manifolds, and Topology*. <https://colah.github.io/posts/2014-03-NN-Manifolds-Topology/>. Very simple and readable article. 
1. Andrej Karpathy. *ConvNetJS:Deep Learning in your browser*.<https://cs.stanford.edu/people/karpathy/convnetjs/>
1. https://developers.google.com/machine-learning.<https://developers.google.com/machine-learning>
1. Adit Deshpande. *A Beginner's Guide to CNNs*. <https://adeshpande3.github.io/A-Beginner%27s-Guide-To-Understanding-Convolutional-Neural-Networks/>
1. The Neural Network Zoo - The Asimov Institute. <http://www.asimovinstitute.org/neural-network-zoo/>
1. *It’s just a linear model:  neural  networks edition*. <https://lucy.shinyapps.io/neural-net-linear/>
1. Machine Learning Tokyo: Interactive Tools for ML/DL, and Math. <https://github.com/Machine-Learning-Tokyo/Interactive_Tool>
1. *Anyone Can Learn AI Using This Blog*. <https://colab.research.google.com/drive/1g5fj7W6QMER4-03jtou7k1t7zMVE9TVt#scrollTo=V8Vq_6Q3zivl>
1. Practical Deep Learning for Coders: An Online Free Course.<https://course.fast.ai>
1. Neural Networks Visual with [vcubingx](https://youtube.com/@vcubingx?feature=shared)
  - Part 1. <https://youtu.be/UOvPeC8WOt8>
  - Part 2. <https://www.youtube.com/watch?v=-at7SLoVK_I>

1. The Neural Network Zoo - The Asimov Institute. <http://www.asimovinstitute.org/neural-network-zoo/>
1. It’s just a linear model:  neural  networks edition. <https://lucy.shinyapps.io/neural-net-linear/>
1. Neural Network Playground. <https://playground.tensorflow.org/>
1. Rohit Patel (20 Oct 2024). *Understanding LLMs from Scratch Using Middle School Math: A self-contained, full explanation to inner workings of an LLM*. <https://towardsdatascience.com/understanding-llms-from-scratch-using-middle-school-math-e602d27ec876>
1. Machine Learning Tokyo: Interactive Tools for ML/DL, and Math. <https://github.com/Machine-Learning-Tokyo/Interactive_Tool>
1. *Anyone Can Learn AI Using This Blog*. <https://colab.research.google.com/drive/1g5fj7W6QMER4-03jtou7k1t7zMVE9TVt#scrollTo=V8Vq_6Q3zivl>
1. Practical Deep Learning for Coders: An Online Free Course.<https://course.fast.ai>


#### Text Books
1. Michael Nielsen. *Neural Networks and Deep Learning*, a free online book. <https://neuralnetworksanddeeplearning.com/index.html>
1. Simone Scardapane. (2024) *Alice’s Adventures in a differentiable Wonderland*. https://www.sscardapane.it/alice-book/
1.  Parr and Howard (2018). *The Matrix Calculus You Need for Deep Learning*.<https://arxiv.org/abs/1802.01528>
1. The Little Book of Deep Learning. [Available Online](https://fleuret.org/francois/lbdl.html)
1. Parr and Howard (2018). *The Matrix Calculus You Need for Deep Learning*.<https://arxiv.org/abs/1802.01528>
1. Zhang, Lipton, Li, Smola. *Dive into Deep Learning*. <https://www.d2l.ai/> 


#### LLMs
1. Brendan Bycroft.*Visualizing LLMs*. <https://bbycroft.net/llm>
1. Rohit Patel (20 Oct 2024). *Understanding LLMs from Scratch Using Middle School Math: A self-contained, full explanation to inner workings of an LLM*. <https://towardsdatascience.com/understanding-llms-from-scratch-using-middle-school-math-e602d27ec876>
1. AI-powered reporting and annotation for radiology. <https://www.md.ai>
1. Carl T. Bergstrom and Jevin D. West. *Modern-Day Oracles or Bullshit Machines?* <https://thebullshitmachines.com/index.html>

#### Using R for DL

1. Geeks for Geeks. *Convolutional Neural Nets in R*. <https://www.geeksforgeeks.org/convolutional-neural-networks-cnns-in-r/>
1. David Selby (9 January 2018). Tea and Stats Blog. *Building a neural network from scratch in R*. <https://selbydavid.com/2018/01/09/neural-network/>
1. *torch for R: An open source machine learning framework based on PyTorch.* <https://torch.mlverse.org>
1. Akshaj Verma. (2020-07-24). *Building A Neural Net from Scratch Using R - Part 1 and Part 2*.
<https://rviews.rstudio.com/2020/07/20/shallow-neural-net-from-scratch-using-r-part-1/> and 
<https://rviews.rstudio.com/2020/07/24/building-a-neural-net-from-scratch-using-r-part-2/>
1. Ander Fernandez Jauregui. <https://anderfernandez.com/en/blog/how-to-create-neural-networks-with-torch-in-r/>
1. <https://f0nzie.github.io/rtorch-minimal-book/>
1.  *`torch` for R: An open source machine learning framework based on `PyTorch`*. <https://torch.mlverse.org/>


::: {#refs style="font-size: 60%;"}
###### {{< iconify lucide package-check >}} R Package Citations

```{r}
#| echo: false
#scan_packages()
cite_packages(
  output = "table",
  out.dir = ".",
  out.format = "html",
  pkgs = c("keras", "tensorflow", "safetensors")
) %>%
  knitr::kable(format = "simple")

```
:::


