Commit 1d8a4d54 authored by leswing's avatar leswing
Browse files

Formatting

parent 44b2c473
Loading
Loading
Loading
Loading
+67 −32
Original line number Diff line number Diff line
@@ -5,7 +5,9 @@ import tensorflow as tf

from deepchem.nn import model_ops, initializations


class Layer(object):

  def __init__(self, **kwargs):
    if "name" not in kwargs:
      self.name = "%s%s" % (self.__class__.__name__, self._random_name())
@@ -30,6 +32,7 @@ class Layer(object):


class Conv1DLayer(Layer):

  def __init__(self, width, out_channels, **kwargs):
    self.width = width
    self.out_channels = out_channels
@@ -54,6 +57,7 @@ class Conv1DLayer(Layer):


class Dense(Layer):

  def __init__(self, out_channels, activation_fn=None, **kwargs):
    self.out_channels = out_channels
    self.out_tensor = None
@@ -76,6 +80,7 @@ class Dense(Layer):


class Flatten(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -93,6 +98,7 @@ class Flatten(Layer):


class Reshape(Layer):

  def __init__(self, shape, **kwargs):
    self.shape = shape
    super().__init__(**kwargs)
@@ -103,6 +109,7 @@ class Reshape(Layer):


class CombineMeanStd(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -117,6 +124,7 @@ class CombineMeanStd(Layer):


class Repeat(Layer):

  def __init__(self, n_times, **kwargs):
    self.n_times = n_times
    super().__init__(**kwargs)
@@ -131,6 +139,7 @@ class Repeat(Layer):


class GRU(Layer):

  def __init__(self, n_hidden, out_channels, batch_size, **kwargs):
    self.n_hidden = n_hidden
    self.out_channels = out_channels
@@ -153,6 +162,7 @@ class GRU(Layer):


class TimeSeriesDense(Layer):

  def __init__(self, out_channels, **kwargs):
    self.out_channels = out_channels
    super().__init__(**kwargs)
@@ -167,6 +177,7 @@ class TimeSeriesDense(Layer):


class Input(Layer):

  def __init__(self, shape, dtype=tf.float32, pre_queue=False, **kwargs):
    self.shape = shape
    self.pre_queue = pre_queue
@@ -186,7 +197,11 @@ class Input(Layer):
    if self.pre_queue:
      raise ValueError("Input is already pre_q")
    q_shape = (batch_size,) + self.shape[1:]
    return Input(shape=q_shape, name="%s_pre_q" % self.name, dtype=self.dtype, pre_queue=True)
    return Input(
        shape=q_shape,
        name="%s_pre_q" % self.name,
        dtype=self.dtype,
        pre_queue=True)

  def get_pre_q_name(self):
    if self.pre_queue:
@@ -195,6 +210,7 @@ class Input(Layer):


class LossLayer(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -206,6 +222,7 @@ class LossLayer(Layer):


class SoftMax(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -218,6 +235,7 @@ class SoftMax(Layer):


class Concat(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -232,6 +250,7 @@ class Concat(Layer):


class SoftMaxCrossEntropy(Layer):

  def __init__(self, **kwargs):
    super().__init__(**kwargs)

@@ -246,6 +265,7 @@ class SoftMaxCrossEntropy(Layer):


class ReduceMean(Layer):

  def __call__(self, *parents):
    parent_tensor = parents[0].out_tensor
    self.out_tensor = tf.reduce_mean(parent_tensor)
@@ -253,6 +273,7 @@ class ReduceMean(Layer):


class Conv2d(Layer):

  def __init__(self, num_outputs, kernel_size=5, **kwargs):
    self.num_outputs = num_outputs
    self.kernel_size = kernel_size
@@ -271,6 +292,7 @@ class Conv2d(Layer):


class MaxPool(Layer):

  def __init__(self,
               ksize=[1, 2, 2, 1],
               strides=[1, 2, 2, 1],
@@ -323,7 +345,13 @@ class InputFifoQueue(Layer):


class GraphConvLayer(Layer):
  def __init__(self, out_channel, min_deg=0, max_deg=10, activation_fn=None, **kwargs):

  def __init__(self,
               out_channel,
               min_deg=0,
               max_deg=10,
               activation_fn=None,
               **kwargs):
    self.out_channel = out_channel
    self.min_degree = min_deg
    self.max_degree = max_deg
@@ -428,6 +456,7 @@ class GraphConvLayer(Layer):


class GraphPoolLayer(Layer):

  def __init__(self, min_degree=0, max_degree=10, **kwargs):
    self.min_degree = min_degree
    self.max_degree = max_degree
@@ -473,6 +502,7 @@ class GraphPoolLayer(Layer):


class GraphGather(Layer):

  def __init__(self, batch_size, activation_fn=None, **kwargs):
    self.batch_size = batch_size
    self.activation_fn = activation_fn
@@ -490,14 +520,17 @@ class GraphGather(Layer):
    assert (self.batch_size > 1, "graph_gather requires batches larger than 1")

    # Obtain the partitions for each of the molecules
    activated_par = tf.dynamic_partition(atom_features, membership, self.batch_size)
    activated_par = tf.dynamic_partition(atom_features, membership,
                                         self.batch_size)

    # Sum over atoms for each molecule
    sparse_reps = [
      tf.reduce_sum(activated, 0, keep_dims=True) for activated in activated_par
        tf.reduce_sum(activated, 0, keep_dims=True)
        for activated in activated_par
    ]
    max_reps = [
      tf.reduce_max(activated, 0, keep_dims=True) for activated in activated_par
        tf.reduce_max(activated, 0, keep_dims=True)
        for activated in activated_par
    ]

    # Get the final sparse representations
@@ -512,6 +545,7 @@ class GraphGather(Layer):


class BatchNormLayer(Layer):

  def __call__(self, *parents):
    parent_tensor = parents[0].out_tensor
    self.out_tensor = tf.layers.batch_normalization(parent_tensor)
@@ -519,6 +553,7 @@ class BatchNormLayer(Layer):


class WeightedError(Layer):

  def __call__(self, *parents):
    entropy, weights = parents[0], parents[1]
    self.out_tensor = tf.reduce_sum(entropy.out_tensor * weights.out_tensor)
+18 −12
Original line number Diff line number Diff line
@@ -7,10 +7,10 @@ from deepchem.feat.mol_graphs import ConvMol
import time



class GraphConvTensorGraph(TensorGraph):
  """
  """

  def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.min_degree = 0
@@ -29,7 +29,8 @@ class GraphConvTensorGraph(TensorGraph):
      feed_dict[self.features[1].out_tensor] = multiConvMol.deg_slice
      feed_dict[self.features[2].out_tensor] = multiConvMol.membership
      for i in range(self.max_degree):
        feed_dict[self.features[i + 3].out_tensor] = multiConvMol.get_deg_adjacency_lists()[i + 1]
        feed_dict[self.features[i + 3]
                  .out_tensor] = multiConvMol.get_deg_adjacency_lists()[i + 1]
    return feed_dict

  def fit(self,
@@ -63,7 +64,8 @@ class GraphConvTensorGraph(TensorGraph):
        avg_loss, n_batches = 0.0, 0.0
        for epoch in range(nb_epoch):
          for ind, (X_b, y_b, w_b, ids_b) in enumerate(
            dataset.iterbatches(self.batch_size, deterministic=True, pad_batches=True)):
              dataset.iterbatches(
                  self.batch_size, deterministic=True, pad_batches=True)):
            feed_dict = self._construct_feed_dict(X_b, y_b, w_b, ids_b)
            output_tensors = [x.out_tensor for x in self.outputs]
            fetches = output_tensors + [train_op, self.loss.out_tensor]
@@ -85,8 +87,7 @@ class GraphConvTensorGraph(TensorGraph):


def graph_conv_model(batch_size, num_tasks):
  model = GraphConvTensorGraph(batch_size=batch_size,
                               use_queue=False)
  model = GraphConvTensorGraph(batch_size=batch_size, use_queue=False)
  atom_features = Input(shape=(None, 75))
  model.add_feature(atom_features)

@@ -103,13 +104,15 @@ def graph_conv_model(batch_size, num_tasks):
    deg_adjs.append(deg_adj)

  gc1 = GraphConvLayer(64, activation_fn=tf.nn.relu)
  model.add_layer(gc1, parents=[atom_features, degree_slice, membership] + deg_adjs)
  model.add_layer(
      gc1, parents=[atom_features, degree_slice, membership] + deg_adjs)

  batch_norm1 = BatchNormLayer()
  model.add_layer(batch_norm1, parents=[gc1])

  gp1 = GraphPoolLayer()
  model.add_layer(gp1, parents=[batch_norm1, degree_slice, membership] + deg_adjs)
  model.add_layer(
      gp1, parents=[batch_norm1, degree_slice, membership] + deg_adjs)

  gc2 = GraphConvLayer(64, activation_fn=tf.nn.relu)
  model.add_layer(gc2, parents=[gp1, degree_slice, membership] + deg_adjs)
@@ -118,7 +121,8 @@ def graph_conv_model(batch_size, num_tasks):
  model.add_layer(batch_norm2, parents=[gc2])

  gp2 = GraphPoolLayer()
  model.add_layer(gp2, parents=[batch_norm2, degree_slice, membership] + deg_adjs)
  model.add_layer(
      gp2, parents=[batch_norm2, degree_slice, membership] + deg_adjs)

  dense = Dense(out_channels=128, activation_fn=None)
  model.add_layer(dense, parents=[gp2])
@@ -127,11 +131,13 @@ def graph_conv_model(batch_size, num_tasks):
  model.add_layer(batch_norm3, parents=[dense])

  gg1 = GraphGather(batch_size=batch_size, activation_fn=tf.nn.tanh)
  model.add_layer(gg1, parents=[batch_norm3, degree_slice, membership] + deg_adjs)
  model.add_layer(
      gg1, parents=[batch_norm3, degree_slice, membership] + deg_adjs)

  costs = []
  for task in range(num_tasks):
    classification = Dense(out_channels=2, name="GUESS%s" % task, activation_fn=None)
    classification = Dense(
        out_channels=2, name="GUESS%s" % task, activation_fn=None)
    model.add_layer(classification, parents=[gg1])

    softmax = SoftMax(name="SOFTMAX%s" % task)
+3 −1
Original line number Diff line number Diff line
@@ -176,6 +176,7 @@ class TensorGraph(Model):
    return retval

  def predict_proba_on_batch(self, X, sess=None):

    def predict():
      out_tensors = [x.out_tensor for x in self.outputs]
      fetches = out_tensors
@@ -376,7 +377,8 @@ class TensorGraph(Model):
      self.tensor_objects['FileWriter'] = tf.summary.FileWriter(self.model_dir)
    elif obj == 'train_op':
      self.tensor_objects['train_op'] = tf.train.AdamOptimizer(
          self.learning_rate,beta1=.9, beta2=.999).minimize(self.loss.out_tensor)
          self.learning_rate, beta1=.9,
          beta2=.999).minimize(self.loss.out_tensor)
    elif obj == 'summary_op':
      self.tensor_objects['summary_op'] = tf.summary.merge_all(
          key=tf.GraphKeys.SUMMARIES)
+2 −1
Original line number Diff line number Diff line
@@ -495,7 +495,8 @@ def benchmark_classification(train_dataset,
    nb_epoch = hyper_parameters['nb_epoch']
    learning_rate = hyper_parameters['learning_rate']

    model_graphconv = dc.models.tensorgraph.models.graph_conv_model(batch_size, len(tasks))
    model_graphconv = dc.models.tensorgraph.models.graph_conv_model(batch_size,
                                                                    len(tasks))
    model_graphconv.fit(train_dataset, nb_epoch=nb_epoch)
    # Evaluating graph convolution model
    train_scores['graphconv'] = model_graphconv.evaluate(
+4 −3
Original line number Diff line number Diff line
@@ -24,7 +24,8 @@ metric = dc.metrics.Metric(
# Batch size of models
batch_size = 50

model = dc.models.tensorgraph.models.graph_conv_model(batch_size, len(tox21_tasks))
model = dc.models.tensorgraph.models.graph_conv_model(batch_size,
                                                      len(tox21_tasks))

model.fit(train_dataset, nb_epoch=10, checkpoint_interval=10)