在 Keras 中向 GloVe 模型添加第二层

时间:2021-01-12 20:06:45

标签: r keras word-embedding

基于以下链接,我能够使用预训练词嵌入构建一个简单的 GloVe 模型:https://keras.rstudio.com/articles/examples/pretrained_word_embeddings.html

使用 Keras 的函数式 API,我使用以下代码定义和训练模型。

# Use Keras Functional API 
main_input <- layer_input(shape = list(maxlen), name = "input")

lstm_out <- main_input %>%
         layer_embedding(input_dim = max_words, 
                         output_dim = dim_size, 
                         input_length = maxlen, 
                         weights = list(word_embeds), trainable = FALSE) %>%
         layer_spatial_dropout_1d(rate = 0.2 ) %>%
         bidirectional(layer_gru(units = 80, return_sequences = TRUE))

max_pool <- lstm_out %>% layer_global_max_pooling_1d()
ave_pool <- lstm_out %>% layer_global_average_pooling_1d()

output <- layer_concatenate(list(ave_pool, max_pool)) %>%
          layer_dense(units = 1, 
                     activation = "sigmoid")

model <- keras_model(input, output)


model %>% compile(
  optimizer = "adam",
  loss = "binary_crossentropy",
  metrics = tensorflow::tf$keras$metrics$AUC()
)

history = model %>% keras::fit(
  x_train, y_train,
  epochs = 8,
  batch_size = 32,
  validation_split = 0.2
)

现在我想向具有三个数值变量的模型添加第二层。根据 keras 文档,必须为此构建一个多输入模型:https://keras.rstudio.com/articles/functional_api.html

我试图以某种方式集成和定义这个新层,但我无法让它工作。

# Define auxiliary_output
auxiliary_output <- lstm_out %>%
                    layer_dense(units = 3, 
                                activation = 'sigmoid', 
                                name = 'aux_output')

# Create auxiliary_input
auxiliary_input <- layer_input(shape = list(maxlen), 
                               name = "aux_input")

# "Stack" both layers on top of each other
main_output <- layer_concatenate(c(lstm_out, auxiliary_input)) %>%
         layer_dense(units = 64, activation = 'relu') %>%
         layer_dense(units = 64, activation = 'relu') %>%
         layer_dense(units = 64, activation = 'relu') %>%
         layer_dense(units = 5, activation = 'sigmoid', name = 'main_output')

# Define model with two in- and outputs
model <- keras_model(inputs = c(data_glove, data_numerical), 
                     outputs = c(output, auxiliary_output ))

这样做会产生以下错误消息:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 60, 160), (None, 60)]

我对此很迷茫,因为我才刚刚开始深入学习深度学习的世界,我将不胜感激。非常感谢。

0 个答案:

没有答案