Commit ecb7160a authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Test fixes and res model experiments.

parent a1e6a688
Loading
Loading
Loading
Loading
+7 −6
Original line number Diff line number Diff line
@@ -9,7 +9,8 @@ import tensorflow as tf

from tensorflow.python.framework import test_util
from tensorflow.python.platform import flags
from deepchem.models.tensorflow_models import model_ops
import deepchem as dc
#from deepchem.models.tensorflow_models import model_ops

FLAGS = flags.FLAGS
FLAGS.test_random_seed = 20151102
@@ -23,7 +24,7 @@ class TestModelOps(test_util.TensorFlowTestCase):
  def test_add_bias(self):
    with self.test_session() as sess:
      w_t = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], shape=[2, 3])
      w_biased_t = model_ops.add_bias(w_t, init=tf.constant(5.0, shape=[3]))
      w_biased_t = dc.nn.add_bias(w_t, init=tf.constant(5.0, shape=[3]))
      sess.run(tf.initialize_all_variables())
      w, w_biased, bias = sess.run([w_t, w_biased_t] + tf.trainable_variables())
      self.assertAllEqual(w, [[1.0, 2.0, 3.0],
@@ -36,7 +37,7 @@ class TestModelOps(test_util.TensorFlowTestCase):
    with self.test_session() as sess:
      features = np.random.random((128, 100))
      features_t = tf.constant(features, dtype=tf.float32)
      dense_t = model_ops.fully_connected_layer(features_t, 50)
      dense_t = dc.nn.fully_connected_layer(features_t, 50)
      sess.run(tf.initialize_all_variables())
      features, dense, w, b = sess.run(
          [features_t, dense_t] + tf.trainable_variables())
@@ -48,7 +49,7 @@ class TestModelOps(test_util.TensorFlowTestCase):
      num_tasks = 3
      np.random.seed(FLAGS.test_random_seed)
      features = np.random.random((5, 100))
      logits_t = model_ops.multitask_logits(
      logits_t = dc.nn.multitask_logits(
          tf.constant(features,
                      dtype=tf.float32),
          num_tasks)
@@ -74,7 +75,7 @@ class TestModelOps(test_util.TensorFlowTestCase):
                          dtype=float)
    with self.test_session() as sess:
      computed = sess.run(
          model_ops.softmax_N(tf.constant(features,
          dc.nn.softmax_N(tf.constant(features,
                                         dtype=tf.float32)))
    self.assertAllClose(np.around(computed, 2), expected)

@@ -83,6 +84,6 @@ class TestModelOps(test_util.TensorFlowTestCase):
    expected = np.exp(features) / np.exp(features).sum(axis=-1, keepdims=True)
    with self.test_session() as sess:
      computed = sess.run(
          model_ops.softmax_N(tf.constant(features,
          dc.nn.softmax_N(tf.constant(features,
                                          dtype=tf.float32)))
      self.assertAllClose(computed, expected)
+1 −1
Original line number Diff line number Diff line
@@ -848,7 +848,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
      support_model.add_test(dc.nn.GraphGather(test_batch_size))
      support_model.add_support(dc.nn.GraphGather(support_batch_size))

      # Apply an attention lstm layer
      # Apply a residual lstm layer
      support_model.join(dc.nn.ResiLSTMEmbedding(
          test_batch_size, support_batch_size, max_depth))

+19 −30
Original line number Diff line number Diff line
@@ -15,16 +15,8 @@ import unittest
import numpy as np
import deepchem as dc
from sklearn.linear_model import LogisticRegression
#from deepchem.models.tests import TestAPI
#from deepchem import metrics
#from deepchem.metrics import Metric
#from deepchem.datasets import DiskDataset
#from deepchem.featurizers.fingerprints import CircularFingerprint
#from deepchem.models.multitask import SingletaskToMultitask 
#from deepchem.models.sklearn_models import SklearnModel
#from deepchem.utils.evaluate import Evaluator

class TestSingletasktoMultitaskAPI(TestAPI):
class TestSingletasktoMultitask(unittest.TestCase):
  """
  Test top-level API for singletask_to_multitask ML models.
  """
@@ -38,8 +30,8 @@ class TestSingletasktoMultitaskAPI(TestAPI):
    y_train = np.random.randint(2, size=(n_train, n_tasks))
    w_train = np.ones_like(y_train)
    ids_train = ["C"] * n_train
    train_dataset = DiskDataset.from_numpy(
        self.train_dir, X_train, y_train, w_train, ids_train)
    train_dataset = dc.datasets.DiskDataset.from_numpy(
        tempfile.mkdtemp(), X_train, y_train, w_train, ids_train)

    # Define test dataset
    n_test = 10
@@ -47,30 +39,23 @@ class TestSingletasktoMultitaskAPI(TestAPI):
    y_test = np.random.randint(2, size=(n_test, n_tasks))
    w_test = np.ones_like(y_test)
    ids_test = ["C"] * n_test
    test_dataset = DiskDataset.from_numpy(
        self.test_dir, X_test, y_test, w_test, ids_test)
    test_dataset = dc.datasets.DiskDataset.from_numpy(
        tempfile.mkdtemp(), X_test, y_test, w_test, ids_test)

    transformers = []
    classification_metrics = [Metric(metrics.roc_auc_score)]
    classification_metrics = [dc.metrics.Metric(dc.metrics.roc_auc_score)]
    def model_builder(model_dir):
      sklearn_model = LogisticRegression()
      return SklearnModel(sklearn_model, model_dir)
    multitask_model = SingletaskToMultitask(
        tasks, model_builder, self.model_dir)
      return dc.models.SklearnModel(sklearn_model, model_dir)
    multitask_model = dc.models.SingletaskToMultitask(
        tasks, model_builder)

    # Fit trained model
    multitask_model.fit(train_dataset)
    multitask_model.save()

    # Eval multitask_model on train
    evaluator = Evaluator(multitask_model, train_dataset, transformers,
                          verbosity=True)
    _ = evaluator.compute_model_performance(classification_metrics)

    # Eval multitask_model on test
    evaluator = Evaluator(multitask_model, test_dataset, transformers,
                          verbosity=True)
    _ = evaluator.compute_model_performance(classification_metrics)
    # Eval multitask_model on train/test
    _ = multitask_model.evaluate(train_dataset, classification_metrics)
    _ = multitask_model.evaluate(test_dataset, classification_metrics)


  def test_to_singletask(self):
@@ -84,16 +69,20 @@ class TestSingletasktoMultitaskAPI(TestAPI):
    w = np.random.randint(2, size=(num_datapoints, num_tasks))
    ids = np.array(["id"] * num_datapoints)
    
    dataset = DiskDataset.from_numpy(self.train_dir, X, y, w, ids)
    dataset = dc.datasets.DiskDataset.from_numpy(
        tempfile.mkdtemp(), X, y, w, ids)

    task_dirs = []
    try:
      for task in range(num_tasks):
        task_dirs.append(tempfile.mkdtemp())
      singletask_datasets = SingletaskToMultitask._to_singletask(dataset, task_dirs)
      singletask_datasets = dc.models.SingletaskToMultitask._to_singletask(
          dataset, task_dirs)
      for task in range(num_tasks):
        singletask_dataset = singletask_datasets[task]
        X_task, y_task, w_task, ids_task = (singletask_dataset.X, singletask_dataset.y, singletask_dataset.w, singletask_dataset.ids)
        X_task, y_task, w_task, ids_task = (
            singletask_dataset.X, singletask_dataset.y, singletask_dataset.w,
            singletask_dataset.ids)
        w_nonzero = w[:, task] != 0
        np.testing.assert_array_equal(X_task, X[w_nonzero != 0])
        np.testing.assert_array_equal(y_task.flatten(), y[:, task][w_nonzero != 0])
+4 −0
Original line number Diff line number Diff line
@@ -564,6 +564,10 @@ class ResiLSTMEmbedding(Layer):
  def __init__(self, n_test, n_support, max_depth, init='glorot_uniform',
               activation='linear', **kwargs):
    """
    Unlike the AttnLSTM model which only modifies the test vectors additively,
    this model allows for an additive update to be performed to both test and
    support using information from each other.

    Parameters
    ----------
    n_support: int
+4 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@ from deepchem.models.tf_keras_models.graph_models import SequentialSupportGraph

from deepchem.models.tensorflow_models.model_ops import weight_decay
from deepchem.models.tensorflow_models.model_ops import optimizer
from deepchem.models.tensorflow_models.model_ops import add_bias
from deepchem.models.tensorflow_models.model_ops import fully_connected_layer
from deepchem.models.tensorflow_models.model_ops import multitask_logits
from deepchem.models.tensorflow_models.model_ops import softmax_N 

from deepchem.models.tf_keras_models.keras_layers import AttnLSTMEmbedding
from deepchem.models.tf_keras_models.keras_layers import ResiLSTMEmbedding
Loading