Commit 65764c86 authored by Peter Eastman's avatar Peter Eastman
Browse files

Implemented weight decay

parent 7c69edd0
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ from deepchem.metrics import to_one_hot


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

class TensorGraphMultiTaskClassifier(TensorGraph):
  def __init__(self,
@@ -29,6 +29,8 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
               layer_sizes=[1000],
               weight_init_stddevs=[0.02],
               bias_init_consts=[1.0],
               weight_decay_penalty=0.0,
               weight_decay_penalty_type="l2",
               dropouts=[0.5],
               n_classes=2,
               **kwargs):
@@ -60,6 +62,8 @@ class TensorGraphMultiTaskClassifier(TensorGraph):
    weights = Weights(shape=(None, n_tasks))
    loss = Reshape(shape=(-1, n_tasks), in_layers=[SoftMaxCrossEntropy(in_layers=[labels, output])])
    weighted_loss = WeightedError(in_layers=[loss, weights])
    if weight_decay_penalty != 0.0:
      weighted_loss = WeightDecay(weight_decay_penalty, weight_decay_penalty_type, in_layers=[weighted_loss])
    self.set_loss(weighted_loss)


@@ -92,6 +96,8 @@ class TensorGraphMultiTaskRegressor(TensorGraph):
               layer_sizes=[1000],
               weight_init_stddevs=[0.02],
               bias_init_consts=[1.0],
               weight_decay_penalty=0.0,
               weight_decay_penalty_type="l2",
               dropouts=[0.5],
               **kwargs):
    super().__init__(mode='regression', **kwargs)
@@ -121,6 +127,8 @@ class TensorGraphMultiTaskRegressor(TensorGraph):
    weights = Weights(shape=(None, n_tasks))
    loss = Reshape(shape=(-1, n_tasks), in_layers=[L2Loss(in_layers=[labels, output])])
    weighted_loss = WeightedError(in_layers=[loss, weights])
    if weight_decay_penalty != 0.0:
      weighted_loss = WeightDecay(weight_decay_penalty, weight_decay_penalty_type, in_layers=[weighted_loss])
    self.set_loss(weighted_loss)


+31 −0
Original line number Diff line number Diff line
@@ -1303,6 +1303,37 @@ class Dropout(Layer):
    return self.out_tensor


class WeightDecay(Layer):
  """Apply a weight decay penalty.


  The input should be the loss value.  This layer adds a weight decay penalty to it
  and outputs the sum.
  """

  def __init__(self, penalty, penalty_type, **kwargs):
    """Create a weight decay penalty layer.

    Parameters
    ----------
    penalty: float
      magnitude of the penalty term
    penalty_type: str
      type of penalty to compute, either 'l1' or 'l2'
    """
    self.penalty = penalty
    self.penalty_type = penalty_type
    super(WeightDecay, self).__init__(**kwargs)

  def create_tensor(self, in_layers=None, **kwargs):
    if in_layers is None:
      in_layers = self.in_layers
    in_layers = convert_to_layers(in_layers)
    parent_tensor = in_layers[0].out_tensor
    self.out_tensor = parent_tensor+model_ops.weight_decay(self.penalty_type, self.penalty)
    return self.out_tensor


class AtomicConvolution(Layer):

  def __init__(self,