How to adapt input_shape parameter to table dimensions in R

Viewed 261

enter image description hereenter image description hereSo far, I have a net looking like this:

data <- data2018

# Splitting data into train and test
set.seed(101)
sample <- sample.int(n = nrow(data), size = floor(.75 * nrow(data)), replace = F)
train <- as.data.table(data[sample, ])
test <- as.data.table(data[-sample, ])

# Preparing the training set
cols <- c(names(train))
train %<>% mutate_at(cols, as.factor)
train %<>% mutate_at(cols, as.numeric)
train %<>% mutate_at(cols,~(scale(.) %>% as.vector))
train[is.na(train) ] <- 0

# Splitting train into x (parameters) and y (prediction values)
x_train <- data.matrix(train[,-"1"])
y_train <- data.matrix(train[, "1"])

# Designing model
model <- keras_model_sequential() %>%
layer_dense(units = 128, activation = 'tanh', input_shape = dim(x_train)[-1]) %>%
layer_dropout(rate = 0.4) %>%
layer_dense(units = 64, activation = 'tanh') %>%
layer_dropout(rate = 0.3) %>%
layer_dense(units = 32, activation = 'tanh') %>%
layer_dropout(rate = 0.2) %>%
layer_dense(units = 16, activation = 'tanh') %>%
layer_dropout(rate = 0.1) %>%
layer_dense(units = length(unique(data$`1`)), activation = 'softmax')

# Summarize model layers and units
summary(model)

# Compile model
model %>% compile(
loss = 'categorical_crossentropy',
optimizer = optimizer_rmsprop(),
metrics = c('accuracy')
)

# Train model
history <- model %>% fit(
x_train, y_train,
epochs = 100, batch_size = 32,
validation_split = 0.2
)

Due to secrecy issues, I cannot show the original datatable but it is a simple table consisting of 7 rows and 34 columns (only a small simulation chunk).

The input (x_train) is already normalized and vectorized. It is a simple table with rows and columns.

dim(x_train)

Result:

> dim(x_train)
[1]  5 33

However, if I try to train the model, keras prints out this cryptic error message.

Error in py_call_impl(callable, dots$args, dots$keywords) : 
ValueError: in user code:

/home/seschlbeck/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-   packages/tensorflow/python/keras/engine/training.py:855 train_function  *
    return step_function(self, iterator)
/home/seschlbeck/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:845 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/seschlbeck/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1285 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/seschlbeck/.local/share/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2833 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/home/seschlbeck/.local/share/r-miniconda/envs/r-reticulate/lib/python3

This has something to do with my input shape, I guess. But I just can't figure out why... I have already tried out the following ones (all not working)

dim(x_train)
c(5, 33)
c(None, 5, 33)
dim(x_train)[1]

Does anyone have an idea what I am doing wrong here? Any help is appreciated...

1 Answers

The input_shape should be fine. This kind of error message occurs when the number of units in the "output" layer is misspecified. There are likely just two categories in your training sample, i.e., binary classification but your code is for the n-class case where you need n units and categorical cross-entropy loss. Try setting a single unit in the last dense layer:

layer_dense(units = 1, activation = 'softmax')
Related