Commit 86a93af4 authored by miaecle's avatar miaecle
Browse files

unit tests

parent 96de0a46
Loading
Loading
Loading
Loading
+113 −32
Original line number Diff line number Diff line
@@ -309,11 +309,11 @@ class WeaveGather(Layer):
                            (0.228, 0.114), (0.468, 0.118), (0.739, 0.134),
                            (1.080, 0.170), (1.645, 0.283)]
    dist = [
        tf.contrib.distributions.Normal(mu=p[0], sigma=p[1])
        tf.contrib.distributions.Normal(loc=p[0], scale=p[1])
        for p in gaussian_memberships
    ]
    dist_max = [dist[i].pdf(gaussian_memberships[i][0]) for i in range(11)]
    outputs = [dist[i].pdf(x) / dist_max[i] for i in range(11)]
    dist_max = [dist[i].prob(gaussian_memberships[i][0]) for i in range(11)]
    outputs = [dist[i].prob(x) / dist_max[i] for i in range(11)]
    outputs = tf.stack(outputs, axis=2)
    outputs = outputs / tf.reduce_sum(outputs, axis=2, keep_dims=True)
    outputs = tf.reshape(outputs, [-1, self.n_input * 11])
@@ -373,8 +373,20 @@ class DTNNEmbedding(Layer):
    self.build()
    atom_number = in_layers[0].out_tensor
    atom_features = tf.nn.embedding_lookup(self.embedding_list, atom_number)
    if set_tensors:
      self.variables = self.trainable_weights
      self.out_tensor = atom_features

  def none_tensors(self):
    embedding_list = self.embedding_list
    self.embedding_list = None
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return embedding_list, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.embedding_list, self.out_tensor, self.trainable_weights, self.variables = tensor


class DTNNStep(Layer):
  """ TensorGraph style implementation
@@ -462,6 +474,16 @@ class DTNNStep(Layer):
      self.out_tensor = out_tensor
    return out_tensor

  def none_tensors(self):
    W_cf, W_df, W_fc, b_cf, b_df = self.W_cf, self.W_df, self.W_fc, self.b_cf, self.b_df
    self.W_cf, self.W_df, self.W_fc, self.b_cf, self.b_df = None, None, None, None, None
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return W_cf, W_df, W_fc, b_cf, b_df, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.W_cf, self.W_df, self.W_fc, self.b_cf, self.b_df, self.out_tensor, self.trainable_weights, self.variables = tensor


class DTNNGather(Layer):
  """ TensorGraph style implementation
@@ -541,6 +563,16 @@ class DTNNGather(Layer):
      self.out_tensor = out_tensor
    return out_tensor

  def none_tensors(self):
    W_list, b_list = self.W_list, self.b_list
    self.W_list, self.b_list = [], []
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return W_list, b_list, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.W_list, self.b_list, self.out_tensor, self.trainable_weights, self.variables = tensor


class DTNNExtract(Layer):

@@ -706,6 +738,16 @@ class DAGLayer(Layer):
      outputs = self.activation(outputs)
    return outputs

  def none_tensors(self):
    W_list, b_list = self.W_list, self.b_list
    self.W_list, self.b_list = [], []
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return W_list, b_list, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.W_list, self.b_list, self.out_tensor, self.trainable_weights, self.variables = tensor


class DAGGather(Layer):
  """ TensorGraph style implementation
@@ -800,6 +842,16 @@ class DAGGather(Layer):
      outputs = self.activation(outputs)
    return outputs

  def none_tensors(self):
    W_list, b_list = self.W_list, self.b_list
    self.W_list, self.b_list = [], []
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return W_list, b_list, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.W_list, self.b_list, self.out_tensor, self.trainable_weights, self.variables = tensor


class MessagePassing(Layer):
  """ General class for MPNN
@@ -873,6 +925,19 @@ class MessagePassing(Layer):
      self.out_tensor = out_tensor
    return out_tensor

  def none_tensors(self):
    message_tensors = self.message_function.none_tensors()
    update_tensors = self.update_function.none_tensors()
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return message_tensors, update_tensors, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    message_tensors, update_tensors, self.out_tensor, self.trainable_weights, self.variables = tensor
    self.message_function.set_tensors(message_tensors)
    self.update_function.set_tensors(update_tensors)


class EdgeNetwork(object):
  """ Submodule for Message Passing """

@@ -896,6 +961,16 @@ class EdgeNetwork(object):
    out = tf.segment_sum(out, atom_to_pair[:, 0])
    return out

  def none_tensors(self):
    A = self.A
    self.A = None,
    trainable_weights = self.trainable_weights
    self.trainable_weights = []
    return A, trainable_weights

  def set_tensors(self, tensor):
    self.A, self.trainable_weights = tensor


class GatedRecurrentUnit(object):
  """ Submodule for Message Passing """
@@ -903,30 +978,37 @@ class GatedRecurrentUnit(object):
  def __init__(self, n_hidden=100, init='glorot_uniform'):
    self.n_hidden = n_hidden
    self.init = initializations.get(init)
    self.Wz = self.init([n_hidden, n_hidden])
    self.Wr = self.init([n_hidden, n_hidden])
    self.Wh = self.init([n_hidden, n_hidden])
    self.Uz = self.init([n_hidden, n_hidden])
    self.Ur = self.init([n_hidden, n_hidden])
    self.Uh = self.init([n_hidden, n_hidden])
    self.bz = model_ops.zeros(shape=(n_hidden,))
    self.br = model_ops.zeros(shape=(n_hidden,))
    self.bh = model_ops.zeros(shape=(n_hidden,))
    self.trainable_weights = [
        self.Wz, self.Wr, self.Wh, self.Uz, self.Ur, self.Uh, self.bz, self.br,
        self.bh
    ]
    Wz = self.init([n_hidden, n_hidden])
    Wr = self.init([n_hidden, n_hidden])
    Wh = self.init([n_hidden, n_hidden])
    Uz = self.init([n_hidden, n_hidden])
    Ur = self.init([n_hidden, n_hidden])
    Uh = self.init([n_hidden, n_hidden])
    bz = model_ops.zeros(shape=(n_hidden,))
    br = model_ops.zeros(shape=(n_hidden,))
    bh = model_ops.zeros(shape=(n_hidden,))
    self.trainable_weights = [Wz, Wr, Wh, Uz, Ur, Uh, bz, br, bh]

  def forward(self, inputs, messages):
    z = tf.nn.sigmoid(tf.matmul(messages, self.Wz) + \
                      tf.matmul(inputs, self.Uz) + self.bz)
    r = tf.nn.sigmoid(tf.matmul(messages, self.Wr) + \
                      tf.matmul(inputs, self.Ur) + self.br)
    h = (1-z) * tf.nn.tanh(tf.matmul(messages, self.Wh) + \
                           tf.matmul(inputs * r, self.Uh) + self.bh) + \
         z * inputs
    z = tf.nn.sigmoid(tf.matmul(messages, self.trainable_weights[0]) + \
                      tf.matmul(inputs, self.trainable_weights[3]) + \
                      self.trainable_weights[6])
    r = tf.nn.sigmoid(tf.matmul(messages, self.trainable_weights[1]) + \
                      tf.matmul(inputs, self.trainable_weights[4]) + \
                      self.trainable_weights[7])
    h = (1-z) * tf.nn.tanh(tf.matmul(messages, self.trainable_weights[2]) + \
                           tf.matmul(inputs * r, self.trainable_weights[5]) + \
                           self.trainable_weights[8]) + z * inputs
    return h

  def none_tensors(self):
    trainable_weights = self.trainable_weights
    self.trainable_weights = []
    return trainable_weights

  def set_tensors(self, tensor):
    self.trainable_weights = tensor


class SetGather(Layer):
  """ set2set gather layer for graph-based model
@@ -1005,13 +1087,12 @@ class SetGather(Layer):

    return h_out, c_out


  def none_tensors(self):
    self.out_tensor = None
    self.h = None
    self.c = None
    saved_tensors = [self.out_tensor, self.h, self.c]
    return saved_tensors

  def set_tensors(self, tensors):
    self.out_tensor, self.h, self.c = tensors
    U, b, c, h = self.U, self.b, self.c, self.h
    self.U, self.b, self.c, self.h = None, None, None, None
    out_tensor, trainable_weights, variables = self.out_tensor, self.trainable_weights, self.variables
    self.out_tensor, self.trainable_weights, self.variables = None, [], []
    return U, b, c, h, out_tensor, trainable_weights, variables

  def set_tensors(self, tensor):
    self.U, self.b, self.c, self.h, self.out_tensor, self.trainable_weights, self.variables = tensor
+1 −1
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ class WeaveTensorGraph(TensorGraph):
    weave_gather = WeaveGather(
        self.batch_size,
        n_input=self.n_graph_feat,
        guassian_expand=True,
        gaussian_expand=True,
        in_layers=[batch_norm1, self.atom_split])

    costs = []
+137 −0
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@ from deepchem.models.tensorgraph.layers import Feature, Conv1D, Dense, Flatten,
    CombineMeanStd, Repeat, GRU, L2Loss, Concat, SoftMax, Constant, Variable, Add, Multiply, InteratomicL2Distances, \
    SoftMaxCrossEntropy, ReduceMean, ToFloat, ReduceSquareDifference, Conv2D, MaxPool, ReduceSum, GraphConv, GraphPool, \
    GraphGather, BatchNorm, WeightedError
from deepchem.models.tensorgraph.graph_layers import Combine_AP, Separate_AP, \
    WeaveLayer, WeaveGather, DTNNEmbedding, DTNNGather, DTNNStep, \
    DTNNExtract, DAGLayer, DAGGather, MessagePassing, SetGather


def test_Conv1D_pickle():
@@ -313,3 +316,137 @@ def test_WeightedError_pickle():
  tg.set_loss(layer)
  tg.build()
  tg.save()


def test_Combine_Separate_AP_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 10))
  pair_feature = Feature(shape=(None, 5))
  C_AP = Combine_AP(in_layers=[atom_feature, pair_feature])
  S_AP = Separate_AP(in_layers=[C_AP])
  tg.add_output(S_AP)
  tg.set_loss(S_AP)
  tg.build()
  tg.save()


def test_Weave_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 75))
  pair_feature = Feature(shape=(None, 14))
  pair_split = Feature(shape=(None,), dtype=tf.int32)
  atom_to_pair = Feature(shape=(None, 2), dtype=tf.int32)
  C_AP = Combine_AP(in_layers=[atom_feature, pair_feature])
  weave = WeaveLayer(in_layers=[C_AP, pair_split, atom_to_pair])
  tg.add_output(weave)
  tg.set_loss(weave)
  tg.build()
  tg.save()


def test_WeaveGather_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 75))
  atom_split = Feature(shape=(None,), dtype=tf.int32)
  weave_gather = WeaveGather(
      32, gaussian_expand=True, in_layers=[atom_feature, atom_split])
  tg.add_output(weave_gather)
  tg.set_loss(weave_gather)
  tg.build()
  tg.save()


def test_DTNNEmbedding_pickle():
  tg = TensorGraph()
  atom_numbers = Feature(shape=(None, 23), dtype=tf.int32)
  Embedding = DTNNEmbedding(in_layers=[atom_numbers])
  tg.add_output(Embedding)
  tg.set_loss(Embedding)
  tg.build()
  tg.save()


def test_DTNNStep_pickle():
  tg = TensorGraph()
  atom_features = Feature(shape=(None, 30))
  distance = Feature(shape=(None, 100))
  distance_membership_i = Feature(shape=(None,), dtype=tf.int32)
  distance_membership_j = Feature(shape=(None,), dtype=tf.int32)
  DTNN = DTNNStep(in_layers=[
      atom_features, distance, distance_membership_i, distance_membership_j
  ])
  tg.add_output(DTNN)
  tg.set_loss(DTNN)
  tg.build()
  tg.save()


def test_DTNNGather_pickle():
  tg = TensorGraph()
  atom_features = Feature(shape=(None, 30))
  atom_membership = Feature(shape=(None,), dtype=tf.int32)
  Gather = DTNNGather(in_layers=[atom_features, atom_membership])
  tg.add_output(Gather)
  tg.set_loss(Gather)
  tg.build()
  tg.save()


def test_DTNNExtract_pickle():
  tg = TensorGraph()
  atom_features = Feature(shape=(None, 30))
  Ext = DTNNExtract(0, in_layers=[atom_features])
  tg.add_output(Ext)
  tg.set_loss(Ext)
  tg.build()
  tg.save()


def test_DAGLayer_pickle():
  tg = TensorGraph(use_queue=False)
  atom_features = Feature(shape=(None, 75))
  parents = Feature(shape=(None, 50, 50), dtype=tf.int32)
  calculation_orders = Feature(shape=(None, 50), dtype=tf.int32)
  calculation_masks = Feature(shape=(None, 50), dtype=tf.bool)
  n_atoms = Feature(shape=(), dtype=tf.int32)
  DAG = DAGLayer(in_layers=[
      atom_features, parents, calculation_orders, calculation_masks, n_atoms
  ])
  tg.add_output(DAG)
  tg.set_loss(DAG)
  tg.build()
  tg.save()


def test_DAGGather_pickle():
  tg = TensorGraph()
  atom_features = Feature(shape=(None, 30))
  membership = Feature(shape=(None,), dtype=tf.int32)
  Gather = DAGGather(in_layers=[atom_features, membership])
  tg.add_output(Gather)
  tg.set_loss(Gather)
  tg.build()
  tg.save()


def test_MP_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 75))
  pair_feature = Feature(shape=(None, 14))
  atom_to_pair = Feature(shape=(None, 2), dtype=tf.int32)
  MP = MessagePassing(5, in_layers=[atom_feature, pair_feature, atom_to_pair])
  tg.add_output(MP)
  tg.set_loss(MP)
  tg.build()
  tg.save()


def test_SetGather_pickle():
  tg = TensorGraph()
  atom_feature = Feature(shape=(None, 100))
  atom_split = Feature(shape=(None,), dtype=tf.int32)
  Gather = SetGather(5, 16, in_layers=[atom_feature, atom_split])
  tg.add_output(Gather)
  tg.set_loss(Gather)
  tg.build()
  tg.save()
+39 −0
Original line number Diff line number Diff line
@@ -1382,6 +1382,45 @@ class TestOverfit(test_util.TensorFlowTestCase):

    assert scores[regression_metric.name] > .8

  def test_MPNN_singletask_regression_overfit(self):
    """Test MPNN overfits tiny data."""
    np.random.seed(123)
    tf.set_random_seed(123)
    n_tasks = 1

    # Load mini log-solubility dataset.
    featurizer = dc.feat.WeaveFeaturizer()
    tasks = ["outcome"]
    input_file = os.path.join(self.current_dir, "example_regression.csv")
    loader = dc.data.CSVLoader(
        tasks=tasks, smiles_field="smiles", featurizer=featurizer)
    dataset = loader.featurize(input_file)

    regression_metric = dc.metrics.Metric(
        dc.metrics.pearson_r2_score, task_averager=np.mean)

    n_atom_feat = 75
    n_pair_feat = 14
    batch_size = 10
    model = dc.models.MPNNTensorGraph(
        n_tasks,
        n_atom_feat=n_atom_feat,
        n_pair_feat=n_pair_feat,
        T=2,
        M=3,
        batch_size=batch_size,
        learning_rate=0.001,
        use_queue=False,
        mode="regression")

    # Fit trained model
    model.fit(dataset, nb_epoch=50)

    # Eval model on train
    scores = model.evaluate(dataset, [regression_metric])

    assert scores[regression_metric.name] > .8

  def test_siamese_singletask_classification_overfit(self):
    """Test siamese singletask model overfits tiny data."""
    np.random.seed(123)
+3 −3
Original line number Diff line number Diff line
@@ -413,11 +413,11 @@ class WeaveGather(Layer):
                            (0.228, 0.114), (0.468, 0.118), (0.739, 0.134),
                            (1.080, 0.170), (1.645, 0.283)]
    dist = [
        tf.contrib.distributions.Normal(mu=p[0], sigma=p[1])
        tf.contrib.distributions.Normal(loc=p[0], scale=p[1])
        for p in gaussian_memberships
    ]
    dist_max = [dist[i].pdf(gaussian_memberships[i][0]) for i in range(11)]
    outputs = [dist[i].pdf(x) / dist_max[i] for i in range(11)]
    dist_max = [dist[i].prob(gaussian_memberships[i][0]) for i in range(11)]
    outputs = [dist[i].prob(x) / dist_max[i] for i in range(11)]
    outputs = tf.stack(outputs, axis=2)
    outputs = outputs / tf.reduce_sum(outputs, axis=2, keep_dims=True)
    outputs = tf.reshape(outputs, [-1, self.n_input * 11])