Parameter adjustment using kerastuneR packet deep neural network - r

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'])

Related

How to implement Bayesian optimization with Keras tuneR

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.

How to apply CNN on a dataframe in R?

now I am trying to build a deep learning model as a classifier. My input is a 2D dataframe. I was wondering if it is possible to use CNN layer in the model.
My data looks like this way:
x_train <- data.frame(pain = c(rep(c(0,1), c(4,5))),
gender = c(rep(c(0,1), c(3,6))),
age = c(runif(9,0,1)))
y_train <- data.frame(illness = c(rep(c(0,1), c(2,2)), rep(c(0,1), c(3,2))))
Now I construct a model like this way:
model <- keras_model_sequential() %>%
layer_conv_1d(filters = 1, kernel_size = 2, activation = "elu",
input_shape = c(3,1)) %>%
# layer_max_pooling_2d(pool_size = c(2,2)) %>%
layer_conv_1d(filters = 1, kernel_size = 2, activation = "elu") %>%
layer_max_pooling_1d(pool_size = 2) %>%
layer_dropout(0.5) %>%
layer_dense(
units = 5,
activation = "elu"
) %>%
layer_dropout(0.3) %>%
layer_dense(
units = 3,
activation = "elu"
) %>%
layer_dropout(0.1) %>%
layer_dense(units = 1, activation = "elu")
model %>% compile(
loss = "mean_squared_error",
optimizer = "SGD",
metrics = c('mae', 'acc') # "accuracy"
)
But it doesn't work and tells me it needs a 3 dimensions input
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking input: expected conv1d_33_input to have 3 dimensions, but got array with shape (9, 3)
Could anyone give me some advice about that? Thanks a lot for your help : )

keras input shape for multivariate LSTM

I'm trying to fit a LSTM model in keras where I have two inputs
y is the output with shape (100,10)
x is the input with shape (100,20)
library(keras)
x_train_vec <- matrix(rnorm(2000), ncol = 20, nrow = 100)
x_train_arr <- array(data = x_train_vec, dim = c(nrow(x_train_vec), 1, 20))
y_train_vec <- matrix(rnorm(1000), ncol = 10, nrow = 100)
y_train_arr <- array(data = y_train_vec, dim = c(nrow(x_train_vec), 1, 10))
> dim(x_train_arr)
[1] 100 1 20
> dim(y_train_arr)
[1] 100 1 10
Now I want to fit the LSTM model
model <- keras_model_sequential()
model %>%
layer_lstm(units = 50,
input_shape = c(1,10),
batch_size = 1) %>%
layer_dense(units = 1)
model %>%
compile(loss = 'mae', optimizer = 'adam')
model %>% fit(x = x_train_arr,
y = y_train_arr,
batch_size = 1,
epochs = 10,
verbose = 1,
shuffle = FALSE)
But I get this error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking input: expected lstm_21_input to have
shape (1, 10) but got array with shape (1, 20)
If I change input size to c(1,20), I get:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking target: expected dense_13 to have 2
dimensions, but got array with shape (100, 1, 10)
I also played with different setting but it never works.
IF your Keras version is < 2.0 you need to use model.add(TimeDistributed(Dense(1))).
NOTE that syntax is for python, you need to find the R equivealent.
I figured out how to make it work:
x_train_vec <- matrix(rnorm(2000), ncol = 20, nrow = 100)
x_train_arr <- array(data = x_train_vec, dim = c(nrow(x_train_vec), 20, 1))
y_train_vec <- matrix(rnorm(1000), ncol = 10, nrow = 100)
y_train_arr <- array(data = y_train_vec, dim = c(nrow(x_train_vec), 10))
model <- keras_model_sequential()
model %>%
layer_lstm(units = 50,
input_shape = c(20,1),
batch_size = 1) %>%
layer_dense(units = 10)
model %>%
compile(loss = 'mae', optimizer = 'adam')
model %>% fit(x = x_train_arr,
y = y_train_arr,
batch_size = 1,
epochs = 10,
verbose = 1,
shuffle = FALSE)

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