Unexpected error keras: "Error: unexpected ',' in:" - r

I am building a classification model with keras R, and my codes are as follows:
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = 'relu', input_shape = ncol(x_train),kernel_regularizer = regularizer_l2(0.001),) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 128, activation = 'relu',kernel_regularizer = regularizer_l2(0.001),) %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 2, activation = 'sigmoid')
history <- model %>% compile(
loss = 'binary_crossentropy',
optimizer = 'adam',
metrics = c('accuracy')
)
model %>% fit(x_train,
y_train,
epochs = 50,
batch_size = 128,
validation_data = (x_val,y_val))
Everything is fine but when I tried to pass the outside data (x_val, y_val) to be used as validation data using 'validation_data', then It got this error:
Error: unexpected ',' in:
" batch_size = 128,
validation_data =(x_val,"
If I simply use validation_split=0.2 then all good.
I looked at the codes many times, but could not figure out what is wrong here.
Can somebody help me on this please?
Many thanks,
Ho

The issue is based on the input arguments to be passed. It should be a list as there is no tuple in R (though it is there in python
According to keras documentation
validation_data - Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. This could be a list (x_val, y_val) or a list (x_val, y_val, val_sample_weights). validation_data will override validation_split.
So, we just replace the (x_val, y_val) with list(x_val, y_val)
model %>%
fit(x_train,
y_train,
epochs = 50,
batch_size = 128,
validation_data = list(x_val,y_val))

Related

Fixing initial weights when training Keras model in R

I want to fix the weights of the model I'm training to get reproducible results for the report. The problem is that on different runs I get similar but slightly different results and training sometimes takes 600 epochs, sometimes takes 3500 using callback_early_stopping monitoring validation MSE and with min_delta of 0.00003.
Overall, I'm happy enough with results of all runs, but just need to find if there's a way to get reproducible results by fixing weights.
I tried setting seed at various parts of the process - before creating model, before compiling it and before training but nothing seems to work. Any way to do it?
BATCH <- nrow(x_train)
SHAPE <- ncol(x_train)
# Create a neural network model
set.seed(42)
model <- keras_model_sequential()
model %>%
layer_dense(units = 12, activation = "relu", input_shape = c(SHAPE)) %>%
layer_dense(units = 24, activation = "relu") %>%
layer_dense(units = 1, activation = "linear")
#print model summary
print(summary(model))
# initialise early stopping callback and optimiser
early_stoping <- callback_early_stopping(
monitor = "val_loss",
min_delta = 0.00003,
patience = 50,
restore_best_weights = TRUE
)
optim <- optimizer_adam(learning_rate = 0.00005)
set.seed(42)
model %>% compile(
optimizer = optim,
loss = "mse",
metrics = c("mse", "mae")
)
# fit model
set.seed(42)
val_data <- list(x_val = x_val, y_val = y_val)
hist <- model %>% fit(
x = x_train,
y = y_train,
batch_size = BATCH,
epochs = 6000,
validation_data = val_data,
shuffle = FALSE,
callbacks = early_stoping
)

How to define lstm, units, lags and batch size in keras package

I have data of almost 4700 entries. I have to predict power output. I am unable to understand the algorithm of the LSTM like what is units? how to select units for my data and what are data lags? The code I am using for this work is available here https://www.r-bloggers.com/2018/11/lstm-with-keras-tensorflow/ as I have interest in lstm so I am only using that part of this code.
library(keras)
model <- keras_model_sequential()
model %>%
layer_lstm(units = 100,
input_shape = c(datalags, 2),
batch_size = batch.size,
return_sequences = TRUE,
stateful = TRUE) %>%
layer_dropout(rate = 0.5) %>%
layer_lstm(units = 50,
return_sequences = FALSE,
stateful = TRUE) %>%
layer_dropout(rate = 0.5) %>%
layer_dense(units = 1)
model %>%
compile(loss = 'mae', optimizer = 'adam')
So in this code I am unable to understand
what is meant by units here?
What are datalags,
The code uses datalags value as 10, how do I define it for my data? and how to manually select them for my data?

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

Keras neural network not fitting in R

I made a neural network in R using the Keras package. I basically made the same model I had created in python. I used the same data as well in the same order. However, when I run it in R, the model doesn't seem to be fitting at all.
When I call predict on the model, it returns the same value regardless of the input.
I'm guessing the weights are zeroing out and its returning the bias.
Heres how I built the model:
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = 'relu',input= c(18)) %>%
layer_dense(units = 64, activation = 'relu')%>%
layer_dropout(rate = 0.25) %>%
layer_dense(units = 32, activation = 'relu') %>%
layer_dropout(rate = 0.25) %>%
layer_dense(units = 16, activation = 'relu') %>%
layer_dropout(rate = 0.25) %>%
layer_dense(units = 8, activation = 'relu') %>%
layer_dense(units = 2, activation = 'softmax')
Heres the output when I call predict:
model%>%
predict(nbainput_test_x)

R-Keras - How come val_loss is not plotting

My first time playing with Keras. I tried to run the model and see the loss and accuracy. For some reason, its not plotting the loss for val_loss.
My code:
model <- keras_model_sequential() %>%
layer_dense(units = 256, activation = "relu", input_shape = dim(train.X)[[2]]) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 128, activation = "relu") %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 1, activation = "sigmoid")
model %>% compile (
optimizer = "rmsprop", #configuring optimizer = optimizer_rmsprop(lr = 0.001)
loss = "binary_crossentropy", #custom loss -> loss_binary_crossentropy
metrics = c("accuracy") #metric_binary_accuracy
)
history <- model %>% fit(
train.X,
train.Y,
epochs = 100,
batch_size = 64,
validation_data = list(x_val, y_val)
)
My results:
I would really appreciate if someone can explain to me why the val_loss function is not plotting.

Resources