Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays...

41
13-Nov-2019 Using R to build artificial neural networks in medical data Thomas Wollseifen

Transcript of Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays...

Page 1: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

13-Nov-2019

Using R to build artificial neural networks in medical data

Thomas Wollseifen

Page 2: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

2© 2019 All rights reserved | Confidential | For Syneos Health® use only

Agenda

1. Introduction to Artificial Neural Networks (ANN) 2. ANNs with R - neuralnet3. Classifying Diabetes with ANNs4. TensorFlow and Convolutional Neural Networks (CNN) with R - keras5. Summary

Page 3: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

3© 2019 All rights reserved | Confidential | For Syneos Health® use only

Introduction to Artificial Neural Networks

~10$$ neuronsin human brain

~10% connections per neuron~10$& connections

Page 4: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

4© 2019 All rights reserved | Confidential | For Syneos Health® use only

Neuron - Signal flow from dendrites to axon ends

Nucleus

DendritesStimulus

Axon hillock

Cellbody

Presynapticcell

Signaldirection

Axon

Synapse

Neurotransmitter

Synaptic terminals

Postsynaptic cell

Synapticterminals

Page 5: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

5© 2019 All rights reserved | Confidential | For Syneos Health® use only

Artificial Neural Network – 1 hidden layer

Input Hidden Layer Output

X1

X2

B1 B2

Y

weights

weights

Bias weight

Bias weights

Page 6: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

6© 2019 All rights reserved | Confidential | For Syneos Health® use only

Artificial Neural Network - multilayers

Inputs Hidden Layers Outputs

X1

X2

B1 B3

Bn

B2

⋮Xj ⋮

Page 7: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

7© 2019 All rights reserved | Confidential | For Syneos Health® use only

Model of a neuron

Dendrites Soma (cell body) Axons

Page 8: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

8© 2019 All rights reserved | Confidential | For Syneos Health® use only

Activation functions

Page 9: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

9© 2019 All rights reserved | Confidential | For Syneos Health® use only

Feed-Forward Pass / Back-Propagation Algorithm

Input Hidden Layer Output

X1

X2

B1 B2

Y

weights

weights

12+,-.

/

(𝑦 − 𝑤,𝑥,)6

+𝐸𝑟𝑟𝑜𝑟 𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑖𝑜𝑛

2. Back-Propagation àAdjust weights

1. Forward-Pass

Result

Page 10: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

10© 2019 All rights reserved | Confidential | For Syneos Health® use only

Artificial Neural Networks with R

Package neuralnet

Page 11: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

11© 2019 All rights reserved | Confidential | For Syneos Health® use only

Example 1 – Square Root Function

Page 12: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

12© 2019 All rights reserved | Confidential | For Syneos Health® use only

Build the ANN with Training Data

traininginput <- as.data.frame(runif(50, min=0, max=200))

trainingoutput <- sqrt(traininginput)

net.sqrt <-neuralnet(Output~Input,trainingdata,hidden=10,threshold=0.01)

plot(net.sqrt)

Page 13: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

13© 2019 All rights reserved | Confidential | For Syneos Health® use only

Test the ANN

testdata <- as.data.frame((0:20)^2)net.results <- predict(net.sqrt, testdata)results<-as.data.frame(net.results)

Input Square root expected

Neural net output predicted

0 0 0.7143873

1 1 1.0243201

4 2 2.0041862

9 3 3.0001876

16 4 4.0006444

25 5 4.9958750

36 6 6.0030675

49 7 7.0034939

64 8 7.9974201

81 9 9.0019381

Page 14: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

14© 2019 All rights reserved | Confidential | For Syneos Health® use only

Classifying Diabetes with ANNs

Global diabetes prevalence of ~8%

Page 15: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

15© 2019 All rights reserved | Confidential | For Syneos Health® use only

Diabetes classification – Pima Indians

Data from 768 women

• Pregnancy• Glucose• Diastolic blood pressure• Skin thickness• Insulin • BMI• Diabetes pedigree function• Age• Outcome variable – Diabetes

Inpu

tva

riabl

es

Page 16: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

16© 2019 All rights reserved | Confidential | For Syneos Health® use only

Diabetes classification – correlation matrix

M <- cor(diabetes.df[,1:9])corrplot(M, method="color", tl.col = "indianred4",

number.digits = 1, addCoef.col = "white", type="lower")

Page 17: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

17© 2019 All rights reserved | Confidential | For Syneos Health® use only

Normalize data in range 0 to 1

normalize <- function(x) {return ((x - min(x)) / (max(x) - min(x)))

}diabetes.df_norm <- as.data.frame(lapply(diabetes.df, normalize))

Page 18: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

18© 2019 All rights reserved | Confidential | For Syneos Health® use only

Training / Test data

index <- sample(1:nrow(diabetes.df_norm),round(0.90*nrow(diabetes.df_norm)))trainset <- diabetes.df_norm[index,] # 90% training datatestset <- diabetes.df_norm[-index,] # 10% test data

Page 19: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

19© 2019 All rights reserved | Confidential | For Syneos Health® use only

ANN within R neuralnet

nn <- neuralnet(Outcome ~ Pregnancies+Glucose+BloodPressure+SkinThickness+Insulin+BMI+DiabetesPedigreeFunction+Age,

data=trainset, hidden=c(6), linear.output=FALSE, threshold=0.01, lifesign='full')

Page 20: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

20© 2019 All rights reserved | Confidential | For Syneos Health® use only

Confusion Matrix

PredictionNo Diabetes Diabetes

Actu

al No Diabetes 46 9Diabetes 5 17

False/Positive rate = %BC$D%BC$DC&CE

= 0.82MSE = 0.17

nn.results <- compute(nn, testset)results <- data.frame(actual = testset$Outcome,

prediction = nn.results$net.result)

Page 21: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

21© 2019 All rights reserved | Confidential | For Syneos Health® use only

TensorFlow with R

Page 22: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

22© 2019 All rights reserved | Confidential | For Syneos Health® use only

TensorFlow

• TensorFlow displays mathematical operations in the form of a graph• The graph represents the sequential flow of all operations to be performed byTensorFlow

• Speech recognition• Google Photos• Developed by Google, now public domain

à “A general purpose numerical computing library“

Page 23: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

23© 2019 All rights reserved | Confidential | For Syneos Health® use only

Classification of breast cancer with TensorFlow

Image: Phys — SPIE: International Society for Optics and Photonics

• Dataset with 569 records and 31 features

Benign Malignant

Page 24: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

24© 2019 All rights reserved | Confidential | For Syneos Health® use only

A look at the data

M <- cor(df)corrplot(M, method="color", number.digits = 1, addCoef.col = "white", number.cex= 0.5, tl.cex = 0.7, type = "lower")

Page 25: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

25© 2019 All rights reserved | Confidential | For Syneos Health® use only

R package Keras

model <- keras_model_sequential()model %>% layer_dense(units = 256,activation="relu",input_shape = ncol(X_train)) %>% layer_dropout(rate = 0.4) %>% layer_dense(units = 75, activation = "relu") %>% layer_dropout(rate = 0.3) %>% layer_dense(units = 2, activation = "sigmoid")

model %>% compile( optimizer = 'adam',loss =sparse_categorical_crossentropy’,metrics = c('accuracy’))

model %>% fit(X_train,y_train,epochs=12,batch_size=5,validation_split=0.2)

Page 26: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

26© 2019 All rights reserved | Confidential | For Syneos Health® use only

Training the ANN with TensorFlow

model %>% fit(X_train,y_train,epochs=12,batch_size=5,validation_split=0.2)

Page 27: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

27© 2019 All rights reserved | Confidential | For Syneos Health® use only

Prediction with TensorFlow

predictions <- model %>% predict_classes(X_test)

df.pred <- data.frame(predictions)

Confusion Matrix

False/positive rate = 97%

Predictionbenign malignant

Actu

al benign 103 2malignant 3 62

Page 28: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

28© 2019 All rights reserved | Confidential | For Syneos Health® use only

Convolutional Neural Networks

Object detection and identificationSpeech and natural language processing

Page 29: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

29© 2019 All rights reserved | Confidential | For Syneos Health® use only

Convolutional Neural Networks (CNN)

• Maintain spatial integrity of input images à 2d or 3d matrix as input• Feature extraction through convolutional filters• Dimensionality reduction via pooling• Fully connetected ANN for categorization

Page 30: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

30© 2019 All rights reserved | Confidential | For Syneos Health® use only

Maintaining of spatial integrity

• Conventional ANN would converted to a 1D list of pixel values• Loss of spatial integrity (e.g. shadows or edges)

Page 31: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

31© 2019 All rights reserved | Confidential | For Syneos Health® use only

Convolutional filter

Convolutional filter with weights

Page 32: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

32© 2019 All rights reserved | Confidential | For Syneos Health® use only

Pooling

Pooling reduces the feature map size without loss of information

Page 33: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

33© 2019 All rights reserved | Confidential | For Syneos Health® use only

Convolutional Neural Networks (CNN)

Feature extraction Classification

Page 34: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

34© 2019 All rights reserved | Confidential | For Syneos Health® use only

„Hello machine learning world“

Page 35: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

35© 2019 All rights reserved | Confidential | For Syneos Health® use only

Fashion MNIST1 dataset – Object detection

70,000 grayscale images (28 × 28 pixels) in 10 categories.

T-shirt/top 0Trouser 1Pullover 2Dress 3Coat 4Sandal 5Shirt 6Sneaker 7Bag 8Ankle boot 9

1MNIST - Modified National Institute of Standards and Technology database

Page 36: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

36© 2019 All rights reserved | Confidential | For Syneos Health® use only

Prepare Fashion MNIST dataset

library(keras)

Get datasetfashion_mnist <- dataset_fashion_mnist()

Training / Test datac(train_images, train_labels) %<-% fashion_mnist$trainc(test_images, test_labels) %<-% fashion_mnist$test

scale pixel values to range of 0 to 1 train_images <- train_images / 255test_images <- test_images / 255

0

10

20

0 10 20

0.00

0.25

0.50

0.75

1.00value

Page 37: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

37© 2019 All rights reserved | Confidential | For Syneos Health® use only

Building the CNN

#Build modelmodel <- keras_model_sequential()model %>%

layer_reshape(c(28,28,1)) %>%layer_conv_2d(input_shape=c(28,28,1), filter = 32, kernel_size = c(3,3), activation='relu') %>%

layer_max_pooling_2d(pool_size=c(2,2)) %>%layer_flatten() %>%layer_dense(units = 100, activation = 'relu') %>%layer_dense(units = 10, activation = 'softmax')

#Compile Modelmodel %>% compile(optimizer = 'adam',

loss = 'sparse_categorical_crossentropy',

metrics = c('accuracy‘))

#Start Training processmodel %>% fit(train_images, train_labels, epochs = 8)

Page 38: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

38© 2019 All rights reserved | Confidential | For Syneos Health® use only

Training process in R

Accurracy of >90%

model %>% fit(train_images, train_labels, epochs = 8)

Page 39: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

39© 2019 All rights reserved | Confidential | For Syneos Health® use only

• Introduction into ANNs• Artificial Neural Networks with R‘s neuralnet• Diabetes classification with simple ANNs• Breast cancer classification with TensorFlow• Convolutional Neural Networks with R‘s keras

Summary

Page 40: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

40© 2019 All rights reserved | Confidential | For Syneos Health® use only

Thank youfor your attention!

Questions to: Thomas WollseifenSyneos Health Germany GmbHEmail: [email protected]

Page 41: Using R to build artificial neural networks in medical data · TensorFlow • TensorFlow displays mathematical operations in the form of a graph • The graph represents the sequential

Shortening the DistanceFrom Lab to Life®.