【问题标题】:How to fit a sequence to sequence model with the R package keras from RStudio?如何使用 RStudio 中的 R 包 keras 拟合序列到序列模型?
【发布时间】:2017-06-16 07:12:17
【问题描述】:

这个问题是关于 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

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

【问题讨论】:

  • 我找不到名为 keras 的包。有一个kerasR,但不是由 Rstudio 提供的。你能澄清一下吗?
  • 你可以在这里找到它:github.com/rstudio/keras

标签: r rstudio keras lstm


【解决方案1】:

已解决:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 2021-01-25
    • 2022-12-29
    • 2021-07-04
    相关资源
    最近更新 更多