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

Merge branch 'master' of https://github.com/deepchem/deepchem into website

parents 73e09ea8 d99c86c6
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
from flaky import flaky

import deepchem as dc
from deepchem.models.tensorgraph.layers import Feature, Label, Dense, L2Loss
import numpy as np
@@ -7,6 +9,7 @@ import unittest

class TestMAML(unittest.TestCase):

  @flaky
  def test_sine(self):
    """Test meta-learning for sine function."""

+39 −6
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@
Code for processing datasets using scikit-learn.
"""
import numpy as np
from sklearn.cross_decomposition import PLSRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LogisticRegression
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.linear_model import LogisticRegression, BayesianRidge
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LassoCV
@@ -14,11 +16,42 @@ from deepchem.models import Model
from deepchem.utils.save import load_from_disk
from deepchem.utils.save import save_to_disk

NON_WEIGHTED_MODELS = [
    LogisticRegression, PLSRegression, GaussianProcessRegressor, ElasticNetCV,
    LassoCV, BayesianRidge
]


class SklearnModel(Model):
  """
  Abstract base class for different ML models.
  """

  def __init__(self,
               model_instance=None,
               model_dir=None,
               verbose=True,
               **kwargs):
    """
    Parameters
    ----------
    model_instance: sklearn model
    model_dir: str
    verbose: bool
    kwargs: dict
      kwargs['use_weights'] is a bool which determines if we pass weights into
      self.model_instance.fit()
    """
    super(SklearnModel, self).__init__(model_instance, model_dir, verbose,
                                       **kwargs)
    if 'use_weights' in kwargs:
      self.use_weights = kwargs['use_weights']
    else:
      self.use_weights = True
    for model_instance in NON_WEIGHTED_MODELS:
      if isinstance(self.model_instance, model_instance):
        self.use_weights = False

  def fit(self, dataset, **kwargs):
    """
    Fits SKLearn model to data.
@@ -27,11 +60,10 @@ class SklearnModel(Model):
    y = np.squeeze(dataset.y)
    w = np.squeeze(dataset.w)
    # Logistic regression doesn't support weights
    if not isinstance(self.model_instance, LogisticRegression):
    if self.use_weights:
      self.model_instance.fit(X, y, w)
    else:
      return
    self.model_instance.fit(X, y)
    y_pred_raw = self.model_instance.predict(X)

  def predict_on_batch(self, X, pad_batch=False):
    """
@@ -73,7 +105,8 @@ class SklearnModel(Model):

  def reload(self):
    """Loads sklearn model from joblib file on disk."""
    self.model_instance = load_from_disk(Model.get_model_filename(self.model_dir))
    self.model_instance = load_from_disk(
        Model.get_model_filename(self.model_dir))

  def get_num_tasks(self):
    """Number of tasks for this model. Defaults to 1"""
+38 −38
Original line number Diff line number Diff line
@@ -365,8 +365,8 @@ class Dense(Layer):
class Flatten(Layer):
  """Flatten every dimension except the first"""

  def __init__(self, **kwargs):
    super(Flatten, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(Flatten, self).__init__(in_layers, **kwargs)
    try:
      parent_shape = self.in_layers[0].shape
      s = list(parent_shape[:2])
@@ -425,9 +425,9 @@ class Reshape(Layer):

class Squeeze(Layer):

  def __init__(self, squeeze_dims=None, **kwargs):
  def __init__(self, in_layers=None, squeeze_dims=None, **kwargs):
    self.squeeze_dims = squeeze_dims
    super(Squeeze, self).__init__(**kwargs)
    super(Squeeze, self).__init__(in_layers, **kwargs)
    try:
      parent_shape = self.in_layers[0].shape
      if squeeze_dims is None:
@@ -472,8 +472,8 @@ class Transpose(Layer):

class CombineMeanStd(Layer):

  def __init__(self, **kwargs):
    super(CombineMeanStd, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(CombineMeanStd, self).__init__(in_layers, **kwargs)
    try:
      self._shape = self.in_layers[0].shape
    except:
@@ -650,8 +650,8 @@ class Weights(Input):

class L1Loss(Layer):

  def __init__(self, **kwargs):
    super(L1Loss, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(L1Loss, self).__init__(in_layers, **kwargs)

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    inputs = self._get_input_tensors(in_layers, True)
@@ -665,8 +665,8 @@ class L1Loss(Layer):

class L2Loss(Layer):

  def __init__(self, **kwargs):
    super(L2Loss, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(L2Loss, self).__init__(in_layers, **kwargs)
    try:
      shape1 = self.in_layers[0].shape
      shape2 = self.in_layers[1].shape
@@ -689,8 +689,8 @@ class L2Loss(Layer):

class SoftMax(Layer):

  def __init__(self, **kwargs):
    super(SoftMax, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(SoftMax, self).__init__(in_layers, **kwargs)
    try:
      self._shape = tuple(self.in_layers[0].shape)
    except:
@@ -709,9 +709,9 @@ class SoftMax(Layer):

class Concat(Layer):

  def __init__(self, axis=1, **kwargs):
  def __init__(self, in_layers=None, axis=1, **kwargs):
    self.axis = axis
    super(Concat, self).__init__(**kwargs)
    super(Concat, self).__init__(in_layers, **kwargs)
    try:
      s = list(self.in_layers[0].shape)
      for parent in self.in_layers[1:]:
@@ -734,9 +734,9 @@ class Concat(Layer):

class Stack(Layer):

  def __init__(self, axis=1, **kwargs):
  def __init__(self, in_layers=None, axis=1, **kwargs):
    self.axis = axis
    super(Stack, self).__init__(**kwargs)
    super(Stack, self).__init__(in_layers, **kwargs)
    try:
      s = list(self.in_layers[0].shape)
      s.insert(axis, len(self.in_layers))
@@ -810,7 +810,7 @@ class Variable(Layer):
class Add(Layer):
  """Compute the (optionally weighted) sum of the input layers."""

  def __init__(self, weights=None, **kwargs):
  def __init__(self, in_layers=None, weights=None, **kwargs):
    """Create an Add layer.

    Parameters
@@ -819,7 +819,7 @@ class Add(Layer):
      an array of length equal to the number of input layers, giving the weight
      to multiply each input by.  If None, all weights are set to 1.
    """
    super(Add, self).__init__(**kwargs)
    super(Add, self).__init__(in_layers, **kwargs)
    self.weights = weights
    try:
      shape1 = list(self.in_layers[0].shape)
@@ -854,8 +854,8 @@ class Add(Layer):
class Multiply(Layer):
  """Compute the product of the input layers."""

  def __init__(self, **kwargs):
    super(Multiply, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(Multiply, self).__init__(in_layers, **kwargs)
    try:
      shape1 = list(self.in_layers[0].shape)
      shape2 = list(self.in_layers[1].shape)
@@ -908,8 +908,8 @@ class InteratomicL2Distances(Layer):

class SparseSoftMaxCrossEntropy(Layer):

  def __init__(self, **kwargs):
    super(SparseSoftMaxCrossEntropy, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(SparseSoftMaxCrossEntropy, self).__init__(in_layers, **kwargs)
    try:
      self._shape = (self.in_layers[1].shape[0], 1)
    except:
@@ -930,8 +930,8 @@ class SparseSoftMaxCrossEntropy(Layer):

class SoftMaxCrossEntropy(Layer):

  def __init__(self, **kwargs):
    super(SoftMaxCrossEntropy, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(SoftMaxCrossEntropy, self).__init__(in_layers, **kwargs)
    try:
      self._shape = (self.in_layers[1].shape[0], 1)
    except:
@@ -952,9 +952,9 @@ class SoftMaxCrossEntropy(Layer):

class ReduceMean(Layer):

  def __init__(self, axis=None, **kwargs):
  def __init__(self, in_layers=None, axis=None, **kwargs):
    self.axis = axis
    super(ReduceMean, self).__init__(**kwargs)
    super(ReduceMean, self).__init__(in_layers, **kwargs)
    if axis is None:
      self._shape = tuple()
    else:
@@ -981,8 +981,8 @@ class ReduceMean(Layer):

class ToFloat(Layer):

  def __init__(self, **kwargs):
    super(ToFloat, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(ToFloat, self).__init__(in_layers, **kwargs)
    try:
      self._shape = tuple(self.in_layers[0].shape)
    except:
@@ -1000,9 +1000,9 @@ class ToFloat(Layer):

class ReduceSum(Layer):

  def __init__(self, axis=None, **kwargs):
  def __init__(self, in_layers=None, axis=None, **kwargs):
    self.axis = axis
    super(ReduceSum, self).__init__(**kwargs)
    super(ReduceSum, self).__init__(in_layers, **kwargs)
    if axis is None:
      self._shape = tuple()
    else:
@@ -1029,9 +1029,9 @@ class ReduceSum(Layer):

class ReduceSquareDifference(Layer):

  def __init__(self, axis=None, **kwargs):
  def __init__(self, in_layers=None, axis=None, **kwargs):
    self.axis = axis
    super(ReduceSquareDifference, self).__init__(**kwargs)
    super(ReduceSquareDifference, self).__init__(in_layers, **kwargs)
    if axis is None:
      self._shape = tuple()
    else:
@@ -1792,8 +1792,8 @@ class IterRefLSTMEmbedding(Layer):

class BatchNorm(Layer):

  def __init__(self, **kwargs):
    super(BatchNorm, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(BatchNorm, self).__init__(in_layers, **kwargs)
    try:
      parent_shape = self.in_layers[0].shape
      self._shape = tuple(self.in_layers[0].shape)
@@ -1855,8 +1855,8 @@ class BatchNormalization(Layer):

class WeightedError(Layer):

  def __init__(self, **kwargs):
    super(WeightedError, self).__init__(**kwargs)
  def __init__(self, in_layers=None, **kwargs):
    super(WeightedError, self).__init__(in_layers, **kwargs)
    self._shape = tuple()

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
@@ -1991,9 +1991,9 @@ class VinaFreeEnergy(Layer):
class WeightedLinearCombo(Layer):
  """Computes a weighted linear combination of input layers, with the weights defined by trainable variables."""

  def __init__(self, std=.3, **kwargs):
  def __init__(self, in_layers=None, std=.3, **kwargs):
    self.std = std
    super(WeightedLinearCombo, self).__init__(**kwargs)
    super(WeightedLinearCombo, self).__init__(in_layers, **kwargs)
    try:
      self._shape = tuple(self.in_layers[0].shape)
    except:
+32 −38
Original line number Diff line number Diff line
@@ -605,9 +605,6 @@ class GraphConvTensorGraph(TensorGraph):
    if not self.built:
      self.build()
    with self._get_tf("Graph").as_default():
      with tf.Session() as sess:
        saver = tf.train.Saver()
        saver.restore(sess, self.last_checkpoint)
      out_tensors = [x.out_tensor for x in self.outputs]
      results = []
      for feed_dict in generator:
@@ -616,7 +613,7 @@ class GraphConvTensorGraph(TensorGraph):
            for k, v in six.iteritems(feed_dict)
        }
        feed_dict[self._training_placeholder] = 1.0  ##
          result = np.array(sess.run(out_tensors, feed_dict=feed_dict))
        result = np.array(self.session.run(out_tensors, feed_dict=feed_dict))
        if len(result.shape) == 3:
          result = np.transpose(result, axes=[1, 0, 2])
        if len(transformers) > 0:
@@ -868,9 +865,6 @@ class MPNNTensorGraph(TensorGraph):
    if not self.built:
      self.build()
    with self._get_tf("Graph").as_default():
      with tf.Session() as sess:
        saver = tf.train.Saver()
        self._initialize_weights(sess, saver)
      out_tensors = [x.out_tensor for x in self.outputs]
      results = []
      for feed_dict in generator:
@@ -881,7 +875,7 @@ class MPNNTensorGraph(TensorGraph):
            for k, v in six.iteritems(feed_dict)
        }
        feed_dict[self._training_placeholder] = 0.0
          result = np.array(sess.run(out_tensors, feed_dict=feed_dict))
        result = np.array(self.session.run(out_tensors, feed_dict=feed_dict))
        if len(result.shape) == 3:
          result = np.transpose(result, axes=[1, 0, 2])
        result = undo_transforms(result, transformers)
+130 −94
Original line number Diff line number Diff line
@@ -81,7 +81,6 @@ class TensorGraph(Model):
    self.tensorboard_log_frequency = tensorboard_log_frequency
    self.tensorboard_step = 0
    self.global_step = 0
    self.last_checkpoint = None
    self.use_queue = use_queue

    self.batch_size = batch_size
@@ -93,6 +92,10 @@ class TensorGraph(Model):
    self.rnn_initial_states = []
    self.rnn_final_states = []
    self.rnn_zero_states = []
    if self.use_queue and self.tensorboard:
      raise ValueError(
          "Currently TensorGraph cannot both use_queue and tensorboard at the same time"
      )

  def _add_layer(self, layer):
    if layer.name is None:
@@ -116,16 +119,52 @@ class TensorGraph(Model):
          nb_epoch=10,
          max_checkpoints_to_keep=5,
          checkpoint_interval=1000,
          deterministic=False):
          deterministic=False,
          restore=False):
    """Train this model on a dataset.

    Parameters
    ----------
    dataset: Dataset
      the Dataset to train on
    nb_epoch: int
      the number of epochs to train for
    max_checkpoints_to_keep: int
      the maximum number of checkpoints to keep.  Older checkpoints are discarded.
    checkpoint_interval: int
      the frequency at which to write checkpoints, measured in training steps.
    deterministic: bool
      if True, the samples are processed in order.  If False, a different random
      order is used for each epoch.
    restore: bool
      if True, restore the model from the most recent checkpoint and continue training
      from there.  If False, retrain the model from scratch.
    """
    return self.fit_generator(
        self.default_generator(
            dataset, epochs=nb_epoch, deterministic=deterministic),
        max_checkpoints_to_keep, checkpoint_interval)
        max_checkpoints_to_keep, checkpoint_interval, restore)

  def fit_generator(self,
                    feed_dict_generator,
                    max_checkpoints_to_keep=5,
                    checkpoint_interval=1000):
                    checkpoint_interval=1000,
                    restore=False):
    """Train this model on data from a generator.

    Parameters
    ----------
    feed_dict_generator: generator
      this should generate batches, each represented as a dict that maps
      Layers to values.
    max_checkpoints_to_keep: int
      the maximum number of checkpoints to keep.  Older checkpoints are discarded.
    checkpoint_interval: int
      the frequency at which to write checkpoints, measured in training steps.
    restore: bool
      if True, restore the model from the most recent checkpoint and continue training
      from there.  If False, retrain the model from scratch.
    """

    def create_feed_dict():
      if self.use_queue:
@@ -142,36 +181,36 @@ class TensorGraph(Model):
      time1 = time.time()
      train_op = self._get_tf('train_op')
      saver = tf.train.Saver(max_to_keep=max_checkpoints_to_keep)
      with tf.Session() as sess:
        self._initialize_weights(sess, saver)
      self.session.run(tf.global_variables_initializer())
      if restore:
        self.restore()
      avg_loss, n_batches = 0.0, 0.0
      coord = tf.train.Coordinator()
      n_samples = 0
      if self.use_queue:
        enqueue_thread = threading.Thread(
            target=_enqueue_batch,
              args=(self, feed_dict_generator, self._get_tf("Graph"), sess,
                    coord))
            args=(self, feed_dict_generator, self._get_tf("Graph"),
                  self.session, coord))
        enqueue_thread.start()
      output_tensors = [x.out_tensor for x in self.outputs]
      fetches = output_tensors + [train_op, self.loss.out_tensor]
      for feed_dict in create_feed_dict():
        try:
            fetched_values = sess.run(fetches, feed_dict=feed_dict)
          fetched_values = self.session.run(fetches, feed_dict=feed_dict)
          loss = fetched_values[-1]
          avg_loss += loss
          n_batches += 1
          self.global_step += 1
          n_samples += 1
          if self.tensorboard and n_samples % self.tensorboard_log_frequency == 0:
              summary = sess.run(
            summary = self.session.run(
                self._get_tf("summary_op"), feed_dict=feed_dict)
            self._log_tensorboard(summary)
        except OutOfRangeError:
          break
        if self.global_step % checkpoint_interval == checkpoint_interval - 1:
            saver.save(sess, self.save_file, global_step=self.global_step)
            self.last_checkpoint = saver.last_checkpoints[-1]
          saver.save(self.session, self.save_file, global_step=self.global_step)
          avg_loss = float(avg_loss) / n_batches
          print('Ending global_step %d: Average loss %g' % (self.global_step,
                                                            avg_loss))
@@ -179,8 +218,7 @@ class TensorGraph(Model):
      avg_loss = float(avg_loss) / n_batches
      print('Ending global_step %d: Average loss %g' % (self.global_step,
                                                        avg_loss))
        saver.save(sess, self.save_file, global_step=self.global_step)
        self.last_checkpoint = saver.last_checkpoints[-1]
      saver.save(self.session, self.save_file, global_step=self.global_step)
      time2 = time.time()
      print("TIMING: model fitting took %0.3f s" % (time2 - time1))

@@ -256,9 +294,6 @@ class TensorGraph(Model):
    elif not isinstance(outputs, collections.Sequence):
      outputs = [outputs]
    with self._get_tf("Graph").as_default():
      with tf.Session() as sess:
        saver = tf.train.Saver()
        self._initialize_weights(sess, saver)
      out_tensors = [x.out_tensor for x in self.outputs]
      # Gather results for each output
      results = [[] for out in out_tensors]
@@ -268,7 +303,7 @@ class TensorGraph(Model):
            for k, v in six.iteritems(feed_dict)
        }
        feed_dict[self._training_placeholder] = 0.0
          feed_results = sess.run(out_tensors, feed_dict=feed_dict)
        feed_results = self.session.run(out_tensors, feed_dict=feed_dict)
        if len(feed_results) > 1:
          if len(transformers):
            raise ValueError("Does not support transformations "
@@ -394,6 +429,7 @@ class TensorGraph(Model):
          self.rnn_final_states += node_layer.rnn_final_states
          self.rnn_zero_states += node_layer.rnn_zero_states
          node_layer.add_summary_to_tg()
      self.session = tf.Session()

      self.built = True

@@ -488,10 +524,12 @@ class TensorGraph(Model):
    rnn_initial_states = self.rnn_initial_states
    rnn_final_states = self.rnn_final_states
    rnn_zero_states = self.rnn_zero_states
    session = self.session
    self.tensor_objects = {}
    self.rnn_initial_states = []
    self.rnn_final_states = []
    self.rnn_zero_states = []
    self.session = None
    out_tensors = []
    if self.built:
      must_restore = True
@@ -526,6 +564,7 @@ class TensorGraph(Model):
    self.rnn_initial_states = rnn_initial_states
    self.rnn_final_states = rnn_final_states
    self.rnn_zero_states = rnn_zero_states
    self.session = session

  def evaluate_generator(self,
                         feed_dict_generator,
@@ -562,8 +601,10 @@ class TensorGraph(Model):
    if not self.built:
      self.build()
    with self._get_tf("Graph").as_default():
      if layer.variable_scope == '':
        return []
      return tf.get_collection(
          tf.GraphKeys.GLOBAL_VARIABLES, scope=layer.variable_scope)
          tf.GraphKeys.TRAINABLE_VARIABLES, scope=layer.variable_scope)

  def get_global_step(self):
    return self._get_tf("GlobalStep")
@@ -610,25 +651,16 @@ class TensorGraph(Model):
        self.tensor_objects['GlobalStep'] = tf.Variable(0, trainable=False)
    return self._get_tf(obj)

  def _initialize_weights(self, sess, saver):
    """
    Parameters
    ----------
    sess: tf.Session
      The Session must be open
    saver: tf.train.Saver
      A saver object to save/restore checkpoints

    Returns
    -------

    """
    if self.last_checkpoint is None:
      sess.run(tf.global_variables_initializer())
      saver.save(sess, self.save_file, global_step=self.global_step)
      self.last_checkpoint = saver.last_checkpoints[-1]
    else:
      saver.restore(sess, self.last_checkpoint)
  def restore(self):
    """Reload the values of all variables from the most recent checkpoint file."""
    if not self.built:
      self.build()
    last_checkpoint = tf.train.latest_checkpoint(self.model_dir)
    if last_checkpoint is None:
      raise ValueError('No checkpoint found')
    with self._get_tf("Graph").as_default():
      saver = tf.train.Saver()
      saver.restore(self.session, last_checkpoint)

  def get_num_tasks(self):
    return len(self.outputs)
@@ -644,6 +676,10 @@ class TensorGraph(Model):
    with open(pickle_name, 'rb') as fout:
      tensorgraph = pickle.load(fout)
      tensorgraph.built = False
      try:
        tensorgraph.restore()
      except ValueError:
        pass  # No checkpoint to load
      return tensorgraph

  def __del__(self):
Loading