Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

r - Adding second layer to GloVe model in Keras

Based on the following link I was able to build a simple GloVe model with pretrained word embeddings: https://keras.rstudio.com/articles/examples/pretrained_word_embeddings.html.

Using the functional API of Keras, I used the following code to define and train the model.

# 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
)

Now I would like to add a second layer to the model which has three numerical variables. According to the keras documentation, one has to build a multi input model for this: https://keras.rstudio.com/articles/functional_api.html.

I have tried to somehow integrate and define this new layer but I can't get it to work.

# 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 ))

Doing it this way yields the following error message:

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)]

I'm pretty lost with this and since I am just beginning to dive into the world of deep learning I would appreciate any input on this. Thank you very much.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...