R.keras error during call to fit function - r

I am new to R keras, so please bear with me. I am trying to build a simple model using variables that are categorical, but I've recast as numeric.
I can get examples working from various tutorials in R/keras with my current installation so I know its not in reticulate or tensorflow or even R. However, when I try to use my own data to create the simple model, I obtain the following errors during the "fit" execution:
I'm pretty sure its my training data format, but I cannot for the life of me figure out what is going wrong. Thank you kindly in advance.
# Fit
model_one <- model %>%
+ fit(training,
+ trainLabels,
+ epochs = 100,
+ batch_size = 32,
+ validation_split = 0.2)
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:571 train_function *
outputs = self.distribute_strategy.run(
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:951 run **
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2290 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2649 _call_for_each_replica
return fn(*args, **kwargs)
C:\Users\JRM\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\site-packages\tensorflow\python\keras\engine\training.py:533 train_step **
y, y_pred, sample_weight, regulari
I've upload my script and sample data file to github:
Sample Data and Script to reproduce error

Actually, found the error:
One hot encoding generates a 2 column matrix:
# One hot encoding
trainLabels <- to_categorical(trainingtarget)
testLabels <- to_categorical(testtarget)
print(testLabels[1:10,])
and the model was expecting 3 columns.
I changed the model call to automatically accept the correct number of columns based on the variables instead:
model %>%
layer_dense(units = 8, activation = 'relu', input_shape = ncol(test)) %>%
layer_dense(units = ncol(trainLabels), activation = 'softmax')

Related

input error when fitting model Neural network in R

I get an error when i try to run this code. i followed a guide on youtube for building a neural network. Everything works except when i try to run this code to fit the model.
history <- modnn %>% fit(
train_X_matrix, train_Y, epochs = 50, batch_size = 600,
validation_data = list(validation_X_matrix,validation_Y))
```
the error i get when i try to run the code above, all the names you see are the names of the columns. So the features of the model:
[error in visual studio](https://i.stack.imgur.com/ZaPXw.png)
some extra info about the variables i use. Here i created a matrix of the input variables. They did this in the guide. I tried train_x_data as input then it gave the same error but immediately so not after 1 epoch
```
# dependent and independent variables in 1 dataframe
train_X_data <- data.frame(train_X,train_y)
validation_X_data <- data.frame(validation_X,validation_y)
train_X_matrix <- model.matrix(average_daily_rate ~. -1 , data = train_X_data)
train_Y <- train_X_data$average_daily_rate
validation_X_matrix <- model.matrix(average_daily_rate ~. -1, data = validation_X_data)
validation_Y <- validation_X_data$average_daily_rate
```
The model i use, it is just a simple single layer model for testing.
# 1) single layer model structure
# step 1 make architecture powerful enough
modnn <- keras_model_sequential() %>%
layer_dense(units = 500, activation = "relu",
input_shape = ncol(train_X)) %>%
layer_dense(units = 1)
summary(modnn)
modnn %>% compile(loss = "mse",
optimizer = optimizer_rmsprop(),
metrics = list("mean_absolute_error"))
The error occurs after running the first epoch. I tought it was because the model could not read the names of the columns, but i tried a lot of things and nothing seemed to work.
Does anyone have an idea on how to fix this?

Multiple Metrics in Keras (in R)

I'm trying to compile a model in Keras (in R) using multiple metrics. The reason for this is to decide which metric works best in evaluating the models created. The code which works for a single metric being:
model %>% compile(optimizer = 'adam', loss = 'mean_squared_error', metrics = metric_mean_absolute_error())
I've seen in python code like
metrics = [metric_mean_absolute_error(), accuracy()]
work well, but in R the brackets give an error. I've tried searching for a way to do this in R, but haven't found luck anywhere. The error is below:
Error in source(file = file, local = envir, echo = echo, encoding = encoding) :
tune.R:30:12: unexpected '['
29: loss = 'mean_squared_error',
30: metrics = [
In R you can create a list with the list() function.
model %>%
compile(
optimizer = 'Adam',
loss = 'mean_squared_error',
metrics = list(metric_mean_absolute_error(),
'accuracy')
)

How to get the flag values used in each tuning run in R when using the R Keras package?

I am trying to tune the hyperparameters of my fully connected deep learning model using flags and tuning_run in R using the keras package. Where do I find the actual flag value used in each run?
I have tried looking for the hyperparameter values used in both the generated result data frame and the runs/ folder. While all the accuracy values, loss function and other meta details about the runs are there, the hyperparameters for which those results are generated are not included (I followed this example given here: https://tensorflow.rstudio.com/tools/tfruns/articles/tuning.html). I am calling my tuning_run as given below
runs <- tuning_run("test.R", flags = list(dropout1=c(0.5,0.4,0.3),dropout2=c(0.3,0.2),dense_units=c(128,256)),sample=0.3)
and my model consumes the flags like
model <- keras_model_sequential()
model %>%
layer_dense(units = 256, activation = 'relu', input_shape = c(784)) %>%
layer_dropout(rate = FLAGS$dropout_1) %>%
layer_dense(units = FLAGS$dense_units, activation = 'relu') %>%
layer_dropout(rate = FLAGS$dropout_2) %>%
layer_dense(units = 10, activation = 'softmax')
When I run it, and later look for the value of the flags for which a certain validation accuracy is generated for (the runs dataframe) This is what I observe
Data frame: 2 x 25
run_dir eval_loss eval_acc metric_loss metric_acc
1 runs/2019-03-29T00-14-10Z 0.1315 0.9794 0.0075 0.9977
2 runs/2019-03-29T00-10-37Z 0.1326 0.9816 0.0096 0.9973
metric_val_loss metric_val_acc
1 0.1475 0.9794
2 0.1443 0.9794
# ... with 18 more columns:
# samples, validation_samples, batch_size, epochs, epochs_completed,
# metrics, model, loss_function, optimizer, learning_rate, script, start,
# end, completed, output, source_code, context, type
I am wondering where to find the flag values used in each iteration. Or am I doing something wrong? Any help would be appreciated. Thanks!
I found out what the problem was. The flags need to be defined in the target script too for keras to report it. And that was why it wasn't showing the flags in the resulting frame.
Once I added these lines to the test.R it worked fine
FLAGS <- flags(
flag_numeric('dropout_1', 0.04, 'First dropout'),
flag_numeric('dropout_2', 0.3, 'Second dropout'),
flag_integer('dense_units', 128, 'Units in dense layer')
)
The same problem and the solution is discussed here: https://github.com/rstudio/tfruns/issues/24

Keras in R, LSTM with flexible inputlength

I'm using Keras with tensorflow backend in R.
I want to create a model that can handle an arbitrary input sequence length.
When I try to define the following model:
model <- keras_model_sequential()
layer_lstm(model, 128, input_shape = c(NULL, 5))
I get the following error:
ValueError: Input 0 is incompatible with layer lstm_3: expected ndim=3, found ndim=2
I guess that it runs into difficulties since the batchsize is already variable. So I could do the following:
model <- keras_model_sequential()
layer_lstm(model, 128, input_shape = c(20, NULL, 5))
This runs without any error. Does this indeed signify a fixed batchsize of 20 a variable sequence length and an input lenght of 5? Or is this just wishfull thinking?

Keras Model doesn't accept input_layer

I am a newbie to Deep Learning, and am trying to build a model in R using Keras. I have 20,000 32x32x3 images stored in an array for model training.When I run:
model = keras_model_sequential()
model %>% layer_input(shape = c(32,32,3))
I get the following error :
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: int() argument must be a string or a number, not 'Sequential'
Detailed traceback:
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 1380, in Input
input_tensor=tensor)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 1287, in __init__
name=self.name)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/contrib/keras/python/keras/backend.py", line 545, in placeholder
x = array_ops.placeholder(dtype, shape=shape, name=name)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1499, in placeholder
shape = tensor_shape.as_shape(shape)
File "/home/abhijit331/.virtualenvs/r-tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 80
can anyone help me figure out how to setup an input layer for my model?
When using Sequential API you don't use the layer_input function.
Your first layer will need to have an input_shape argument that will act as layer_input. For example:
model %>%
layer_dense(units = 32, input_shape = c(784)) %>%
layer_activation('relu') %>%
layer_dense(units = 10) %>%
layer_activation('softmax')
You can use the layer_input function when using the Functional API. See more here.

Resources