Top Banner
Neural Networks in R The neuralnet package Fritz G¨ unther 1 / 68
100

Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Feb 16, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks in R

The neuralnet package

Fritz Gunther

1 / 68

Page 2: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

2 / 68

Page 3: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Our neuralnet example will be on colour vision

I Since we want to use neural networks as psychological models,first some repetition on colour vision

3 / 68

Page 4: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Our neuralnet example will be on colour vision

I Since we want to use neural networks as psychological models,first some repetition on colour vision

3 / 68

Page 5: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Light

I The eyes’ receptor cells react towards lightproduced by or reflected from objects

I Light is (in part) an electromagnetic wave

I Visible spectrum: For humans ∼ 360− 750 nm

4 / 68

Page 6: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Light

I The eyes’ receptor cells react towards lightproduced by or reflected from objects

I Light is (in part) an electromagnetic wave

I Visible spectrum: For humans ∼ 360− 750 nm

4 / 68

Page 7: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Light

I The eyes’ receptor cells react towards lightproduced by or reflected from objects

I Light is (in part) an electromagnetic wave

I Visible spectrum: For humans ∼ 360− 750 nm

4 / 68

Page 8: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Light

5 / 68

Page 9: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

6 / 68

Page 10: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

I Two types of photoreceptor cells

- rods- cones

I Only cones enable the differentiation of chromatic light (=colour vision)

7 / 68

Page 11: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

I Two types of photoreceptor cells

- rods- cones

I Only cones enable the differentiation of chromatic light (=colour vision)

7 / 68

Page 12: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

I Two types of photoreceptor cells

- rods- cones

I Only cones enable the differentiation of chromatic light (=colour vision)

7 / 68

Page 13: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

I Cones contain one of three different photopigments

I These react with different intensity towards light of differentwavelengths

8 / 68

Page 14: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The eye

I Cones contain one of three different photopigments

I These react with different intensity towards light of differentwavelengths

8 / 68

Page 15: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

9 / 68

Page 16: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

9 / 68

Page 17: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

9 / 68

Page 18: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

9 / 68

Page 19: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

9 / 68

Page 20: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour vision

I Humans can differentiate between millions of colours

I Three dimensions of colour:

- Hue (wave length)

- Brightness

- Saturation

10 / 68

Page 21: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

11 / 68

Page 22: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Young-Helmholtz-Theory: Tri-chromatic colour vision,depending on three different types of cones (S, M, L)

I Cone types differ in the photopigments they contain

I All cone types react, to some degree, towards all wave lengths

I Colour is therefore coded by the pattern of cone activities

12 / 68

Page 23: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Young-Helmholtz-Theory: Tri-chromatic colour vision,depending on three different types of cones (S, M, L)

I Cone types differ in the photopigments they contain

I All cone types react, to some degree, towards all wave lengths

I Colour is therefore coded by the pattern of cone activities

12 / 68

Page 24: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Young-Helmholtz-Theory: Tri-chromatic colour vision,depending on three different types of cones (S, M, L)

I Cone types differ in the photopigments they contain

I All cone types react, to some degree, towards all wave lengths

I Colour is therefore coded by the pattern of cone activities

12 / 68

Page 25: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Young-Helmholtz-Theory: Tri-chromatic colour vision,depending on three different types of cones (S, M, L)

I Cone types differ in the photopigments they contain

I All cone types react, to some degree, towards all wave lengths

I Colour is therefore coded by the pattern of cone activities

12 / 68

Page 26: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

13 / 68

Page 27: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I What happens if light from different sources overlaps?

I Receptor activity is the sum of activities for the different wavelengths

I If one receptor has a firing rate of 100, this may be caused bythe following input:- Firing rate 100 for wave length A- Firing rate 10 for wave length A and 90 for wave length B- Firing rate 10 for A, 70 for B, 20 for C

I Best example: RGB colours (TV, computer monitors)

14 / 68

Page 28: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I What happens if light from different sources overlaps?

I Receptor activity is the sum of activities for the different wavelengths

I If one receptor has a firing rate of 100, this may be caused bythe following input:- Firing rate 100 for wave length A- Firing rate 10 for wave length A and 90 for wave length B- Firing rate 10 for A, 70 for B, 20 for C

I Best example: RGB colours (TV, computer monitors)

14 / 68

Page 29: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I What happens if light from different sources overlaps?

I Receptor activity is the sum of activities for the different wavelengths

I If one receptor has a firing rate of 100, this may be caused bythe following input:- Firing rate 100 for wave length A- Firing rate 10 for wave length A and 90 for wave length B- Firing rate 10 for A, 70 for B, 20 for C

I Best example: RGB colours (TV, computer monitors)

14 / 68

Page 30: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I What happens if light from different sources overlaps?

I Receptor activity is the sum of activities for the different wavelengths

I If one receptor has a firing rate of 100, this may be caused bythe following input:- Firing rate 100 for wave length A- Firing rate 10 for wave length A and 90 for wave length B- Firing rate 10 for A, 70 for B, 20 for C

I Best example: RGB colours (TV, computer monitors)

14 / 68

Page 31: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Source: www.handprint.com

15 / 68

Page 32: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing: Through overlap of chromatic light

I Chromatic light is characterized by a certain distribution ofwave lengths

I Through mixing, these distributions are added, resulting in anew distribution

Achromatic light (white, black, greyscale): Uniformdistribution

16 / 68

Page 33: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing: Through overlap of chromatic light

I Chromatic light is characterized by a certain distribution ofwave lengths

I Through mixing, these distributions are added, resulting in anew distribution

Achromatic light (white, black, greyscale): Uniformdistribution

16 / 68

Page 34: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing: Through overlap of chromatic light

I Chromatic light is characterized by a certain distribution ofwave lengths

I Through mixing, these distributions are added, resulting in anew distribution

Achromatic light (white, black, greyscale): Uniformdistribution

16 / 68

Page 35: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing: Through overlap of chromatic light

I Chromatic light is characterized by a certain distribution ofwave lengths

I Through mixing, these distributions are added, resulting in anew distribution

Achromatic light (white, black, greyscale): Uniformdistribution

16 / 68

Page 36: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

17 / 68

Page 37: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Source: www.handprint.com

I Colour vision is not a copy of the physical world18 / 68

Page 38: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing is a physiological (and ultimatelypsychological) phenomenon, based on our receptors and theprocessing of firing rates

I (In comparison, substractive colour mixing as done in paintingis a physical phenomenon)

19 / 68

Page 39: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Additive Colour Mixing is a physiological (and ultimatelypsychological) phenomenon, based on our receptors and theprocessing of firing rates

I (In comparison, substractive colour mixing as done in paintingis a physical phenomenon)

19 / 68

Page 40: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Colour Contrast: Areas adjacent to colours appear in theircomplementary colour

I Complementary colours are blue - yellow; red - green (andblack - white)

20 / 68

Page 41: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

I Colour Contrast: Areas adjacent to colours appear in theircomplementary colour

I Complementary colours are blue - yellow; red - green (andblack - white)

20 / 68

Page 42: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

21 / 68

Page 43: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

22 / 68

Page 44: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

23 / 68

Page 45: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Opponent-Process-Theory (Hering; Hurvich & Jameson)

I Theory postulates a layer of neurons in the visual systemreceiving input from the cones

I These code the input in three pairs: red - green; blue - yellow;black - white

I Depending on the input, these neurons fire more (perceptionshifted towards one side of the pair) or less (perception shiftstowards the other side)

I Example:Input shifts red-green to red and blue-yellow to blue=⇒ Perception purple

Input shifts blue-yellow to blue but doesn’t affect red-green=⇒ Perception blue

24 / 68

Page 46: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Opponent-Process-Theory (Hering; Hurvich & Jameson)

I Theory postulates a layer of neurons in the visual systemreceiving input from the cones

I These code the input in three pairs: red - green; blue - yellow;black - white

I Depending on the input, these neurons fire more (perceptionshifted towards one side of the pair) or less (perception shiftstowards the other side)

I Example:Input shifts red-green to red and blue-yellow to blue=⇒ Perception purple

Input shifts blue-yellow to blue but doesn’t affect red-green=⇒ Perception blue

24 / 68

Page 47: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Opponent-Process-Theory (Hering; Hurvich & Jameson)

I Theory postulates a layer of neurons in the visual systemreceiving input from the cones

I These code the input in three pairs: red - green; blue - yellow;black - white

I Depending on the input, these neurons fire more (perceptionshifted towards one side of the pair) or less (perception shiftstowards the other side)

I Example:Input shifts red-green to red and blue-yellow to blue=⇒ Perception purple

Input shifts blue-yellow to blue but doesn’t affect red-green=⇒ Perception blue

24 / 68

Page 48: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

Opponent-Process-Theory (Hering; Hurvich & Jameson)

I Theory postulates a layer of neurons in the visual systemreceiving input from the cones

I These code the input in three pairs: red - green; blue - yellow;black - white

I Depending on the input, these neurons fire more (perceptionshifted towards one side of the pair) or less (perception shiftstowards the other side)

I Example:Input shifts red-green to red and blue-yellow to blue=⇒ Perception purple

Input shifts blue-yellow to blue but doesn’t affect red-green=⇒ Perception blue

24 / 68

Page 49: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Colour Vision

25 / 68

Page 50: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - An overview

26 / 68

Page 51: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

Interviewer:: Why should we hire you?Applicant: I am an expert in machine learning.Interviewer: So you’re good ad maths? What is 16 + 3?Applicant: 4Interviewer: That’s not even close, it’s 19!Applicant: 13Interviewer: No, it’s 19!Applicant: 18Interviewer: No, 19!Applicant: 19Interviewer: You’re hired!

27 / 68

Page 52: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Nice overview about implementing neural networks in R canbe found here:https://selbydavid.com/2018/01/09/neural-network/

28 / 68

Page 53: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Neural Networks are similar to regression models: Predictoutcomes from predictors

I They learn weights linking predictor values to outcome values

29 / 68

Page 54: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Computing the output (forward propagation):

y0 =∑

wi · xi

30 / 68

Page 55: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Computing the output (forward propagation):

y = g(∑

wi · xi )

, where g is the activation function

31 / 68

Page 56: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Neural Networks can also predict multiple outcome valuesfrom a set of predictors

32 / 68

Page 57: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I In that case, we have

yj = g(∑

wij · xi )

33 / 68

Page 58: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Neural networks are typically trained to obtain the weights.Basic training procedure:I Start with random weightsI Take a training itemI Compute output from predictors (forward propagation)I Compute error between predicted output and actual output

(supervised learning)I Adjust the weights according to the error (backpropagation)I Take the next training item and repeat these stepsI Cycle through all training items until weights don’t really

change anymore

34 / 68

Page 59: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - OverviewBackpropagation: The delta rule

I Closely correspondes to learning in the Rescorla-Wagner model

I (1) Compute difference between predicted and actual output

E =1

2(tj − yj)

2

To compute a weight change value from the error, the derivative ofthe error function will enter the formula:

E ′ = (tj − yj)

35 / 68

Page 60: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - OverviewBackpropagation: The delta rule

I (2) Adjust (multiply) by learning rate

α(tj − yj)

36 / 68

Page 61: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - OverviewBackpropagation: The delta rule

I (3) Change in weight linking input xi to yj is this productmultiplied by input activation

∆wij = α(tj − yj) · xi

I This is the core delta rule for linear activation functions

37 / 68

Page 62: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - OverviewBackpropagation: The delta rule

I (4) In the general case for any activation function, itsderivation is applied to the weighted input and included

∆wij = α(tj − yj)g′(hj)xi

With hj =∑

wijxi and yj = g(hj)

38 / 68

Page 63: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - OverviewBackpropagation: The delta rule

I Training continues until the changes in weights ∆wij nolonger exceed a threshold value t. Every training cylce uses alltraining items.

39 / 68

Page 64: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Hidden layers are intermediate levels between input and outputI Typically, they take input from all nodes in the previous layer,

and give output to all nodes in the next layerI In this case, it’s easiest to consider them as multiple, chained

neural networks where the output of layer n serves as input forlayer n + 1

40 / 68

Page 65: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

I Hidden layers allow the network to deal with non-linearities

I Example: Predict color from x and y coordinates

without hidden layer with hidden layer

41 / 68

Page 66: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

Neural Networks - Overview

Play around with neural networks:

https://playground.tensorflow.org/

42 / 68

Page 67: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

43 / 68

Page 68: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Article describing the neuralnet package and its background:

Gunther, F., & Fritsch, S. (2010). neuralnet: Training ofneural networks. The R journal, 2(1), 30-38.

(The author is Frauke Gunther, not me)

44 / 68

Page 69: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Install the neuralnet package withinstall.packages("neuralnet")

I Load the package withlibrary(neuralnet)

45 / 68

Page 70: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Install the neuralnet package withinstall.packages("neuralnet")

I Load the package withlibrary(neuralnet)

45 / 68

Page 71: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Load the colors.txt data set usingsetwd("PATH TO DATA")

dat <- read.table("colors.txt")

(or specify the path directly in the read.table command)

46 / 68

Page 72: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I The main function in the neuralnet package is theneuralnet function

I This function trains a neural network from input data

I User defines network structure

47 / 68

Page 73: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I The main function in the neuralnet package is theneuralnet function

I This function trains a neural network from input data

I User defines network structure

47 / 68

Page 74: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I The main function in the neuralnet package is theneuralnet function

I This function trains a neural network from input data

I User defines network structure

47 / 68

Page 75: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Usage:

neuralnet(formula, data, hidden = 1, threshold =

0.01, stepmax = 1e+05, rep = 1, startweights =

NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus =

1.2), learningrate=NULL, lifesign = "none",

lifesign.step = 1000, algorithm = "rprop+",

err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL,

constant.weights = NULL, likelihood = FALSE)

48 / 68

Page 76: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

formula A formula specifying the input and outputvariables

As in all other models in R (such as lm() or aov()):

out1 + out2 ∼ var1 + var2 + var3

49 / 68

Page 77: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

data The data frame containing the input andoutput variables

50 / 68

Page 78: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

hidden A vector specifying the hidden layer struc-ture

hidden=0 No hidden layerhidden=c(4,5) Two hidden layers: First layer with 4 nodes,

second layer with 5 nodes

51 / 68

Page 79: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

threshold Specifies the threshold for weight adjust-ments (training is considered as convergingif there are no more weight changes abovethe threshold)

52 / 68

Page 80: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

stepmax Maximum number of training steps(One training step = one iteration over thewhole data set)

53 / 68

Page 81: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

rep Number of repetitions (i.e. how often thecomplete training algorithm is executed)

54 / 68

Page 82: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

lifesign Observe training progress withlifesign="full"

55 / 68

Page 83: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

algorithm The learning algorithm (several included, seethe help-function). Standard backpropaga-tion backprop requires a learningrate

56 / 68

Page 84: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

err.fct The error function, computing the differ-ence between network-predicted and ob-served outcome. Sum of squared errors andcross-entropy are included, other (differen-tiable) functions can be provided

57 / 68

Page 85: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

I Important Arguments:

neuralnet(formula, data, hidden = 1, threshold = 0.01, stepmax =

1e+05, rep = 1, startweights = NULL, learningrate.limit = NULL,

learningrate.factor = list(minus = 0.5, plus = 1.2),

learningrate=NULL, lifesign = "none", lifesign.step = 1000,

algorithm = "rprop+", err.fct = "sse", act.fct = "logistic",

linear.output = TRUE, exclude = NULL, constant.weights = NULL,

likelihood = FALSE)

act.fct Activation function computing the outputvalue from the input values

58 / 68

Page 86: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Task: Train a single-layer (i.e., no hidden layers) network to predictthe colour labels from the RGB code

Note: This is a physiological/psychological model, since additivecolour mixing is not a physical phenomenon!

59 / 68

Page 87: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Inspecting the neural network

I Generic R functionssummary(network)

str(network)

60 / 68

Page 88: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Inspecting the neural network

I Generic R functionssummary(network)

str(network)

60 / 68

Page 89: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

An nn object contains the following elements (along with the inputdata):

net.results The network’s predicted output for the train-ing data

weights The trained network weightsresult.matrix Several indices summarizing the model (AIC,

BIC, number of steps, reached threshold, er-ror)

61 / 68

Page 90: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

An nn object contains the following elements (along with the inputdata):

net.results The network’s predicted output for the train-ing data

weights The trained network weightsresult.matrix Several indices summarizing the model (AIC,

BIC, number of steps, reached threshold, er-ror)

61 / 68

Page 91: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Inspecting the neural network

I The plot.nn function:plot.nn(network)

62 / 68

Page 92: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Inspecting the neural network

I The plot.nn function:plot.nn(network)

62 / 68

Page 93: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Predict output for given input

I The predict function:predict(network,testset)

I The testset needs to have the same input variables asspecified for the network!

63 / 68

Page 94: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Predict output for given input

I The predict function:predict(network,testset)

I The testset needs to have the same input variables asspecified for the network!

63 / 68

Page 95: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Predict output for given input

I The predict function:predict(network,testset)

I The testset needs to have the same input variables asspecified for the network!

63 / 68

Page 96: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

What do we learn from our single-layer network?Does it make sense?

64 / 68

Page 97: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Task: Train a network with one hidden layer (three nodes) topredict the colour labels from the RGB code

(Why?)

65 / 68

Page 98: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Is the hidden-layer network better than the single-layer network?

Does it work as expected?

66 / 68

Page 99: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

In order to create the colors.txt data set, I just assigned colourlabels on an intuitive basis

What happens when we apply a more “theory-conform” labellingsystem (including white and black)?

67 / 68

Page 100: Neural Networks in R - uni-tuebingen.de · Light I The eyes’ receptor cells react towards light produced by or re ected from objects I Light is (in part) an electromagnetic wave

The neuralnet package

Does it help to include this rectified sum as an additional one-nodehidden layer?

68 / 68