Skip to content

Commit

Permalink
Ported code of BiLSTM to Keras 2.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Nils Reimers committed Jul 13, 2017
1 parent 97c189e commit 2092dcf
Show file tree
Hide file tree
Showing 21 changed files with 654,139 additions and 538 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@


# Create the train and predict_labels function
n_in = train_tokens.shape[1]
n_out = len(label2Idx)
n_in = train_tokens.shape[1] # Length of the input, i.e. 7 for a window size of 7
n_out = len(label2Idx) # Number of output labels

words_input = Input(shape=(n_in,), dtype='int32', name='words_input')
words = Embedding(input_dim=wordEmbeddings.shape[0], output_dim=wordEmbeddings.shape[1], input_length=n_in, weights=[wordEmbeddings], trainable=False)(words_input)
words = Embedding(input_dim=wordEmbeddings.shape[0], output_dim=wordEmbeddings.shape[1], weights=[wordEmbeddings], trainable=False)(words_input)
words = Flatten()(words)


casing_input = Input(shape=(n_in,), dtype='int32', name='casing_input')
casing = Embedding(input_dim=caseEmbeddings.shape[0], output_dim=caseEmbeddings.shape[1], input_length=n_in, weights=[caseEmbeddings], trainable=False)(casing_input)
casing = Embedding(input_dim=caseEmbeddings.shape[0], output_dim=caseEmbeddings.shape[1], weights=[caseEmbeddings], trainable=False)(casing_input)
casing = Flatten()(casing)

output = concatenate([words, casing])
Expand Down
111 changes: 0 additions & 111 deletions 2017-07_Seminar/Session 1 - SENNA/code for NER/NER_template.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@


# Create the train and predict_labels function
n_in = train_tokens.shape[1]
n_out = len(label2Idx)
n_in = train_tokens.shape[1] # Length of the input, i.e. 7 for a window size of 7
n_out = len(label2Idx) # Number of output labels

words_input = Input(shape=(n_in,), dtype='int32', name='words_input')
words = Embedding(input_dim=wordEmbeddings.shape[0], output_dim=wordEmbeddings.shape[1], input_length=n_in, weights=[wordEmbeddings], trainable=False)(words_input)
words = Embedding(input_dim=wordEmbeddings.shape[0], output_dim=wordEmbeddings.shape[1], weights=[wordEmbeddings], trainable=False)(words_input)
words = Flatten()(words)


casing_input = Input(shape=(n_in,), dtype='int32', name='casing_input')
casing = Embedding(input_dim=caseEmbeddings.shape[0], output_dim=caseEmbeddings.shape[1], input_length=n_in, weights=[caseEmbeddings], trainable=False)(casing_input)
casing = Embedding(input_dim=caseEmbeddings.shape[0], output_dim=caseEmbeddings.shape[1], weights=[caseEmbeddings], trainable=False)(casing_input)
casing = Flatten()(casing)

output = concatenate([words, casing])
Expand Down Expand Up @@ -115,11 +115,12 @@ def predict_classes(prediction):
print("\n------------- Epoch %d ------------" % (epoch+1))
model.fit([train_tokens, train_case], train_y, epochs=1, batch_size=minibatch_size, verbose=True, shuffle=True)


#Predict labels for development set
dev_pred = predict_classes(model.predict([dev_tokens, dev_case]))
dev_acc = np.sum(dev_pred == dev_y) / float(len(dev_y))
print("Dev-Accuracy: %.2f" % (dev_acc*100))

#Predict labels for test set
test_pred = predict_classes(model.predict([test_tokens, test_case]))
test_acc = np.sum(test_pred == test_y) / float(len(test_y))
print("Test-Accuracy: %.2f" % (test_acc*100))
Expand Down
109 changes: 0 additions & 109 deletions 2017-07_Seminar/Session 1 - SENNA/code for POS/POS_template.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def getCasing(word, caseLookup):
basename = os.path.basename(embeddingsPath)
if basename == 'wiki_extvec.gz':
print("Start downloading word embeddings for English using wget ...")
#os.system("wget https://www.cs.york.ac.uk/nlp/extvec/"+basename+" -P embeddings/")
#os.system("wget https://www.cs.york.ac.uk/nlp/extvec/"+basename+" -P embeddings/") #Original path from York University
os.system("wget https://public.ukp.informatik.tu-darmstadt.de/reimers/2017_english_embeddings/"+basename+" -P embeddings/")
else:
print(embeddingsPath, "does not exist. Please provide pre-trained embeddings")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def wordIdxLookup(word, word_idx_map):

#Our word embedding layer
wordsEmbeddingLayer = Embedding(word_embeddings.shape[0],
word_embeddings.shape[1],
input_length=max_sentence_len,
word_embeddings.shape[1],
weights=[word_embeddings],
trainable=False)

Expand All @@ -152,7 +151,7 @@ def wordIdxLookup(word, word_idx_map):



# We add a vanilla hidden layer:
# We add a vanilla hidden layer together with dropout layers:
output = Dropout(0.5)(output)
output = Dense(hidden_dims, activation='tanh', kernel_regularizer=keras.regularizers.l2(0.01))(output)
output = Dropout(0.25)(output)
Expand All @@ -162,16 +161,15 @@ def wordIdxLookup(word, word_idx_map):
output = Dense(1, activation='sigmoid', kernel_regularizer=keras.regularizers.l2(0.01))(output)

model = Model(inputs=[words_input], outputs=[output])
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.summary()

for epoch in range(nb_epoch):
print("\n------------- Epoch %d ------------" % (epoch+1))
model.fit(X_train, y_train, batch_size=batch_size, epochs=1)

#Use Keras to compute the loss and the accuracy
dev_loss, dev_accuracy = model.evaluate(X_dev, y_dev, verbose=False)
test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=False)

Expand Down
Loading

0 comments on commit 2092dcf

Please sign in to comment.