How to implement Bayesian optimization with Keras tuneR - r

I am hoping to run Bayesian optimization for my neural network via keras tuner.
I have the following code so far:
build_model <- function(hp) {
model <- keras_model_sequential()
model %>% layer_dense(units = hp$Int('units', min_value = 10, max_value = 50, step = 10),
activation = "relu",
input_shape = dim(X_pca_scores_scaled)[[2]]) %>%
layer_dropout(rate = hp$Float('rate', min_value = 0, max_value = 0.5, step = 0.1)) %>%
layer_dense(units = hp$Int('units', min_value = 0, max_value = 50, step = 10),
activation = "relu") %>%
layer_dropout(rate = hp$Float('rate', min_value = 0, max_value = 0.5, step = 0.1)) %>%
layer_dense(units = 1) %>%
compile(
optimizer = "adam",
loss = "mse",
metrics = c("mae"))
return(model)
}
tuner <- kerastuneR::BayesianOptimization(
objective = 'mae',
max_trials = 30)
stop_early <- callback_early_stopping(monitor = "mae",
patience = 5,
min_delta = 0.25,
mode = "min")
tuner %>% fit_tuner(np_array(X_pca_scores_scaled),
np_array(train_targets),
epochs = 30,
callbacks = c(stop_early))
The above code will lead to the following error:
Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'BayesianOptimizationOracle' object has no attribute 'search'
I'm not sure what an oracle is...so I know the problem is somewhere in my implementation regarding that.

Related

Parameter adjustment using kerastuneR packet deep neural network

What is the cause of the following code error?
library(magrittr)
x_data <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data <- ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()
x_data2 <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data2 <- ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()
library(keras)
library(tensorflow)
library(kerastuneR)
build_model = function(hp) {
model = keras_model_sequential()
model %>% layer_dense(units = hp$Int('units',
min_value = 32,
max_value = 512,
step= 32),input_shape = ncol(x_data),
activation = 'relu') %>%
layer_dense(units = 1, activation = 'softmax') %>%
compile(
optimizer = tf$keras$optimizers$Adam(
hp$Choice('learning_rate',
values=c(1e-2, 1e-3, 1e-4))),
loss = 'binary_crossentropy',
metrics = 'accuracy')
return(model)
}
tuner = RandomSearch(
build_model,
objective = 'val_accuracy',
max_trials = 5,
executions_per_trial = 3,
directory = 'my_dir',
project_name = 'helloworld')
tuner %>% search_summary()
tuner %>% fit_tuner(x_data,y_data,
epochs = 5,
validation_data = list(x_data2,y_data2))
result = kerastuneR::plot_tuner(tuner)
best_5_models = tuner %>% get_best_models(5)
best_5_models[[1]] %>% plot_keras_model()
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Objective value missing in metrics reported to the Oracle, expected: ['val_accuracy'], found: dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])

How to control learning rate in KerasR in R

To fit a classification model in R, have been using library(KerasR). To control learning rate and KerasR says
compile(optimizer=Adam(lr = 0.001, beta_1 = 0.9, beta_2 = 0.999, epsilon = 1e-08, decay = 0, clipnorm = -1, clipvalue = -1), loss = 'binary_crossentropy', metrics = c('categorical_accuracy') )
But it is given me an error like this
Error in modules$keras.optimizers$Adam(lr = lr, beta_1 = beta_2,
beta_2 = beta_2, : attempt to apply non-function
I also used keras_compile still getting the same error.
I can change optimizer in compile but the largest learning rate is 0.01, I want to try 0.2.
model <- keras_model_sequential()
model %>% layer_dense(units = 512, activation = 'relu', input_shape = ncol(X_train)) %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 128, activation = 'relu')%>%
layer_dropout(rate = 0.1) %>%
layer_dense(units = 2, activation = 'sigmoid')%>%
compile(
optimizer = 'Adam',
loss = 'binary_crossentropy',
metrics = c('categorical_accuracy')
)
I think the issue is you are using two different libraries kerasR and keras together. You should use only one of them. First, you are using keras_model_sequential function
which is from keras and then you try to use Adam function which is from kerasR library. You find the difference between these two libraries here: https://www.datacamp.com/community/tutorials/keras-r-deep-learning#differences
The following code is working for me which is using only keras library.
library(keras)
model <- keras_model_sequential()
model %>%
layer_dense(units = 512, activation = 'relu', input_shape = ncol(X_train)) %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 128, activation = 'relu')%>%
layer_dropout(rate = 0.1) %>%
layer_dense(units = 2, activation = 'sigmoid')%>%
compile(optimizer=optimizer_adam(lr = 0.2), loss= 'binary_crossentropy', metrics = c('accuracy') )

Building a LSTM for Stock Prediction

I am building my first LSTM model using keras in R. I believe my problem is with my input_shape and I would appreciate your help.
I am using closing stock returns at time t to predict returns at t+1
so i believe my input shape should equal 1.
Here is my code:
#LSTM Model
model <- keras::keras_model_sequential() %>%
layer_lstm(units = 50, return_sequences = TRUE, dropout = 0.2, weights = 0.01, input_shape = 1
)%>%
layer_lstm(units = 100, return_sequences = FALSE, dropout = 0.2, weights = 0.01
)%>%
layer_dense(units = 1, activation = 'linear')
model %>% compile(
optimizer = optimizer_rmsprop(),
loss = "mse"
)
However when I run this I get the following error message:
error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Input 0 is incompatible with layer lstm_12: expected ndim=3, found ndim=2
Any ideas on what I am doing wrong and suggestions on what I should do?
Thank you for your time.
I believe the information I provided should be enough to answer my question. If not, here is my full code to get the data and results
#Downloading Stock Price and normalize to returns
BRI_Price <-quantmod::getSymbols("BBRI.JK", src = "yahoo", from = as.Date("2015-01-01"), to = as.Date("2017-02-17"), by = "day", auto.assign = FALSE)
Returns_Closing <-quantmod::dailyReturn(BRI_Price$BBRI.JK.Close)
#Training and Testing Data
train_size <- round(.9*nrow(Returns_Closing))
test_size <- nrow(Returns_Closing) - train_size
train <- Returns_Closing[0:train_size,]
test <- Returns_Closing[train_size:nrow(Returns_Closing),]
#Reshape into X=t and Y=t+1
x_train <- Returns_Closing[1:train_size,]
y_train <- Returns_Closing[2:train_size+1]
x_test <- Returns_Closing[train_size:nrow(Returns_Closing),]
y_test <- Returns_Closing[468:nrow(Returns_Closing),]
#LSTM Model
model <- keras::keras_model_sequential() %>%
layer_lstm(units = 50, return_sequences = TRUE, dropout = 0.2, weights = 0.01, input_shape = 1 )%>%
layer_lstm(units = 100, return_sequences = FALSE, dropout = 0.2, weights = 0.01 )%>%
layer_dense(units = 1, activation = 'linear')
model %>%
compile( optimizer = optimizer_rmsprop(), loss = "mse" )
#train the model
Train_Model <- model %>% fit( x = x_train, batch_size = 100, epochs = 1, validation_data = list(x_test, y_test) )

How to make sure inputs can be diveded by batch size in stateful LSTM?

I'm having some issues training a network using stateful LSTMs.
Given the code below, I'm getting the following error message:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: In a stateful network, you should only pass inputs with a number of samples that can be divided by the batch size. Found: 9384 samples
Input is sent from an external application, so I cannot control the exact number of inputs sent. What would be the best way to ensure that the input can always be divided nu the batch size?
neural.train = function(model,XY)
{
XY <- as.matrix(XY)
X <- XY[,-ncol(XY)]
Y <- XY[,ncol(XY)]
Y <<- ifelse(Y > 0,1,0)
dropout <- 0.3
batchSize <- 64
newModel <- keras_model_sequential()
newModel %>%
layer_lstm(batch_input_shape = c(batchSize, 30, 19), units = 72, return_sequences = TRUE, stateful = TRUE, dropout = dropout, recurrent_dropout = dropout) %>%
#layer_dense(units = 20) %>%
#layer_lstm(units = 50, return_sequences = TRUE, stateful = TRUE, dropout = dropout, recurrent_dropout = dropout) %>%
layer_lstm(units = 16, dropout = dropout, recurrent_dropout = dropout, return_sequences = FALSE, stateful = TRUE) %>%
layer_dense(units = 8) %>%
layer_batch_normalization() %>%
layer_dense(units = 1, activation = 'relu')
newModel %>% compile(
optimizer = optimizer_rmsprop(lr = 0.001),
loss = 'binary_crossentropy',
metrics = c('accuracy')
)
#X_conv <- matrix(c(X[1,1:10],X[1,11:20]),ncol=10,nrow=2)
ar <- array(X,c(dim(X)[1],30,19))
#newModel %>% fit(X, Y, epochs=20, batch_size=100, validation_split = 0.2, shuffle=TRUE, callbacks=reduce_lr)
newModel %>% fit(ar, Y, epochs=100, batch_size=batchSize, validation_split = 0.2, shuffle=FALSE)
Models[[model]] <<- serialize_model(newModel, include_optimizer = TRUE)
}

Keras (R) error while executing fit generator

I have an error using fit_generator in R...
here's my code..`
model <- keras_model_sequential()
model %>%
layer_conv_2d(32, c(3,3), input_shape = c(64, 64, 3)) %>%
layer_activation("relu") %>%
layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_conv_2d(32, c(3, 3)) %>%
layer_activation("relu") %>%
layer_max_pooling_2d(pool_size = c(2, 2)) %>%
layer_flatten() %>%
layer_dense(128) %>%
layer_activation("relu") %>%
layer_dense(128) %>%
layer_activation("relu") %>%
layer_dense(2) %>%
layer_activation("softmax")
opt <- optimizer_adam(lr = 0.001, decay = 1e-6)
model %>%
compile(loss = "categorical_crossentropy", optimizer = opt, metrics = "accuracy")
train_gen <- image_data_generator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = T)
test_gen <- image_data_generator(rescale = 1./255)
train_set = train_gen$flow_from_directory('dataset/training_set',
target_size = c(64, 64),
class_mode = "categorical")
test_set = test_gen$flow_from_directory('dataset/test_set',
target_size = c(64, 64),
batch_size = 32,
class_mode = 'categorical')
model$fit_generator(train_set,
steps_per_epoch = 50,
epochs = 10)
Error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
StopIteration: 'float' object cannot be interpreted as an integer
If I put validation set it has another error too
bool(validation_data). Float error..
It is difficult help you without a minimal reproducible example.
I am guessing you get this error when you are trying to run
train_set = train_gen$flow_from_directory('dataset/training_set',
target_size = c(64, 64),
class_mode = "categorical")
Here you are calling the python function yourself using reticulate and not a keras (the R package) wrapper. That might work, but you have to be more explicit about the type and use target_size = as.integer(c(64, 64)), since python expects an integer.
Alternatively, I would suggest looking into the flow_images_from_directory() function included in the keras package.
The same goes for
model$fit_generator(train_set,
steps_per_epoch = 50,
epochs = 10)
I'd suggest looking into
model %>%
fit_generator()
instead, which is part of the keras package.

Resources