如何使用来自RStudio的R包keras将序列拟合到序列模型中?

时间:2017-06-16 07:12:18

标签: r rstudio keras lstm

这个问题是关于R和来自Rstudio的包keras。 (https://github.com/rstudio/keras

我正在尝试学习一个模型来标记序列的某些部分。 我希望模型能够做到这样的事情:[64,34,77,33,88] - > [0,0,1,1,0] 所以,在输入中我有一个用pad_sequences生成的序列矩阵(每行1个序列),如下所示:

int [1:21885, 1:30] 21 21 1506 28 102 21 61 224 15 15 ...

,输出也是由pad_sequences生成的序列矩阵:

int [1:21885, 1:30] 0 0 1 0 0 0 1 1 0 0 ...

这是模拟我使用的输入/输出形状的代码:

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)

但是当我尝试运行时,我收到了这个错误:

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)

所以我尝试将输出向量转换为包含to_categorical的二维矩阵列表 像这样:

tags_train_cat <- lapply(1:nrow(tag_train), function(x) (to_categorical(tag_train[x,])))

然后我的新目标看起来像这样:

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 ...
...

但知道我收到了这个错误:

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

所以,我的问题是:我做错了什么?

1 个答案:

答案 0 :(得分:0)

求助:

即使输入(X)必须是二维数组(由于嵌入), 输出(Y)必须是具有尺寸(样本,序列长度,特征)的3维形状。这里的功能= 1.