Trying to solve data dimension mismatch in keras - r

I'm working on testing a model before I let it rip on a full dataset. My data is RGB images in an array, so, my training dataset currently has the dimensions
> dim(ff_train)
[1] 10 500 500 3
So, 10 images, each 500x500 with 3 color layers (RGB).
My test data is the same
> dim(ff_test)
[1] 10 500 500 3
I've setup my model like so:
model <- keras_model_sequential() %>%
layer_dense(units = 16, activation = "relu",
input_shape = c(10)) %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 1, activation = "sigmoid")
model %>% compile(
optimizer = "rmsprop",
loss = "binary_crossentropy",
metrics = c("accuracy")
)
history <- model %>% fit(
x = ff_train,
y = ff_train_labels$fraction_yes,
epochs = 20,
validation_data = list(ff_test, ff_test_labels$fraction_yes))
where input shape is 10 as I have 10 images. I also have 10 labels for each which are numbers in a numeric vector between 0 and 1 (fraction of an event occurring in a sample) - both are of length 10.
However, when I run the model, I get the error
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: in user code:
which, after following googling around led me to https://github.com/rstudio/keras/issues/1063 stating that the problem is a mismatch in my dimensions or structure between train and test which.... seems incorrect?
What am I missing here? Where is the dimensional mismatch?

your input is image, so you need to use Convolution layer as first layer, after which you need flatten layer, this will be before Dense layer.
without flatten layer your output is 4 dimension, but you need 2 dimension output.
your output is dimension is 10,500,500,1
but you need 10,10
your final dense layer should have 10 neurons.

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?

In which form should the input and the label data be needed into Keras fit function?

I am trying to train a sequential classifier, with 1 input neuron, 3 output neurons. The data is in data frames X and Y, but how must I feed this data into fit function in keras library? In other words, what should be the variable type of train_x and train_y (for example, is it data frame, matrix, list, etc)?
[...]
predictor <- keras_model_sequential() %>%
layer_dense(units = 8, activation = "relu", input_shape = c(1)) %>%
layer_dense(units = 8, activation = "relu") %>%
layer_dense(units = 3, activation = "softmax")
[...]
train_x <- X
train_y <- Y
history <- predictor %>% fit(
train_x,
train_y,
epochs = 20,
verbose = 2
)
Edit:
If I can use dataframe, then how should I set input_shape?
The variable type for fit should be of vector, matrix, or array.
As per the documentation, it states below,
x -
Vector, matrix, or array of training data (or list if the model has multiple inputs). If all inputs in the model are named, you can also pass a list mapping input names to data. x can be NULL (default) if feeding from framework-native tensors (e.g. TensorFlow data tensors).
y - Vector, matrix, or array of target (label) data (or list if the
model has multiple outputs). If all outputs in the model are named,
you can also pass a list mapping output names to data. y can be NULL
(default) if feeding from framework-native tensors (e.g. TensorFlow
data tensors).
The model needs to know what input shape it should expect. For this reason, the first layer in a sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. Ex: You can pass a batch_size argument to a layer. If you pass both batch_size=32 and input_shape=c(6, 8) to a layer, it will then expect every batch of inputs to have the batch shape (32, 6, 8).
Hope this answers your question. Happy Learning.

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

Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (3653, 3)

I am trying to learn LSTM with keras in R. I am not being able to fully understand the conventions used in keras.
I have dataset that looks like below, with the first 3 columns considered as input and the last one as output.
Based on this, I am trying to build a stateless LSTM as follows:
model %>%
layer_lstm(units = 1024, input_shape = c(1, 3), return_sequences = T ) %>%
layer_lstm(units = 1024, return_sequences = F) %>%
# using linear activation on last layer, as output is needed in real number
layer_dense(units = 1, activation = "linear")
model %>% compile(loss = 'mse', optimizer = 'rmsprop')
The model looks like below
Layer (type) Output Shape Param #
=====================================================
lstm_1 (LSTM) (None, 1, 1024) 4210688
_____________________________________________________
lstm_2 (LSTM) (None, 1024) 8392704
_____________________________________________________
dense_3 (Dense) (None, 1) 1025
=====================================================
Total params: 12,604,417
Trainable params: 12,604,417
Non-trainable params: 0
_____________________________________________________
I am trying to train the model as follows:
history <- model %>% fit(dt[,1:3], dt[,4], epochs=50, shuffle=F)
However, i am getting the following error when I try to execute the code.
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (3653, 3)
Not sure what I am missing here.
Update: After looking around in internet, it seems that I need to reshape the dataset into a 3 dimensional (batchsize, timestep, #features) array. However, I am not using any batch, thus not sure how to reshape my data.
Update on 29.01.2018: This is what worked for me. I used input_shape = c(1, 3) in my first LSTM layer, as I have 3 features and I am not using any batch. Thus, I also ended up reshaping my data using the following function:
reshapeDt <- function(data){ # data is the original train matrix (training dataset)
rows <- nrow(data)
cols <- ncol(data)-1
dt <- array(dim=c(rows, 1, cols))
for(i in 1:rows){
dt[i,1,] <- data[i,1:cols]
}
dt
}
This means that the call to fit looks like below:
model %>% fit(reshapeDt(dt), dt[,4], epochs=50, shuffle=F)
This means that dim(reshapeDt(dt)) returns number_of_rows_in_dt 1 3.
Input shapes for LSTM layers should be (batch, time_steps, features).
You must organize your data to have this shape.
It seems that you have only one sequence, with 6 time steps, and 3 features. So, input_shape=(6,3). You can actually use (None,3) for sequences with variable length.
Your input array dt should have shape (1,length,3).

Resources