Commit 7c69edd0 authored by Peter Eastman's avatar Peter Eastman
Browse files

Can set the optimizer used by TensorGraph

parent 47e7c512
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -19,8 +19,8 @@ from deepchem.models.tensorflow_models import TensorflowRegressor
from deepchem.metrics import to_one_hot


from deepchem.models.tensorgraph.tensor_graph import TensorGraph
from deepchem.models.tensorgraph.layers import Feature, Label, Weights, WeightedError, Dense, Dropout, Reshape, SoftMaxCrossEntropy, L2Loss, Initializer
from deepchem.models.tensorgraph.tensor_graph import TensorGraph, TFWrapper
from deepchem.models.tensorgraph.layers import Feature, Label, Weights, WeightedError, Dense, Dropout, Reshape, SoftMaxCrossEntropy, L2Loss

class TensorGraphMultiTaskClassifier(TensorGraph):
  def __init__(self,
@@ -46,8 +46,8 @@ class TensorGraphMultiTaskClassifier(TensorGraph):

    for size, weight_stddev, bias_const, dropout in zip(layer_sizes, weight_init_stddevs, bias_init_consts, dropouts):
      layer = Dense(in_layers=[prev_layer], out_channels=size, activation_fn=tf.nn.relu,
                    weights_initializer=Initializer(tf.truncated_normal_initializer, stddev=weight_stddev),
                    biases_initializer=Initializer(tf.constant_initializer, value=bias_const))
                    weights_initializer=TFWrapper(tf.truncated_normal_initializer, stddev=weight_stddev),
                    biases_initializer=TFWrapper(tf.constant_initializer, value=bias_const))
      if dropout > 0.0:
        layer = Dropout(dropout, in_layers=[layer])
      prev_layer = layer
@@ -107,8 +107,8 @@ class TensorGraphMultiTaskRegressor(TensorGraph):

    for size, weight_stddev, bias_const, dropout in zip(layer_sizes, weight_init_stddevs, bias_init_consts, dropouts):
      layer = Dense(in_layers=[prev_layer], out_channels=size, activation_fn=tf.nn.relu,
                    weights_initializer=Initializer(tf.truncated_normal_initializer, stddev=weight_stddev),
                    biases_initializer=Initializer(tf.constant_initializer, value=bias_const))
                    weights_initializer=TFWrapper(tf.truncated_normal_initializer, stddev=weight_stddev),
                    biases_initializer=TFWrapper(tf.constant_initializer, value=bias_const))
      if dropout > 0.0:
        layer = Dropout(dropout, in_layers=[layer])
      prev_layer = layer
+1 −1
Original line number Diff line number Diff line
from deepchem.models.tensorgraph.tensor_graph import TensorGraphfrom deepchem.models.tensorgraph import models
 No newline at end of file
from deepchem.models.tensorgraph.tensor_graph import TensorGraph, TFWrapperfrom deepchem.models.tensorgraph import models
 No newline at end of file
+0 −20
Original line number Diff line number Diff line
@@ -98,26 +98,6 @@ def convert_to_layers(in_layers):
  return layers


class Initializer(object):
  """This class exists as a workaround for Tensorflow initializers not being picklable."""

  def __init__(self, initializer_class, **kwargs):
    """Create an Initializer for constructing Tensorflow initializers.

    Parameters
    ----------
    initializer_class: class
      the type of initializer to create
    kwargs:
      any other arguments will be passed on to the Tensorflow initializer's constructor
    """
    self.initializer_class = initializer_class
    self.kwargs = kwargs

  def __call__(self):
    return self.initializer_class(**self.kwargs)


class Conv1D(Layer):

  def __init__(self, width, out_channels, **kwargs):
+36 −7
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ class TensorGraph(Model):
  def __init__(self,
               tensorboard=False,
               tensorboard_log_frequency=100,
               learning_rate=0.001,
               batch_size=100,
               random_seed=None,
               use_queue=True,
@@ -36,8 +35,6 @@ class TensorGraph(Model):
      Should we log to model_dir data for tensorboard?
    tensorboard_log_frequency: int
      How many training batches before logging tensorboard?
    learning_rate: float
      learning rate for optimizer
    batch_size: int
      default batch size for training and evaluating
    use_queue: boolean
@@ -63,6 +60,7 @@ class TensorGraph(Model):
    self.loss = None
    self.built = False
    self.queue_installed = False
    self.optimizer = TFWrapper(tf.train.AdamOptimizer, learning_rate=0.001, beta1=0.9, beta2=0.999)

    # Singular place to hold Tensor objects which don't serialize
    # These have to be reconstructed on restoring from pickle
@@ -81,7 +79,6 @@ class TensorGraph(Model):
    self.last_checkpoint = None
    self.use_queue = use_queue

    self.learning_rate = learning_rate
    self.batch_size = batch_size
    self.random_seed = random_seed
    super(TensorGraph, self).__init__(**kwargs)
@@ -368,6 +365,14 @@ class TensorGraph(Model):
    self._add_layer(layer)
    self.outputs.append(layer)

  def set_optimizer(self, optimizer):
    """Set the optimizer to use for fitting.

    The argument should be a callable object (most often a TFWrapper) that constructs
    a Tensorflow optimizer when called.
    """
    self.optimizer = optimizer

  def save(self):
    # Remove out_tensor from the object to be pickled
    must_restore = False
@@ -443,9 +448,7 @@ class TensorGraph(Model):
    elif obj == "FileWriter":
      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.tensor_objects['train_op'] = self.optimizer().minimize(self.loss.out_tensor)
    elif obj == 'summary_op':
      self.tensor_objects['summary_op'] = tf.summary.merge_all(
          key=tf.GraphKeys.SUMMARIES)
@@ -518,3 +521,29 @@ def _enqueue_batch(tg, generator, graph, sess, coord):
    sess.run(tg.input_queue.close_op)
    coord.num_samples = num_samples
    coord.request_stop()


class TFWrapper(object):
  """This class exists as a workaround for Tensorflow objects not being picklable.

  The job of a TFWrapper is to create Tensorflow objects by passing defined arguments
  to a constructor.  There are cases where we really want to store Tensorflow objects
  of various sorts (optimizers, initializers, etc.), but we can't because they cannot
  be pickled.  So instead we store a TFWrapper that creates the object when needed.
  """

  def __init__(self, tf_class, **kwargs):
    """Create a TFWrapper for constructing a Tensorflow object.

    Parameters
    ----------
    tf_class: class
      the type of object to create
    kwargs:
      any other arguments will be passed on to the object's constructor
    """
    self.tf_class = tf_class
    self.kwargs = kwargs

  def __call__(self):
    return self.tf_class(**self.kwargs)