Skip to content
Snippets Groups Projects
Commit 723a0302 authored by Alex Rubinsteyn's avatar Alex Rubinsteyn
Browse files

added tests for construction of neural nets

parent 5bbf2a3d
No related branches found
No related tags found
No related merge requests found
......@@ -27,28 +27,6 @@ import theano
theano.config.exception_verbosity = 'high'
def compile_forward_predictor(model, theano_mode=None):
"""
In cases where we want to get predictions from a model that hasn't
been compiled (to avoid overhead of compiling training code),
use this helper to only compile the subset of Theano needed for
forward-propagation/predictions.
"""
model.X_test = model.get_input(train=False)
model.y_test = model.get_output(train=False)
if type(model.X_test) == list:
predict_ins = model.X_test
else:
predict_ins = [model.X_test]
model._predict = theano.function(
predict_ins,
model.y_test,
allow_input_downcast=True,
mode=theano_mode)
def make_network(
input_size,
embedding_input_dim=None,
......@@ -61,8 +39,7 @@ def make_network(
dropout_probability=0.0,
model=None,
optimizer=None,
learning_rate=0.001,
compile_for_training=True):
learning_rate=0.001):
if model is None:
model = Sequential()
......@@ -112,16 +89,13 @@ def make_network(
output_dim=1,
init=init))
model.add(Activation(output_activation))
if compile_for_training:
model.compile(loss=loss, optimizer=optimizer)
else:
compile_forward_predictor(model)
model.compile(loss=loss, optimizer=optimizer)
return model
def make_hotshot_network(
peptide_length=9,
layer_sizes=[500],
layer_sizes=[100],
activation="relu",
init="lecun_uniform",
loss="mse",
......@@ -146,7 +120,7 @@ def make_embedding_network(
peptide_length=9,
embedding_input_dim=20,
embedding_output_dim=20,
layer_sizes=[500],
layer_sizes=[100],
activation="relu",
init="lecun_uniform",
loss="mse",
......
......@@ -25,7 +25,6 @@ from .data import (
create_allele_data_from_peptide_to_ic50_dict,
)
def prune_dense_matrix_and_labels(
X,
peptide_list,
......
from mhcflurry.feedforward import (
make_embedding_network,
make_hotshot_network,
)
import numpy as np
def test_make_embedding_network():
nn = make_embedding_network(
peptide_length=3,
layer_sizes=[3],
activation="tanh",
embedding_input_dim=3,
embedding_output_dim=20,
learning_rate=0.1)
X_negative = np.array([
[0] * 3,
[1] * 3,
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
])
X_positive = np.array([
[0, 2, 0],
[1, 2, 0],
[1, 2, 1],
[0, 2, 1],
[2, 2, 0],
[2, 2, 1],
[2, 2, 2],
])
X_index = np.vstack([X_negative, X_positive])
Y = np.array([0.0] * len(X_negative) + [1.0] * len(X_positive))
nn.fit(X_index, Y, nb_epoch=10)
Y_pred = nn.predict(X_index)
print(Y)
print(Y_pred)
for (Y_i, Y_pred_i) in zip(Y, Y_pred):
assert abs(Y_i - Y_pred_i) <= 0.25, (Y_i, Y_pred_i)
def test_make_hotshot_network():
nn = make_hotshot_network(
peptide_length=3,
activation="relu",
layer_sizes=[4],
n_amino_acids=2,
learning_rate=0.1)
X_binary = np.array([
[True, False, True, False, True, False],
[True, False, True, False, False, True],
[True, False, False, True, True, False],
[True, False, False, True, False, True],
[False, True, True, False, True, False],
[False, True, True, False, False, True],
], dtype=bool)
Y = np.array([0.0, 0.0, 0.0, 0.0, 1.0, 1.0])
nn.fit(X_binary, Y, nb_epoch=10)
Y_pred = nn.predict(X_binary)
print(Y)
print(Y_pred)
for (Y_i, Y_pred_i) in zip(Y, Y_pred):
assert abs(Y_i - Y_pred_i) <= 0.25, (Y_i, Y_pred_i)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment