Hi guys,
I am trying to learn a model to label some parts of sequences.
I would like the model to do something like: [64,34,77,33,88] -> [0,0,1,1,0]
So, in input I have a matrix of sequences (1 sequence per row) produced with pad_sequences
which looks like this:
int [1:21885, 1:30] 21 21 1506 28 102 21 61 224 15 15 ...
and the output is also a matrix of sequences produced by pad_sequences
:
int [1:21885, 1:30] 0 0 1 0 0 0 1 1 0 0 ...
here is the code to simulate the shape of inputs/outputs I use:
input_length = 30
n_sample = 5
vocab_size = 100
quest_train <- matrix(floor(runif(input_length*n_sample, 1,vocab_size)), ncol = input_length)
tag_train <- matrix(sample(c(0,1), size = input_length*n_sample, replace = T), ncol = input_length)
input_dim = vocab_size
embed_dim = 50
model <- keras_model_sequential()
model %>%
layer_embedding(input_dim = input_dim,
output_dim = embed_dim) %>%
layer_dropout(rate = 0.2) %>%
layer_lstm(units = 128, return_sequences = T) %>%
layer_dropout(rate = 0.5) %>%
time_distributed(layer_dense(units = 2, activation = 'softmax'))
model %>%
compile(loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = c('accuracy'))
model %>% fit(quest_train,
tag_train,
batch_size = 16 ,
epochs = 10,
shuffle = TRUE)
But when I try to run this I get this error:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Error when checking model target: expected time_distributed_23 to have 3 dimensions, but got array with shape (5, 30)
So I tried to convert the output vector to a list of 2 dimensional matrices with to_categorical
like this:
tags_train_cat <- lapply(1:nrow(tag_train), function(x) (to_categorical(tag_train[x,])))
then my new target looks like this:
List of 5
$ : num [1:30, 1:2] 1 1 1 1 1 0 1 1 1 1 ...
$ : num [1:30, 1:2] 1 1 1 1 1 0 0 1 1 1 ...
$ : num [1:30, 1:2] 0 1 1 1 1 1 1 1 1 1 ...
$ : num [1:30, 1:2] 1 1 1 1 1 1 0 0 0 0 ...
...
but know I get this error:
ValueError: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of X arrays
So, my question is: What do I do wrong ?