Skip to content

Commit

Permalink
Add smoke test for inference crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
vene committed Jul 30, 2017
1 parent 8b4f1a4 commit 0f13c9f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
9 changes: 7 additions & 2 deletions marseille/test_argrnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class ArgDocStub(_BaseArgumentationDoc):

TYPES = ["fact", "value", "policy"]

def __init__(self, n_words=20, n_props=3, random_state=None):
def __init__(self, n_words=20, n_props=3, prop_types=None,
random_state=None):
self.random_state = random_state

rng = check_random_state(self.random_state)
Expand All @@ -35,8 +36,11 @@ def __init__(self, n_words=20, n_props=3, random_state=None):
self.prop_offsets.sort()
self.prop_offsets = self.prop_offsets.reshape(-1, 2)

if prop_types is None:
prop_types = self.TYPES

self._prop_features = [
{'label_': lbl} for lbl in rng.choice(self.TYPES, size=n_props)]
{'label_': lbl} for lbl in rng.choice(prop_types, size=n_props)]

self._features = [
{
Expand All @@ -50,6 +54,7 @@ def __init__(self, n_words=20, n_props=3, random_state=None):

self._link_to_prop = None
self._second_order = None
self.prop_para = np.zeros(len(self.prop_offsets))

def tokens(self, key=None, lower=True):

Expand Down
4 changes: 1 addition & 3 deletions marseille/test_dynet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import dynet as dy

from nose.tools import assert_almost_equal

from marseille.dynet_utils import MultilinearFactored


Expand All @@ -25,4 +23,4 @@ def test_multilinear_forward():
expected *= np.dot(U[2], c)
expected = np.sum(expected)

assert_almost_equal(expected, dy_fwd, 4)
assert (expected - dy_fwd) ** 2 < 1e-4
56 changes: 56 additions & 0 deletions marseille/test_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest
import numpy as np
from marseille.argdoc import DocLabel
from marseille.struct_models import BaseArgumentMixin
from .test_argrnn import ArgDocStub

class InferenceStub(BaseArgumentMixin):
compat_features = False
class_weight = None
def __init__(self, constraints):
self.constraints = constraints
if 'cdcp' in constraints:
self.prop_types = ['fact', 'value', 'policy', 'testimony', 'reference']
else:
self.prop_types = ['Claim', 'MajorClaim', 'Premise']
y_stub = [DocLabel(self.prop_types, [False, True])]
self.initialize_labels(y_stub)

def inference(self, exact=False):

# generate stub doc
rng = np.random.RandomState(0)
n_props = 5
doc = ArgDocStub(prop_types=self.prop_types,
n_props=n_props,
random_state=rng)
# generate random potentials
prop_potentials = rng.randn(n_props, self.n_prop_states)
link_potentials = rng.randn(len(doc.link_to_prop), self.n_link_states)
compat_potentials = rng.randn(self.n_prop_states,
self.n_prop_states,
self.n_link_states)
grandparent_potentials = rng.randn(len(doc.second_order))
coparent_potentials = sibling_potentials = []
potentials = (prop_potentials,
link_potentials,
compat_potentials,
coparent_potentials,
grandparent_potentials,
sibling_potentials)

return self._inference(doc, potentials, return_energy=True,
constraints=self.constraints,
exact=exact)

@pytest.mark.parametrize('constraints', [
'none',
'ukp',
'ukp-strict',
'cdcp',
'cdcp-strict'
])
@pytest.mark.parametrize('exact', [False, True])
def test_smoke_inference(constraints, exact):
# test that inferece runs without errors
y_hat, status, energy = InferenceStub(constraints).inference(exact)

0 comments on commit 0f13c9f

Please sign in to comment.