### Keras ### required.packages <- c('keras', 'caret', 'ggplot2') new.packages <- required.packages[!(required.packages %in% installed.packages()[,"Package"])] if(length(new.packages)) install.packages(new.packages, repos='http://cran.us.r-project.org') library(keras, quietly = TRUE) library(caret, quietly = TRUE) library(ggplot2, quietly = TRUE) neural.init <- function() { set.seed(503) Models <<- vector("list") } neural.train <- function(model, XY) { XY <- as.matrix(XY, ncol = ncol(XY)) X <- XY[,-ncol(XY)] Y <- XY[,ncol(XY)] # convert target to binary Y <- ifelse(Y > 0, 1, 0) # build network architecture mod <- keras_model_sequential() mod %>% layer_dense(units = 20, activation = 'relu', input_shape = c(ncol(X))) %>% layer_dropout(rate = 0.2) %>% layer_dense(units = 20, activation = 'relu') %>% layer_dropout(rate = 0.2) %>% layer_dense(units = 20, activation = 'relu') %>% layer_dropout(rate = 0.2) %>% layer_dense(units = 1, activation = 'sigmoid') mod %>% compile( loss = 'binary_crossentropy', optimizer = optimizer_rmsprop(), metrics = c('accuracy') ) history <- mod %>% fit( X, Y, epochs = 150, batch_size = 1000, validation_split = 0, shuffle = FALSE ) Models[[model]] <<- serialize_model(mod, include_optimizer = TRUE) } neural.predict <- function(model, X) { X <- as.matrix(X, nrow=1) m <- Models[[model]] #pred <- m %>% predict_classes(t(X)) pred <- m %>% predict_proba(t(X)) return(pred) } neural.save <- function(name) { save(Models,file=name) } neural.load <- function(name) { load(name) for(i in c(1:length(Models))) { Models[[i]] <<- unserialize_model(Models[[i]], custom_objects = NULL, compile = NULL) } } hp.tune <- function(dropout, num_epochs, filename) { neural.init() # load path <- "C:/Zorro/Data/" XY <<- read.csv(paste0(path, 'adv_69_export_dataEURUSD_L.csv'), header = F) XY <- data.frame(XY) # convert to data frame. some models will need a matrix, but df OK for caret # convert target to factor XY[, ncol(XY)] <- ifelse(XY[, ncol(XY)] > 0, 1, 0) XY[, ncol(XY)] <- as.factor(XY[, ncol(XY)]) # split into training and test sets training.samples <- createDataPartition(XY[, ncol(XY)], p=0.75, list=FALSE) XY.train <- XY[training.samples, ] XY.test <- XY[-training.samples, ] X_train <- as.matrix(XY.train[,-ncol(XY)]) Y_train <- as.matrix(XY.train[, ncol(XY)]) X_test <- as.matrix(XY.test[, -ncol(XY)]) Y_test <- as.matrix(XY.test[, ncol(XY)]) # scale features scaler <- preProcess(X_train, method = c("center", "scale")) X_train <- predict(scaler, X_train) X_test <- predict(scaler, X_test) # build network architecture model <- keras_model_sequential() model %>% layer_dense(units = 20, activation = 'relu', input_shape = c(ncol(X_train))) %>% layer_dropout(rate = dropout) %>% layer_dense(units = 20, activation = 'relu') %>% layer_dropout(rate = dropout) %>% layer_dense(units = 20, activation = 'relu') %>% layer_dropout(rate = dropout) %>% layer_dense(units = 1, activation = 'sigmoid') model %>% compile( loss = 'binary_crossentropy', optimizer = optimizer_rmsprop(), metrics = c('accuracy') ) history <- model %>% fit( X_train, Y_train, epochs = num_epochs, batch_size = 1000, validation_split = 0.33, shuffle = FALSE ) train_val <- plot(history) png(paste0(path, filename, ".png")) print(train_val) dev.off() }