Unverified Commit 25b9527e authored by Bharath Ramsundar's avatar Bharath Ramsundar Committed by GitHub
Browse files

Merge pull request #954 from lilleswing/graph_convs

GraphConvTensorGraph classification with multiple tasks
parents cf32351f 7792f51b
Loading
Loading
Loading
Loading
+13 −13
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ from deepchem.models.tensorgraph.graph_layers import WeaveGather, \
  DTNNEmbedding, DTNNStep, DTNNGather, DAGLayer, \
  DAGGather, DTNNExtract, MessagePassing, SetGather
from deepchem.models.tensorgraph.graph_layers import WeaveLayerFactory
from deepchem.models.tensorgraph.layers import Dense, Concat, SoftMax, \
from deepchem.models.tensorgraph.layers import Dense, SoftMax, \
  SoftMaxCrossEntropy, GraphConv, BatchNorm, \
  GraphPool, GraphGather, WeightedError, Dropout, BatchNormalization, Stack, Flatten, GraphCNN, GraphCNNPool
from deepchem.models.tensorgraph.layers import L2Loss, Label, Weights, Feature
@@ -116,7 +116,7 @@ class WeaveTensorGraph(TensorGraph):
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      all_cost = Concat(in_layers=costs, axis=1)
      all_cost = Stack(in_layers=costs, axis=1)
    elif self.mode == "regression":
      all_cost = Stack(in_layers=costs, axis=1)
    self.weights = Weights(shape=(None, self.n_tasks))
@@ -174,8 +174,8 @@ class WeaveTensorGraph(TensorGraph):
          atom_feat.append(mol.get_atom_features())
          # pair features
          pair_feat.append(
              np.reshape(mol.get_pair_features(), (n_atoms * n_atoms,
                                                   self.n_pair_feat)))
              np.reshape(mol.get_pair_features(),
                         (n_atoms * n_atoms, self.n_pair_feat)))

        feed_dict[self.atom_features] = np.concatenate(atom_feat, axis=0)
        feed_dict[self.pair_features] = np.concatenate(pair_feat, axis=0)
@@ -310,8 +310,8 @@ class DTNNTensorGraph(TensorGraph):
        num_atoms = list(map(sum, X_b.astype(bool)[:, :, 0]))
        atom_number = [
            np.round(
                np.power(2 * np.diag(X_b[i, :num_atoms[i], :num_atoms[i]]), 1 /
                         2.4)).astype(int) for i in range(len(num_atoms))
                np.power(2 * np.diag(X_b[i, :num_atoms[i], :num_atoms[i]]),
                         1 / 2.4)).astype(int) for i in range(len(num_atoms))
        ]
        start = 0
        for im, molecule in enumerate(atom_number):
@@ -425,7 +425,7 @@ class DAGTensorGraph(TensorGraph):
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      all_cost = Concat(in_layers=costs, axis=1)
      all_cost = Stack(in_layers=costs, axis=1)
    elif self.mode == "regression":
      all_cost = Stack(in_layers=costs, axis=1)
    self.weights = Weights(shape=(None, self.n_tasks))
@@ -570,7 +570,7 @@ class PetroskiSuchTensorGraph(TensorGraph):
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      entropy = Concat(in_layers=costs, axis=-1)
      entropy = Stack(in_layers=costs, axis=-1)
    elif self.mode == "regression":
      entropy = Stack(in_layers=costs, axis=1)
    self.my_task_weights = Weights(shape=(None, self.n_tasks))
@@ -716,7 +716,7 @@ class GraphConvTensorGraph(TensorGraph):
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      entropy = Concat(in_layers=costs, axis=-1)
      entropy = Stack(in_layers=costs, axis=1)
    elif self.mode == "regression":
      entropy = Stack(in_layers=costs, axis=1)
    self.my_task_weights = Weights(shape=(None, self.n_tasks))
@@ -994,7 +994,7 @@ class MPNNTensorGraph(TensorGraph):
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)
    if self.mode == "classification":
      all_cost = Concat(in_layers=costs, axis=1)
      all_cost = Stack(in_layers=costs, axis=1)
    elif self.mode == "regression":
      all_cost = Stack(in_layers=costs, axis=1)
    self.weights = Weights(shape=(None, self.n_tasks))
@@ -1051,8 +1051,8 @@ class MPNNTensorGraph(TensorGraph):
          atom_feat.append(mol.get_atom_features())
          # pair features
          pair_feat.append(
              np.reshape(mol.get_pair_features(), (n_atoms * n_atoms,
                                                   self.n_pair_feat)))
              np.reshape(mol.get_pair_features(),
                         (n_atoms * n_atoms, self.n_pair_feat)))

        feed_dict[self.atom_features] = np.concatenate(atom_feat, axis=0)
        feed_dict[self.pair_features] = np.concatenate(pair_feat, axis=0)
+10 −4
Original line number Diff line number Diff line
@@ -11,21 +11,27 @@ from deepchem.molnet.load_function.delaney_datasets import load_delaney

class TestGraphModels(unittest.TestCase):

  def get_dataset(self, mode='classification', featurizer='GraphConv'):
  def get_dataset(self,
                  mode='classification',
                  featurizer='GraphConv',
                  num_tasks=2):
    data_points = 10
    tasks, all_dataset, transformers = load_delaney(featurizer)
    train, valid, test = all_dataset
    for i in range(1, num_tasks):
      tasks.append("random_task")
    w = np.ones(shape=(data_points, len(tasks)))

    if mode == 'classification':
      y = np.random.randint(0, 2, size=(data_points, len(tasks)))
      metric = deepchem.metrics.Metric(
          deepchem.metrics.roc_auc_score, np.mean, mode="classification")
    if mode == 'regression':
    else:
      y = np.random.normal(size=(data_points, len(tasks)))
      metric = deepchem.metrics.Metric(
          deepchem.metrics.mean_absolute_error, mode="regression")

    ds = NumpyDataset(train.X[:10], y, train.w[:10], train.ids[:10])
    ds = NumpyDataset(train.X[:10], y, w, train.ids[:10])

    return tasks, ds, transformers, metric

@@ -61,7 +67,7 @@ class TestGraphModels(unittest.TestCase):

  def test_graph_conv_error_bars(self):
    tasks, dataset, transformers, metric = self.get_dataset(
        'regression', 'GraphConv')
        'regression', 'GraphConv', num_tasks=1)

    batch_size = 50
    model = GraphConvTensorGraph(
+1 −0
Original line number Diff line number Diff line
@@ -280,6 +280,7 @@ class TestLayers(test_util.TensorFlowTestCase):
    with self.test_session() as sess:
      result = Log()(value).eval()
      assert np.array_equal(np.log(value), result)
      assert np.all(np.isclose(np.log(value), result, atol=0.001))

  def test_exp(self):
    """Test that Exp can be invoked."""