Commit 30669439 authored by Vignesh's avatar Vignesh
Browse files

Fix errors in docstring, remove redundancies in tests

parent 7308dd24
Loading
Loading
Loading
Loading
+22 −21
Original line number Diff line number Diff line
@@ -1020,8 +1020,6 @@ class KerasModel(Model):
    ----------
    source_model: dc.models.KerasModel
        Source model to create value map from
    include_top: bool, default True
        if true, copies the last dense layer
    """
    value_map = {}
    source_vars = source_model.model.trainable_variables
@@ -1044,37 +1042,40 @@ class KerasModel(Model):
                           model_dir=None,
                           include_top=True,
                           **kwargs):
    """Copies variable values between pretrained model and current model. This
    method assumes that the variable values of the pretrained model are saved to
    disk. source_model is a dc.KerasModel with the same architecture as the
    pretrained model. The variable values are then restored to the source_model.
    assignment_map is a dictionary mapping the variables from the source model
    to those in the current model. If no assignment_map is provided, one is made
    from scratch and assumes the model is composed of several different layers,
    with the final one being a dense layer. include_top is used to control whether
    or not the final dense layer is used. The default assignment map is useful in
    cases where the type of task is different (classification vs regression) and/or
    number of tasks in the setting.
    """Copies variable values from a pretrained model. `source_model` can either
    be a pretrained model or a model with the same architecture. `value_map`
    is a variable-value dictionary. If no `value_map` is provided, the variable
    values are restored to the `source_model` from a checkpoint and a default
    `value_map` is created. `assignment_map` is a dictionary mapping variables
    from the `source_model` to the current model. If no `assignment_map` is
    provided, one is made from scratch and assumes the model is composed of
    several different layers, with the final one being a dense layer. include_top
    is used to control whether or not the final dense layer is used. The default
    assignment map is useful in cases where the type of task is different
    (classification vs regression) and/or number of tasks in the setting.

    Parameters
    ----------
    source_model: dc.KerasModel, required
      Model which has the same topology and architecture as the pretrained
      model.
      source_model can either be the pretrained model or a dc.KerasModel with
      the same architecture as the pretrained model. It is used to restore from
      a checkpoint, if value_map is None and to create a default assignment map
      if assignment_map is None
    assignment_map: Dict, default None
      Dictionary containing layer mapping between source and current model layers
      Dictionary mapping the source_model variables and current model variables
    value_map: Dict, default None
      Dictionary containing source model trainable variables mapped to numpy
      arrays
      Dictionary containing source_model trainable variables mapped to numpy
      arrays. If value_map is None, the values are restored and a default
      variable map is created using the restored values
    checkpoint: str, default None
      the path to the checkpoint file to load.  If this is None, the most recent
      checkpoint will be chosen automatically.  Call get_checkpoints() to get a
      list of all available checkpoints.
      list of all available checkpoints
    model_dir: str, default None
      Restore model from custom model directory if needed
    include_top: bool, default True
        if True, copies the weights and bias associated with the final dense layer.
        Used only when assignment map is None.
        if True, copies the weights and bias associated with the final dense
        layer. Used only when assignment map is None
    """

    self._ensure_built()
+17 −67
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ class MLP(dc.models.KerasModel):
class TestPretrained(unittest.TestCase):

  def setUp(self):
    model_dir = "./MLP/"
    self.feature_dim = 2
    self.hidden_layer_size = 10
    data_points = 10
@@ -46,22 +45,11 @@ class TestPretrained(unittest.TestCase):

    self.dataset = dc.data.NumpyDataset(X, y)

    model = MLP(
        hidden_layer_size=self.hidden_layer_size,
        feature_dim=self.feature_dim,
        model_dir=model_dir,
        batch_size=10)

    model.fit(self.dataset, nb_epoch=1000)
    predictions = np.squeeze(model.predict_on_batch(self.dataset.X))
    np.testing.assert_array_almost_equal(self.dataset.y, np.round(predictions))

  def test_load_from_pretrained_graph_mode(self):
    """Tests loading pretrained model in graph mode."""
    source_model = MLP(
        hidden_layer_size=self.hidden_layer_size,
        feature_dim=self.feature_dim,
        model_dir=None,
        batch_size=10)

    source_model.fit(self.dataset, nb_epoch=1000, checkpoint_interval=0)
@@ -78,6 +66,9 @@ class TestPretrained(unittest.TestCase):
    for idx, dest_var in enumerate(dest_vars):
      source_var = source_model.model.trainable_variables[idx]
      assignment_map[source_var] = dest_var
      if tf.executing_eagerly():
        value_map[source_var] = source_var.numpy()
      else:
        value_map[source_var] = source_var.eval(session=source_model.session)

    dest_model.load_from_pretrained(
@@ -86,50 +77,25 @@ class TestPretrained(unittest.TestCase):
        value_map=value_map)

    for source_var, dest_var in assignment_map.items():
      np.testing.assert_array_almost_equal(
          source_var.eval(session=source_model.session),
          dest_var.eval(session=dest_model.session))
      if tf.executing_eagerly():
        source_val = source_var.numpy()
        dest_val = dest_var.numpy()
      else:
        source_val = source_var.eval(session=source_model.session)
        dest_val = dest_var.eval(session=dest_model.session)
      np.testing.assert_array_almost_equal(source_val, dest_val)

  def test_load_from_pretrained_eager_mode(self):
    """Tests loading pretrained model in eager execution mode."""
    with context.eager_mode():
      source_model = MLP(
          hidden_layer_size=self.hidden_layer_size,
          feature_dim=self.feature_dim,
          model_dir=None,
          batch_size=10)

      source_model.fit(self.dataset, nb_epoch=1000, checkpoint_interval=0)

      dest_model = MLP(
          feature_dim=self.feature_dim,
          hidden_layer_size=self.hidden_layer_size,
          n_tasks=10)

      assignment_map = dict()
      value_map = dict()
      dest_vars = dest_model.model.trainable_variables[:-2]

      for idx, dest_var in enumerate(dest_vars):
        source_var = source_model.model.trainable_variables[idx]
        assignment_map[source_var] = dest_var
        value_map[source_var] = source_var.numpy()

      dest_model.load_from_pretrained(
          source_model=source_model,
          value_map=value_map,
          assignment_map=assignment_map)

      for source_var, dest_var in assignment_map.items():
        np.testing.assert_array_almost_equal(source_var.numpy(),
                                             dest_var.numpy())
      self.test_load_from_pretrained_graph_mode()

  def test_restore_equivalency_graph_mode(self):
    """Test for restore based pretrained model loading in graph mode."""
    source_model = MLP(
        model_dir="./MLP/",
        feature_dim=self.feature_dim,
        hidden_layer_size=self.hidden_layer_size)
        feature_dim=self.feature_dim, hidden_layer_size=self.hidden_layer_size)

    source_model.fit(self.dataset, nb_epoch=1000)

    dest_model = MLP(
        feature_dim=self.feature_dim, hidden_layer_size=self.hidden_layer_size)
@@ -138,6 +104,7 @@ class TestPretrained(unittest.TestCase):
        source_model=source_model,
        assignment_map=None,
        value_map=None,
        model_dir=None,
        include_top=True)

    predictions = np.squeeze(dest_model.predict_on_batch(self.dataset.X))
@@ -146,21 +113,4 @@ class TestPretrained(unittest.TestCase):
  def test_restore_equivalency_eager_mode(self):
    """Test for restore based pretrained model loading in eager mode."""
    with context.eager_mode():
      source_model = MLP(
          model_dir="./MLP/",
          feature_dim=self.feature_dim,
          hidden_layer_size=self.hidden_layer_size)

      dest_model = MLP(
          feature_dim=self.feature_dim,
          hidden_layer_size=self.hidden_layer_size)

      dest_model.load_from_pretrained(
          source_model=source_model,
          assignment_map=None,
          value_map=None,
          include_top=True)

      predictions = np.squeeze(dest_model.predict_on_batch(self.dataset.X))
      np.testing.assert_array_almost_equal(self.dataset.y,
                                           np.round(predictions))
      self.test_restore_equivalency_graph_mode()