forked from opensourceware/Neural-ParsCit
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Word embeddings loaded directly (#6)
* Model Evaluation (regression on v1.0.2) * Improved peak memory usage
- Loading branch information
Showing
9 changed files
with
161 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ python: | |
install: | ||
- pip install -r requirements/test.txt | ||
script: | ||
- pytest | ||
- pytest -rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
FROM python:2 | ||
|
||
ENV ENVIRONMENT prod | ||
|
||
WORKDIR /usr/src | ||
|
||
RUN apt-get update \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
-r prod.txt | ||
pylint==1.9.2 | ||
pytest==3.5.1 | ||
-r test.txt | ||
ipython==5.7.0 | ||
git+https://github.com/pytorch/text.git@master | ||
torch==0.4.1 | ||
sklearn==0.19.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import os | ||
import tempfile | ||
import pytest | ||
import requests | ||
import numpy as np | ||
|
||
from model import Model | ||
from loader import load_sentences, prepare_dataset | ||
from utils import create_input | ||
|
||
CORA_URL = "https://raw.githubusercontent.com/knmnyn/ParsCit/master/crfpp/traindata/cora.train" | ||
|
||
# Skip this test when running in CI as the amount of memory is not sufficient | ||
# to build the model | ||
@pytest.mark.skipif(os.getenv("CI") == 'true', reason="Not running in CI") | ||
def test_inference_performance(): | ||
from sklearn.metrics import f1_score | ||
from torchtext.datasets import SequenceTaggingDataset | ||
from torchtext.data import Field, NestedField | ||
|
||
WORD = Field(init_token='<bos>', eos_token='<eos>') | ||
CHAR_NESTING = Field(tokenize=list, init_token='<bos>', eos_token='<eos>') | ||
CHAR = NestedField(CHAR_NESTING, init_token='<bos>', eos_token='<eos>') | ||
ENTITY = Field(init_token='<bos>', eos_token='<eos>') | ||
|
||
data_file = tempfile.NamedTemporaryFile(delete=True) | ||
|
||
# TODO Need to be decoded in Python 3 | ||
data_file.write(requests.get(CORA_URL).content) | ||
|
||
fields = [(('text', 'char'), (WORD, CHAR))] + [(None, None)] * 22 + [('entity', ENTITY)] | ||
|
||
dataset = SequenceTaggingDataset(data_file.name, fields, separator=" ") | ||
|
||
model = Model(model_path='models/neuralParsCit') | ||
model.parameters['pre_emb'] = os.path.join(os.getcwd(), 'vectors_with_unk.kv') | ||
f = model.build(training=False, **model.parameters) | ||
|
||
model.reload() | ||
|
||
word_to_id = {v:i for i, v in model.id_to_word.items()} | ||
char_to_id = {v:i for i, v in model.id_to_char.items()} | ||
tag_to_id = {tag: i for i, tag in model.id_to_tag.items()} | ||
|
||
tf = tempfile.NamedTemporaryFile(delete=False) | ||
tf.write("\n\n".join(["\n".join(example.text) for example in dataset.examples])) | ||
tf.close() | ||
|
||
train_sentences = load_sentences(tf.name, | ||
model.parameters['lower'], | ||
model.parameters['zeros']) | ||
|
||
train_inputs = prepare_dataset(train_sentences, | ||
word_to_id, | ||
char_to_id, | ||
model.parameters['lower'], True) | ||
|
||
preds = [] | ||
|
||
for citation in train_inputs: | ||
inputs = create_input(citation, model.parameters, False) | ||
y_pred = np.array(f[1](*inputs))[1:-1] | ||
|
||
preds.append([(w, y_pred[i]) for i, w in enumerate(citation['str_words'])]) | ||
|
||
assert len(preds) == len(dataset.examples) | ||
|
||
results = [] | ||
|
||
for P, T in zip(preds, dataset.examples): | ||
for p, t in zip(P, zip(T.text, T.entity)): | ||
results.append((p[1], tag_to_id[t[1]])) | ||
|
||
pred, true = zip(*results) | ||
|
||
eval_metrics = { | ||
'micro_f1': f1_score(true, pred, average='micro'), | ||
'macro_f1': f1_score(true, pred, average='macro') | ||
} | ||
|
||
data_file.close() | ||
|
||
assert eval_metrics == pytest.approx({'macro_f1': 0.98, 'micro_f1': 0.99}, abs=0.01) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters