Neural Networks made ridiculously simple

Brendan Clarke, NHS Education for Scotland, brendan.clarke2@nhs.scot

09/08/2024

Welcome

  • this session is 🌶 - a beginner-friendly introduction
  • we’ll get going properly at 13.05
  • if you can’t access the chat, you might need to join our Teams channel: tinyurl.com/kindnetwork
  • you can find session materials at tinyurl.com/kindtrp

The KIND network

  • a social learning space for staff working with knowledge, information, and data across health, social care, and housing in Scotland
  • we offer social support, free training, mentoring, community events, …
  • Teams channel / mailing list

KIND training sessions

Session Date Area Level
Hacker Stats (AKA Resampling Methods) 14:00-15:00 Wed 14th August 2024 R 🌶🌶🌶 : advanced-level
Flexdashboard 13:00-14:30 Thu 15th August 2024 R 🌶🌶 : intermediate-level
Excel first steps 09:30-10:30 Tue 3rd September 2024 Excel 🌶 :beginner-level

What’s this session for?

  • neural nets are a core technology for AI/ML systems
  • they’ve been around for decades (and probably will go on for decades)
  • they’re also particularly helpful for health & care folk as a way of understanding AI/ML tools in general

What this session won’t do

  • give a general introduction to AI/ML
  • explain how to build a neural net of your very own
  • discuss in any detail the (often formidable) maths of neural nets

Biology: the neurone

  • biological neurones:
    • receive some input from an upstream neuron(s)
    • process that input in some way
    • generate some output(s) in response to that input, and pass it downstream

Biology: activation

https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Action_potential.svg/778px-Action_potential.svg.png
  • neurones respond to stimulii
    • threshold-y
    • approximately digital output (on/off)
    • sometimes complex behaviour about inputs

Biology: networks of neurones

  • neurones are usually found in networks
  • can produce complex and sophisticated behaviours in a robust way
  • non-obvious relationship between structure and function

Machines: the node

Here’s a simple representation of a node, implemented in code, that we might find in a neural network:

Machines: activation functions

Here are some example input:output pairs for our node:

  • there are lots of possible activation functions
  • a simple one: NOT
    • our node outputs TRUE when we input FALSE, and vice versa

bit of code - nothing fancy!

node <- function(input){
  !input
}

node(TRUE)

[1] FALSE

Machines: networks of nodes

Machines: networks of nodes

  • nodes are usually found in networks
  • can produce complex and sophisticated behaviours in a robust way
    Chat GPT nonsense
  • non-obvious relationship between structure and function in artifical neural networks (ANN)

Several kinds of networks

Why ANNs?

  • ANNs can can potentially replicate any input-output ransformation
  • we do that by a) increasing complexity and b) allowing them to ‘learn’

A more complex feed-forward neural network

Different activation functions

  • binary (true/false)
  • continuous
    • linear
    • non-linear (like sigmoid, ReLU)

Training in neural networks

  • ANNs can be trained
    • take a dataset
    • split it into training and test parts
      • classify (by hand) the training data
    • then train
      • feed your ANN the training data and evaluate how well it performs
      • modify the ANN based on that evaluation
      • repeat until done/bored/perfect
    • finally, test your model with your unlabelled test data and evaluate

MNIST

  • a classic dataset
  • recognizing handwritten numbers = actually-important task
  • 60000 labelled training images
  • 10000 test images
  • each is a 28*28 pixel matrix grey values encoded as 0-255

MNIST data example

V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
0 0 0 0 0 0 0 0 0 0 0
0 0 0 3 18 18 18 126 136 175 26
36 94 154 170 253 253 253 253 253 225 172
253 253 253 253 253 253 253 253 251 93 82
253 253 253 253 253 198 182 247 241 0 0
156 107 253 253 205 11 0 43 154 0 0

Train for MNIST

  • take your training data
  • put together a neural network (number of nodes, layers, feedback, activation functions)
  • run the training data, and evaluate based on labelling
  • modify your neural network, rinse, and repeat
  • when happy, try the unlabelled test data

MNIST examples

MNIST in R

An aside here for the R enthusiasts - we can plot the handwritten numbers back out of the data using ggplot():

mnist_plot_dat <- function(df) {
   # matrix to pivoted tibble for plotting
  df |>
      as_tibble() |>
      mutate(rn = row_number()) |>
      pivot_longer(!rn) |>
      mutate(name = as.numeric(gsub("V", "", name)))
}

mnist_main_plot <- function(df) {
  df |>
    ggplot() +
    geom_tile(aes(
      x = rn,
      y = reorder(name,-name),
      fill = value
    )) +
    scale_fill_gradient2(mid = "white", high = "black")
}

mnist_plot <- function(n){

  mnist_plot_dat(matrix(mnist$train$images[n, ], 28, 28)) |>
    mnist_main_plot() +
      ggtitle(glue("Label: { mnist$train$labels[n]}")) +
      theme_void() +
      theme(legend.position = "none")
    
    # matrix(mnist$train$images[n, ], 28, 28) |>
    #   as_tibble() |>
    #   mutate(rn = row_number()) |>
    #   pivot_longer(!rn) |>
    #   mutate(name = as.numeric(gsub("V", "", name))) |>
    #   ggplot() +
    #   geom_tile(aes(
    #     x = rn,
    #     y = reorder(name,-name),
    #     fill = value
    #   )) +
    #   scale_fill_gradient2(mid = "white", high = "black") +
    #   ggtitle(glue("Label: { mnist$train$labels[n]}")) +
    #   theme_void() +
    #   theme(legend.position = "none")

}

#mnist_plot_dat(matrix(mnist$train$images[1:36, ], 28, 28))

gridExtra::grid.arrange(grobs = map(1:36, mnist_plot), nrow = 6, top="Some MNIST examples")

MNIST in R

Feedback

  • please can I ask for some feedback - takes less than a minute, completely anonymous, helps people like you find the right training for them
Session Date Area Level
Hacker Stats (AKA Resampling Methods) 14:00-15:00 Wed 14th August 2024 R 🌶🌶🌶 : advanced-level
Flexdashboard 13:00-14:30 Thu 15th August 2024 R 🌶🌶 : intermediate-level
Excel first steps 09:30-10:30 Tue 3rd September 2024 Excel 🌶 :beginner-level

Rosenblatt, Frank. 1958. “The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain.” Psychological Review 65 (6): 386.