├── .gitignore ├── README.md ├── data-raw ├── BreastCancerRaw.rds ├── README.md └── preproc-walking.R ├── data ├── bcX.rds ├── bcY.rds ├── walking.rds ├── xIris.rds ├── xWalk.rds ├── yIris.rds └── yWalk.rds ├── keras-workshop.Rproj ├── material ├── Deep Learning with Keras Workshop - EARL 2019.pdf ├── README.md ├── keras-workshop.pdf └── save-perm.png └── scripts ├── 02 Getting Started.R ├── 03 Spatial.R ├── breast_cancer.R ├── iris.R └── walking_final.R /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | r-reticulate/ 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Keras for R Workshop 2 | 3 | ## Environment 4 | 5 | You can either use the RStudio Cloud project or use your laptop. It can be a mixed experience configuring everything so the cloud is a safe fallback. 6 | 7 | ### RStudio Cloud 8 | 9 | An environment with this repo checked out is available at: https://rstudio.cloud/project/489173 10 | 11 | You will need to setup and account on RStudio Cloud. Afterwards you should be able to deploy the project (this can take a minute or two). Then create a copy in your own space: 12 | 13 | ![save permanent copy](material/save-perm.png) 14 | 15 | Test it's working with: 16 | 17 | ```r 18 | library(keras) 19 | is_keras_available() 20 | [1] TRUE 21 | ``` 22 | 23 | This takes a minute the first time and will be quick from then on. 24 | 25 | ### Local (laptop) 26 | 27 | All of the models we're building will work on a laptop so if you want to follow along on your own machine then please follow the steps below: 28 | 29 | * Install R from https://cran.r-project.org/ 30 | * RStudio desktop from https://www.rstudio.com 31 | * Install Anaconda from https://www.anaconda.com/download/ 32 | 33 | Install the following R packages from CRAN in the usual way: 34 | 35 | ```r 36 | install.packages(c("tidyverse", "rsample", "recipes", "keras")) 37 | ``` 38 | 39 | In an R session install the keras/tensorflow python libraries by running: 40 | 41 | ```r 42 | library(keras) 43 | install_keras() 44 | ``` 45 | 46 | This takes a while as it will install the various python packages that are required. For further instructions please see https://keras.rstudio.com/ and follow the instructions there. 47 | 48 | If it worked you should get: 49 | 50 | ```r 51 | library(keras) 52 | is_keras_available() 53 | [1] TRUE 54 | ``` 55 | -------------------------------------------------------------------------------- /data-raw/BreastCancerRaw.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data-raw/BreastCancerRaw.rds -------------------------------------------------------------------------------- /data-raw/README.md: -------------------------------------------------------------------------------- 1 | # Raw Data 2 | 3 | Download the zip from [UCI Activity Recognition from Single Chest Mounted Accelerometer](https://archive.ics.uci.edu/ml/datasets/Activity+Recognition+from+Single+Chest-Mounted+Accelerometer) 4 | 5 | Or [direct link to zip file](https://archive.ics.uci.edu/ml/machine-learning-databases/00287/Activity%20Recognition%20from%20Single%20Chest-Mounted%20Accelerometer.zip) 6 | 7 | Extract the contents into `data-raw`. The manifest is: 8 | 9 | 1.csv 10 | 10.csv 11 | 11.csv 12 | 12.csv 13 | 13.csv 14 | 14.csv 15 | 15.csv 16 | 2.csv 17 | 3.csv 18 | 4.csv 19 | 5.csv 20 | 6.csv 21 | 7.csv 22 | 8.csv 23 | 9.csv 24 | README 25 | 26 | This should be directly in data-raw 27 | -------------------------------------------------------------------------------- /data-raw/preproc-walking.R: -------------------------------------------------------------------------------- 1 | # This is the pre-processing we did to build the walking data file. 2 | # Download the raw data from: 3 | # https://archive.ics.uci.edu/ml/datasets/Dataset+for+ADL+Recognition+with+Wrist-worn+Accelerometer 4 | # Or direct from: 5 | # https://archive.ics.uci.edu/ml/machine-learning-databases/00283/ADL_Dataset.zip 6 | # 7 | # Extract to a data-raw directory. 8 | # 9 | 10 | # script to preprocess the accelerometer data 11 | 12 | library(tidyverse) 13 | 14 | # Initialise array for data: 15 | # Rows will be observations 16 | # Columns will be: 17 | # * Time point (sequential count integer) 18 | # * x-, y-, z-directional accelerometer data time series (integer) 19 | # * Activity label (1-7) 20 | # * Person label (0-14) 21 | 22 | data_files <- list.files("data-raw", pattern = "*.csv", full.names = TRUE) 23 | 24 | dataset <- data.frame() 25 | 26 | # Add data from each file in turn 27 | for (k in seq_along(data_files)) { 28 | 29 | #cat("Reading file", k, "/", length(data_files), "\n") 30 | 31 | d <- read_csv(data_files[k], col_names = c("obs", "acc_x", "acc_y", "acc_z", "activity"), col_types = "ddddd") 32 | 33 | # Add a column with a label representing the person 34 | d$person <- k 35 | 36 | dataset <- bind_rows(dataset, d) 37 | } 38 | 39 | head(dataset) 40 | 41 | # Reshape data into 3 dimensions: 42 | # 0-dimension ("rows") is observations (1926896 in total) 43 | # 1-dimension ("columns") is time series values (260 = 5{seconds}*52{Hz} in total) 44 | # 2-dimension ("leaves") are as follows (5 in total): 45 | # * 3 directions (x-, y-, z-acceleration) 46 | # * Activity type labels 47 | # * Person labels 48 | 49 | # Chop the time series into 260-length (5 second) sections every 52 points (every 1 second) 50 | m <- (nrow(dataset) - 208) %/% 52 51 | reshaped_data <- array(0, dim = c(m, 260, 5)) 52 | 53 | for (k in seq_len(m)) { 54 | 55 | start <- 52*(k-1) + 1 56 | stop <- start + 259 57 | 58 | # If the count column's value at "stop" is smaller than at "start", we've changed person because the # counts start again from 1, so discard. 59 | # If the activity label column is not all the same, we have more than one activity in that section, # so discard. 60 | 61 | if (dataset[stop, "obs"] < dataset[start, "obs"] || 62 | !all(dataset[start:stop, "activity"] == dataset[start, "activity"])) { 63 | next 64 | } 65 | 66 | # Else copy all but count column to the new data block 67 | reshaped_data[k, , ] <- as.matrix(dataset[start:stop, -1]) 68 | } 69 | 70 | # Remove the extra rows, which will have person label 0 71 | reshaped_data <- reshaped_data[(reshaped_data[, 1, 5] != 0), , ] 72 | 73 | # Select only observations that correspond to "walking", which is activity label 4. 74 | walking <- reshaped_data[(reshaped_data[, 1, 4] == 4), , ] 75 | 76 | walking_x <- walking[,,1:3] %>% 77 | apply(c(1, 3), scale) %>% # scaling within each series so no issue with train/test 78 | aperm(c(2,1,3)) # undo apply's ridiculous transpose 79 | 80 | dimnames(walking_x) <- list(NULL, NULL, c("acc_x", "acc_y", "acc_z")) 81 | 82 | walking_labels <- walking[,1,5] %>% 83 | as.integer() 84 | 85 | walkingData <- list(x = walking_x, 86 | labels = walking_labels) 87 | 88 | saveRDS(walkingData, "walking.rds") 89 | 90 | # And for people that fall behind ----- 91 | 92 | set.seed(19) 93 | m <- nrow(walking_x) 94 | 95 | # generate random indicies 96 | indices <- sample(1:m, m) 97 | 98 | ind_train <- indices[1:floor(m*0.6)] 99 | ind_val <- indices[ceiling(m*0.6):floor(m*0.8)] 100 | ind_test <- indices[ceiling(m*0.8):m] 101 | 102 | walkingData$y <- to_categorical(walkingData$labels - 1) 103 | 104 | xWalk <- list(train = walkingData$x[indTrain, ,], 105 | val = walkingData$x[indVal, ,], 106 | test = walkingData$x[indTest, ,]) 107 | 108 | yWalk <- list(train = walkingData$y[indTrain, ], 109 | val = walkingData$y[indVal, ], 110 | test = walkingData$y[indTest, ]) 111 | 112 | saveRDS(xWalk, "xWalk.rds") 113 | saveRDS(yWalk, "yWalk.rds") 114 | 115 | -------------------------------------------------------------------------------- /data/bcX.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/bcX.rds -------------------------------------------------------------------------------- /data/bcY.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/bcY.rds -------------------------------------------------------------------------------- /data/walking.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/walking.rds -------------------------------------------------------------------------------- /data/xIris.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/xIris.rds -------------------------------------------------------------------------------- /data/xWalk.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/xWalk.rds -------------------------------------------------------------------------------- /data/yIris.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/yIris.rds -------------------------------------------------------------------------------- /data/yWalk.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/data/yWalk.rds -------------------------------------------------------------------------------- /keras-workshop.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | -------------------------------------------------------------------------------- /material/Deep Learning with Keras Workshop - EARL 2019.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/material/Deep Learning with Keras Workshop - EARL 2019.pdf -------------------------------------------------------------------------------- /material/README.md: -------------------------------------------------------------------------------- 1 | # Docs folder 2 | 3 | Notes images etc 4 | -------------------------------------------------------------------------------- /material/keras-workshop.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/material/keras-workshop.pdf -------------------------------------------------------------------------------- /material/save-perm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MangoTheCat/keras-workshop/94c2491100dd953b0c44d39f1f9ee229a17f5624/material/save-perm.png -------------------------------------------------------------------------------- /scripts/02 Getting Started.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Chapter 2. Getting Started with Keras ----------------------------------- 4 | 5 | 6 | library(keras) 7 | 8 | is_keras_available() 9 | 10 | # if not you may need to run: 11 | # install_keras() 12 | 13 | # Train Test Split -------------------------------------------------------- 14 | 15 | library(rsample) 16 | ## Create training and test sets 17 | 18 | set.seed(367) 19 | 20 | data_split <- initial_split(iris, strata = "Species", prop = 0.8) 21 | 22 | fullData <- list(train = analysis(data_split), 23 | test = assessment(data_split)) 24 | 25 | # Pre-processing ---------------------------------------------------------- 26 | 27 | library(recipes) 28 | library(purrr) 29 | 30 | # Recipes 31 | 32 | empty_recipe <- recipe(Species ~ ., data = fullData$train) 33 | empty_recipe 34 | 35 | # One hot encode 36 | 37 | dummy_recipe <- empty_recipe %>% 38 | step_dummy(Species, one_hot = TRUE, role = "outcome") 39 | 40 | 41 | dummy_recipe %>% 42 | prep(fullData$train) %>% 43 | bake(fullData$train, all_outcomes()) %>% 44 | head() 45 | 46 | # Center and scale 47 | 48 | scale_recipe <- empty_recipe %>% 49 | step_center(all_predictors()) %>% 50 | step_scale(all_predictors()) 51 | 52 | scale_recipe %>% 53 | prep(fullData$train) %>% 54 | bake(fullData$train, 55 | all_predictors()) %>% 56 | head() 57 | 58 | 59 | # Put it all together and prep 60 | 61 | iris_recipe <- recipe(Species ~ ., data = fullData$train) %>% 62 | step_dummy(Species, one_hot = TRUE, role = "outcome") %>% 63 | step_center(all_predictors()) %>% 64 | step_scale(all_predictors()) %>% 65 | prep(training = fullData$train) 66 | 67 | tidy(iris_recipe) 68 | 69 | ## Create x and y matrix 70 | 71 | xIris <- map(fullData, ~ bake(object = iris_recipe, 72 | new_data = .x, 73 | all_predictors(), 74 | composition = "matrix")) 75 | 76 | yIris <- map(fullData, ~ bake(object = iris_recipe, 77 | new_data = .x, 78 | all_outcomes(), 79 | composition = "matrix")) 80 | 81 | # If you're lost just do 82 | 83 | xIris <- readRDS("data/xIris.rds") 84 | yIris <- readRDS("data/yIris.rds") 85 | 86 | # Exercise 2-5 --------------------------------------------------------- 87 | 88 | # 1. Load the BreastCancer data 89 | library(mlbench) 90 | data("BreastCancer") 91 | 92 | # 2. Remove the Id column and convert all but the Class column to numeric 93 | bc <- BreastCancer %>% 94 | select(-Id) %>% 95 | mutate_at(vars(-Class), as.numeric) 96 | 97 | # 3. Split the data so that 80% is used for training and 20% for testing 98 | set.seed(82) 99 | bcSplit <- initial_split(bc, strata = "Class", prop = 0.8) 100 | 101 | bcFull <- list(train = analysis(bcSplit), 102 | test = assessment(bcSplit)) 103 | 104 | 105 | # 4. Create dummy variables for the class variable 106 | # 5. Scale the numeric variables 107 | # 6. Replace all missing values with 0 108 | # 7. Convert the target and feature data frames to matrices 109 | 110 | bc_recipe <- recipe(Class ~ ., data = bcFull$train) %>% 111 | step_dummy(Class, one_hot = TRUE, role = "outcome") %>% 112 | step_center(all_predictors()) %>% 113 | step_scale(all_predictors()) %>% 114 | step_meanimpute(all_predictors(), means = 0) %>% 115 | prep(bcFull$train) 116 | 117 | bcX <- map(bcFull, ~ bake(object = bc_recipe, 118 | new_data = .x, 119 | all_predictors(), 120 | composition = "matrix")) 121 | 122 | bcY <- map(bcFull, ~ bake(object = bc_recipe, 123 | new_data = .x, 124 | all_outcomes(), 125 | composition = "matrix")) 126 | 127 | 128 | 129 | 130 | ############# Building models ----------------- 131 | 132 | model <- keras_model_sequential() 133 | 134 | ## Add layers 135 | 136 | model %>% 137 | layer_dense(units = 10, input_shape = 4) %>% 138 | layer_dense(units = 3, activation = 'softmax') 139 | 140 | 141 | ## Define compilation 142 | 143 | model %>% compile( 144 | optimizer = 'rmsprop', 145 | loss = 'categorical_crossentropy', 146 | metrics = 'accuracy' 147 | ) 148 | 149 | ## Train the model 150 | 151 | history <- model %>% fit(xIris$train, 152 | yIris$train, 153 | epochs = 100, 154 | validation_data = list(xIris$test, yIris$test)) 155 | 156 | summary(model) 157 | 158 | plot(history) 159 | 160 | # Exercise page 2-11 161 | # 1. Load the pre-cleaned BreastCancer data 162 | # 2. Create a model with: 163 | # a. A dense layer with 5 hidden units 164 | # b. A dense, output layer using the "sigmoid" activation function 165 | # 3. Compile the model using "binary_crossentropy" as the loss function 166 | # 4. Fit the model over 20 epochs 167 | # Extension Questions 168 | # 5. Change the activation function in the first dense layer to "relu", what effect does this have? 169 | # 6. Increase the number of hidden units in the dense layer, does this have any impact? 170 | # 7. What effect does adding additional layers to your model have? 171 | 172 | # Exercise - Breast Cancer Model -------------- 173 | 174 | # reload cleaned data 175 | bcX <- readRDS("data/bcX.rds") 176 | bcY <- readRDS("data/bcY.rds") 177 | 178 | 179 | bcModel <- keras_model_sequential() 180 | 181 | bcModel %>% 182 | layer_dense(units = 5, input_shape = 9) %>% 183 | layer_dropout(rate = 0.3) %>% 184 | layer_dense(units = 2, activation = "sigmoid") 185 | 186 | bcModel %>% 187 | compile( 188 | optimizer = "rmsprop", 189 | loss = "binary_crossentropy", 190 | metrics = "accuracy" 191 | ) 192 | 193 | hist <- bcModel %>% 194 | fit(bcX$train, bcY$train, 195 | epochs = 30, 196 | validation_split = 0.2) 197 | 198 | ### Evaluate and predict model -------------- 199 | 200 | model %>% 201 | evaluate(xIris$test, yIris$test) 202 | 203 | model %>% 204 | predict(xIris$test) %>% 205 | head() 206 | 207 | model %>% 208 | predict_classes(xIris$test) 209 | 210 | 211 | # Exercise page 2-12 ---------- 212 | # 1. Using the model that you built in the last exercise and the pre-cleaned test breast cancer data evaluate the performance of your model 213 | # 2. Predict the classes for the test data 214 | 215 | bcModel %>% evaluate(bcX$test, bcY$test) 216 | 217 | bcModel %>% predict_classes(bcX$test) 218 | 219 | 220 | ############# Controlling Layers -------------------- 221 | 222 | model <- keras_model_sequential() 223 | 224 | model %>% 225 | layer_dense(units = 10, input_shape = 4) %>% 226 | layer_dropout(rate = 0.3) %>% 227 | layer_dense(units = 3, activation = 'softmax') 228 | 229 | model %>% compile( 230 | optimizer = 'rmsprop', 231 | loss = 'categorical_crossentropy', 232 | metrics = 'accuracy' 233 | ) 234 | 235 | model %>% fit(xIris$train, 236 | yIris$train, 237 | epochs = 100, 238 | validation_data = list(xIris$test, yIris$test)) 239 | 240 | 241 | # Exercise page 2-16 242 | # 1. Using the pre-cleaned Boston House Price data, build a model from scratch to predict the house price deciding: 243 | # a. An initial number of layers 244 | # b. The number of hidden units 245 | # c. The activation function(s) to use 246 | # 2. Add a dropout layer to your model, does this improve performance on the test data? 247 | # 248 | 249 | -------------------------------------------------------------------------------- /scripts/03 Spatial.R: -------------------------------------------------------------------------------- 1 | 2 | library(tidyverse) 3 | library(keras) 4 | 5 | walking <- readRDS("data/walking.rds") 6 | names(walking) 7 | 8 | dim(walking$x) 9 | 10 | walking$labels 11 | 12 | # Split train and test. Exercise 1 13 | 14 | set.seed(20) 15 | m <- nrow(walking$x) 16 | # generate random indicies 17 | indices <- sample(1:m, m) 18 | indTrain <- indices[1:floor(m*0.8)] 19 | indTest <- indices[ceiling(m*0.8):m] 20 | 21 | xWalk <- list(train = walking$x[indTrain, ,], 22 | test = walking$x[indTest, ,]) 23 | 24 | yWalk <- list(train = walking$y[indTrain, ], 25 | test = walking$y[indTest, ]) 26 | 27 | # Make a new model 28 | 29 | model <- keras_model_sequential() 30 | 31 | # Conv layer 32 | model %>% 33 | layer_conv_1d(filters = 40, kernel_size = 40, strides = 2, 34 | activation = "relu", input_shape = c(260, 3)) 35 | model 36 | 37 | # Max pool 38 | model %>% 39 | layer_max_pooling_1d(pool_size = 2) 40 | model 41 | 42 | # Flatten 43 | model %>% 44 | layer_flatten() 45 | model 46 | 47 | # Finish 48 | model %>% 49 | layer_dense(units = 100, activation = "sigmoid") %>% 50 | layer_dense(units = 15, activation = "softmax") 51 | model 52 | 53 | 54 | model %>% compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = c("accuracy")) 55 | history <- model %>% fit(xWalk$train, yWalk$train, 56 | epochs = 15, 57 | batch_size = 128, 58 | validation_split = 0.3, 59 | verbose = 1) 60 | 61 | 62 | # the whole thing 63 | model <- keras_model_sequential() %>% 64 | layer_conv_1d(filters = 40, kernel_size = 30, strides = 2, 65 | activation = "relu", input_shape = c(260, 3)) %>% 66 | layer_max_pooling_1d(pool_size = 2) %>% 67 | layer_conv_1d(filters = 40, kernel_size = 10, activation = "relu") %>% 68 | layer_max_pooling_1d(pool_size = 2) %>% 69 | layer_flatten() %>% 70 | layer_dense(units = 100, activation = "sigmoid") %>% 71 | layer_dense(units = 15, activation = "softmax") 72 | 73 | model %>% compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = c("accuracy")) 74 | history <- model %>% fit(xWalk$train, yWalk$train, 75 | epochs = 15, 76 | batch_size = 128, 77 | validation_split = 0.3, 78 | verbose = 1) 79 | 80 | -------------------------------------------------------------------------------- /scripts/breast_cancer.R: -------------------------------------------------------------------------------- 1 | library(keras) 2 | library(rsample) 3 | library(recipes) 4 | library(tidyverse) 5 | library(mlbench) 6 | 7 | ########################### 8 | 9 | data("BreastCancer") 10 | 11 | ########################### 12 | # Data Prep 13 | 14 | set.seed(19) 15 | 16 | bc_data <- BreastCancer %>% 17 | select(-Id) %>% 18 | mutate_at(vars(-Class), as.numeric) 19 | 20 | ## Split into train and test 21 | bc_split <- initial_split(bc_data, prop = 0.8, strata = Class) 22 | 23 | bc_train <- training(bc_split) 24 | bc_test <- testing(bc_split) 25 | 26 | # Preprocess 27 | 28 | bc_recipe <- recipe(Class ~ ., data = bc_train) %>% 29 | step_center(all_predictors()) %>% 30 | step_scale(all_predictors()) %>% 31 | step_meanimpute(all_predictors()) %>% 32 | step_dummy(Class, role = "outcome") %>% 33 | prep(bc_train) 34 | 35 | bc <- list(train = bc_train, test = bc_test) 36 | 37 | bcX <- map(bc, ~ bake(bc_recipe, .x, all_predictors(), composition = "matrix")) 38 | bcY <- map(bc, ~ bake(bc_recipe, .x, all_outcomes(), composition = "matrix")) 39 | 40 | # write_rds(xData, "./Training/Material/Workshops/Workshop_DeepLearning/Data/BreastCancerCleanFeatures.rds") 41 | # write_rds(yData, "./Training/Material/Workshops/Workshop_DeepLearning/Data/BreastCancerCleanTarget.rds") 42 | 43 | 44 | ################### 45 | 46 | # Build keras model 47 | 48 | bcModel <- keras_model_sequential() 49 | 50 | bcModel %>% 51 | layer_dense(units = 5, input_shape = 9) %>% 52 | layer_dropout(rate = 0.3) %>% 53 | layer_dense(units = 1, activation = "sigmoid") 54 | 55 | bcModel %>% 56 | compile( 57 | optimizer = "rmsprop", 58 | loss = "binary_crossentropy", 59 | metrics = "accuracy" 60 | ) 61 | 62 | hist <- bcModel %>% 63 | fit(bcX$train, bcY$train, 64 | epochs = 30, 65 | validation_split = 0.2) 66 | 67 | 68 | bcModel %>% evaluate(bcX$test, bcY$test) 69 | 70 | bcModel %>% predict_classes(bcX$test) 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /scripts/iris.R: -------------------------------------------------------------------------------- 1 | library(keras) 2 | library(rsample) 3 | library(recipes) ## ** requires devel version of recipes for now ** 4 | library(tidyverse) 5 | 6 | ## Create training and test sets 7 | 8 | set.seed(367) 9 | 10 | data_split <- initial_split(iris, strata = "Species", prop = 0.8) 11 | 12 | fullData <- list(train = analysis(data_split), 13 | test = assessment(data_split)) 14 | 15 | 16 | # Recipes 17 | 18 | empty_recipe <- recipe(Species ~ ., data = fullData$train) 19 | empty_recipe 20 | 21 | # One hot encode 22 | 23 | dummy_recipe <- empty_recipe %>% 24 | step_dummy(Species, one_hot = TRUE, role = "outcome") 25 | 26 | 27 | dummy_recipe %>% 28 | prep(fullData$train) %>% 29 | bake(fullData$train, all_outcomes()) %>% 30 | head() 31 | 32 | # Center and scale 33 | 34 | scale_recipe <- empty_recipe %>% 35 | step_center(all_predictors()) %>% 36 | step_scale(all_predictors()) 37 | 38 | scale_recipe %>% 39 | prep(fullData$train) %>% 40 | bake(fullData$train, 41 | all_predictors()) %>% 42 | head() 43 | 44 | 45 | # Put it all together and prep 46 | 47 | iris_recipe <- recipe(Species ~ ., data = fullData$train) %>% 48 | step_dummy(Species, one_hot = TRUE, role = "outcome") %>% 49 | step_center(all_predictors()) %>% 50 | step_scale(all_predictors()) %>% 51 | prep(training = fullData$train) 52 | 53 | tidy(iris_recipe) 54 | 55 | ## Create x and y matrix 56 | 57 | xIris <- map(fullData, ~ bake(object = iris_recipe, 58 | new_data = .x, 59 | all_predictors(), 60 | composition = "matrix")) 61 | 62 | yIris <- map(fullData, ~ bake(object = iris_recipe, 63 | new_data = .x, 64 | all_outcomes(), 65 | composition = "matrix")) 66 | 67 | 68 | ############# Building models 69 | 70 | model <- keras_model_sequential() 71 | 72 | ## Add layers 73 | 74 | model %>% 75 | layer_dense(units = 10, input_shape = 4) %>% 76 | layer_dense(units = 3, activation = 'softmax') 77 | 78 | 79 | ## Define compilation 80 | 81 | model %>% compile( 82 | optimizer = 'rmsprop', 83 | loss = 'categorical_crossentropy', 84 | metrics = 'accuracy' 85 | ) 86 | 87 | ## Train the model 88 | 89 | history <- model %>% fit(xIris$train, 90 | yIris$train, 91 | epochs = 100, 92 | validation_data = list(xIris$test, yIris$test)) 93 | 94 | summary(model) 95 | 96 | plot(history) 97 | 98 | 99 | ### Evaluate and predict model 100 | 101 | model %>% 102 | evaluate(xIris$test, yIris$test) 103 | 104 | model %>% 105 | predict(xIris$test) %>% 106 | head() 107 | 108 | model %>% 109 | predict_classes(xIris$test) 110 | 111 | ############# Controlling Layers 112 | 113 | model <- keras_model_sequential() 114 | 115 | model %>% 116 | layer_dense(units = 10, input_shape = 4) %>% 117 | layer_dropout(rate = 0.3) %>% 118 | layer_dense(units = 3, activation = 'softmax') 119 | 120 | model %>% compile( 121 | optimizer = 'rmsprop', 122 | loss = 'categorical_crossentropy', 123 | metrics = 'accuracy' 124 | ) 125 | 126 | model %>% fit(xIris$train, 127 | yIris$train, 128 | epochs = 100, 129 | validation_data = list(xIris$test, yIris$test)) 130 | -------------------------------------------------------------------------------- /scripts/walking_final.R: -------------------------------------------------------------------------------- 1 | library(keras) 2 | # Use this to limit cpu 3 | use_session_with_seed(1234) 4 | 5 | 6 | # Load the test data 7 | xWalk <- readRDS("/data/xWalk.rds") 8 | yWalk <- readRDS("/data/yWalk.rds") 9 | 10 | # Make an empty model 11 | model <- keras_model_sequential() 12 | 13 | # Build our CNN 14 | model %>% 15 | layer_conv_1d( 16 | filters = 40, 17 | kernel_size = 30, 18 | strides = 2, 19 | activation = "relu", 20 | input_shape = c(260, 3) 21 | ) %>% 22 | layer_max_pooling_1d(pool_size = 2) %>% 23 | layer_conv_1d(filters = 40, 24 | kernel_size = 10, 25 | activation = "relu") %>% 26 | layer_max_pooling_1d(pool_size = 2) %>% 27 | layer_flatten() %>% 28 | layer_dense(units = 100, activation = "sigmoid") %>% 29 | layer_dense(units = 15, activation = "softmax") 30 | 31 | model 32 | 33 | 34 | 35 | # Compile 36 | model %>% 37 | compile(loss = "categorical_crossentropy", 38 | optimizer = "adam", 39 | metrics = c("accuracy")) 40 | 41 | # Run 42 | history <- model %>% fit( 43 | xWalk$train, 44 | yWalk$train, 45 | epochs = 15, 46 | batch_size = 128, 47 | validation_split = 0.3, 48 | verbose = 1 49 | ) 50 | 51 | # Evaluate 52 | model %>% 53 | evaluate(xWalk$test, yWalk$test, verbose = 0) 54 | --------------------------------------------------------------------------------