Skip to content

Commit

Permalink
Expanded model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cgpotts committed Mar 31, 2019
1 parent 29edc69 commit e46ce79
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
17 changes: 14 additions & 3 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ def test_np_rnn_classifier(X_sequence):
mod.predict_one_proba(X_test[0])


def test_np_rnn_classifier_simple_example():
@pytest.mark.parametrize("initial_embedding, use_embedding",[
[True, False],
[True, True],
[False, False],
[False, True]
])
def test_np_rnn_classifier_simple_example(initial_embedding, use_embedding):
np_rnn_classifier.simple_example()


Expand Down Expand Up @@ -236,8 +242,13 @@ def test_torch_rnn_classifier_cheese_disease(cheese_disease_dataset):
assert accuracy_score(cheese_disease_dataset['y_test'], pred) > 0.80


@pytest.mark.parametrize("initial_embedding", [True, False])
def test_torch_rnn_classifier_simple_example(initial_embedding):
@pytest.mark.parametrize("initial_embedding, use_embedding",[
[True, False],
[True, True],
[False, False],
[False, True]
])
def test_torch_rnn_classifier_simple_example(initial_embedding, use_embedding):
torch_rnn_classifier.simple_example(initial_embedding)


Expand Down
50 changes: 42 additions & 8 deletions test/test_np_model_gradients.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
from np_autoencoder import Autoencoder
from np_tree_nn import TreeNN
import numpy as np
import pytest
import utils


class GradientCheckError(Exception):
"""Raised if a gradient check fails."""


def test_np_shallow_neural_classifier_gradients():
model = ShallowNeuralClassifier(max_iter=10)
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_shallow_neural_classifier_gradients(hidden_activation, d_hidden_activation):
model = ShallowNeuralClassifier(
max_iter=10,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
X = utils.randmatrix(5, 2)
Expand All @@ -35,15 +43,24 @@ def test_np_shallow_neural_classifier_gradients():
gradient_check(param_pairs, model, ex, label)


def test_np_rnn_classifier():
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_rnn_classifier(hidden_activation, d_hidden_activation):
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
vocab = ['a', 'b', '$UNK']
data = [
[list('ab'), 'good'],
[list('aab'), 'good'],
[list('abb'), 'good']]
model = RNNClassifier(vocab, max_iter=10, hidden_dim=2)
model = RNNClassifier(
vocab,
max_iter=10,
hidden_dim=2,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
X, y = zip(*data)
model.fit(X, y)
# Use the first example for the check:
Expand All @@ -63,8 +80,16 @@ def test_np_rnn_classifier():
gradient_check(param_pairs, model, ex, label)


def test_np_autoencoder():
model = Autoencoder(max_iter=10, hidden_dim=2)
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_autoencoder(hidden_activation, d_hidden_activation):
model = Autoencoder(
max_iter=10,
hidden_dim=2,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
X = utils.randmatrix(5, 5)
Expand All @@ -86,15 +111,24 @@ def test_np_autoencoder():
gradient_check(param_pairs, model, ex, label)


def test_np_tree_nn():
@pytest.mark.parametrize("hidden_activation, d_hidden_activation", [
[np.tanh, utils.d_tanh],
[utils.relu, utils.d_relu]
])
def test_np_tree_nn(hidden_activation, d_hidden_activation):
# A tiny dataset so that we can run `fit` and set all the model
# parameters:
vocab = ["1", "+", "2"]
X = [
"(even (odd 1) (neutral (neutral +) (odd 1)))",
"(odd (odd 1) (neutral (neutral +) (even 2)))"]
X = [Tree.fromstring(ex) for ex in X]
model = TreeNN(vocab, max_iter=10, hidden_dim=5)
model = TreeNN(
vocab,
max_iter=10,
hidden_dim=5,
hidden_activation=hidden_activation,
d_hidden_activation=d_hidden_activation)
model.fit(X)
# Use the first example for the check:
ex = X[0]
Expand Down

0 comments on commit e46ce79

Please sign in to comment.