Keras Bi-LSTM CRF Python至R

时间:2018-07-08 18:14:24

标签: keras sequence lstm crf labeling

我正在尝试创建一个bi-lstm crf,以便在序列建模场景中分配标签。我有很多句子,其中单词以BIO(开头,内部,外部)方案标记,我希望深度神经网络从单词序列和分配的标签中学习,并向前和向后传播和使用条件随机字段。

我正在遵循https://www.depends-on-the-definition.com/sequence-tagging-lstm-crf/中显示的python实现,并尝试在R中实现它。

我到达了对序列进行序列化和填充的位置,并创建了X_Train和Y_Train来馈入深度学习网络。

下面是该模型的Python实现(来自上面的链接)

input = Input(shape=(max_len,))
model = Embedding(input_dim=n_words + 1, output_dim=20,
                  input_length=max_len, mask_zero=True)(input)  # 20-dim embedding
model = Bidirectional(LSTM(units=50, return_sequences=True,
                           recurrent_dropout=0.1))(model)  # variational biLSTM
model = TimeDistributed(Dense(50, activation="relu"))(model)  # a dense layer as suggested by neuralNer
crf = CRF(n_tags)  # CRF layer
out = crf(model)  # output

model = Model(input, out)
model.compile(optimizer="rmsprop", loss=crf.loss_function, metrics=[crf.accuracy])

history = model.fit(X_tr, np.array(y_tr), batch_size=32, epochs=5,
                    validation_split=0.1, verbose=1)

R中将如何实施? 我到目前为止的内容在下面,但似乎不正确。

input <- layer_input(shape = c(max_len,))  ## --- Does not seem correct --

output <- input %>%
  layer_embedding(input_dim = n_words + 1, output_dim = 20, input_length=max_len, mask_zero = TRUE) %>%
  bidirectional(layer_lstm(units=50, return_sequences = TRUE,recurrent_dropout = 0.1)) %>%
  time_distributed(layer_dense(units = 50, activation = "relu"))
         ## -- Dont know how to create a CRF layer

model <- keras_model(input, output)

model %>% compile(
  optimizer = "rmsprop",
  loss = "categorical_crossentropy"
)

summary(model)
model %>% fit(S1S2S3XTrain, S1S2S3YTrain, epochs = 5, batch_size = 32)
model %>% evaluate(x_train, y_train)

请帮助。

0 个答案:

没有答案