Keras Model doesn't accept input_layer - r

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.

Related

How to extract the output of an intermediate layer in Keras [for R]?

I am having trouble extracting the output of an intermediate layer in a Keras NN model. Although there is a ton of resource on this most of them are geared towards for python users. I have tried working towards the tutorial posted here and here. Essentially both of the tutorial says to recreate the original model and then somehow extract the intermediate layers by simply calling it. The issue I am having is model$input returns nothing in my code and I have no idea where layer_name is in a model. After playing around with this method I tried creating a new model to the layer I am interested in and put in the trained weights of the original model in order to get the intermediate layer output. However, I found myself at a lack of resource for R users on how to place weights into a new model. I would appreciate if anyone would help with walking through a concrete example on how to do either method or a better one. Here is my original model:
network <- keras_model_sequential()
network %>%
layer_dropout(rate = 0.1) %>%
layer_dense(units = 64, activation = "relu", input_shape = length(training_data) ) %>%
layer_dense(units = 8, activation = "sigmoid" ) %>%
layer_dense(units = 64, activation = "relu" ) %>%
layer_dense(units = length(training_data), activation = "softmax")

R.keras error during call to fit function

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

Input shape in keras (This loss expects targets to have the same shape as the output)

this is my first time using keras, I'm trying to follow a tutorial I've found online and fit my own data to it. I have a matrix and binary labels.
> str(d_train)
num [1:1062, 1:180] -0.04748 0.04607 -0.05429 -0.0126 -0.00219 ...
> str(trainlabels)
num [1:1062, 1:2] 0 0 0 0 0 0 1 0 0 0 ...
my code:
model = keras_model_sequential()
model %>%
layer_dense(units = 8, activation = 'relu', input_shape = c(180)) %>%
layer_dense(units = 3, activation = "softmax")
summary(model)
## Compile
model %>%
compile(loss = "binary_crossentropy",
optimizer = "adam",
metrics = "accuracy")
## Fit model
history = model %>%
fit(d_train,
trainlabels,
epoch=200,
batch_size=32,
validation_split=0.2)
I can't seem to fit the model, I'm getting this error message:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: A target array with shape (1062, 2) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.
Based on the error message, asking for different shape of my input array, I tried to change the dimensions around with no luck.
I am not an R expert, but here:
layer_dense(units = 3, activation = "softmax")
You are telling Keras that the output of your network has three classes. Your labels have shape (1062, 2) which suggest it has two classes, hence there is an inconsistency.
You could just change units = 2 in your last dense and it should work. Also note that you are using the softmax activation, and in that case you should prefer to use the categorical_crossentropy loss.
To use binary_crossentropy for binary classification, you should have units = 1, sigmoid activation, and labels should be (1062, 1) or (1062,), which means they are 0-1 encoded.

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?

Resources