Commit 2a62b791 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Fixing some issues

parent 69116772
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ class Sequential(TensorGraph):
      if kwargs["use_queue"]:
        raise ValueError("Sequential doesn't support queues.")
    kwargs["use_queue"] = False
    self.layer_list = []
    self._layer_list = []
    self._built = False
    super(Sequential, self).__init__(**kwargs)

  def add(self, layer):
@@ -40,7 +41,7 @@ class Sequential(TensorGraph):
    layer: Layer
      Adds layer to this graph.
    """
    self.layer_list.append(layer)
    self._layer_list.append(layer)

  def fit(self, dataset, loss, **kwargs):
    """Fits on the specified dataset.
@@ -55,6 +56,8 @@ class Sequential(TensorGraph):
      Only "binary_crossentropy" for now.
    """
    X_shape, y_shape, _, _ = dataset.get_shape()
    # Calling fit() for first time
    if not self._built:
      feature_shape = X_shape[1:]
      label_shape = y_shape[1:]
      # Add in features
@@ -66,7 +69,7 @@ class Sequential(TensorGraph):

      # Add in all layers
      prev_layer = features
    for ind, layer in enumerate(self.layer_list):
      for ind, layer in enumerate(self._layer_list):
        if not len(layer.in_layers) == 0:
          raise ValueError("Cannot specify in_layers for Sequential.")
        layer.in_layers += [prev_layer]
@@ -82,5 +85,6 @@ class Sequential(TensorGraph):
      else:
        # TODO(rbharath): Add in support for additional losses.
        raise ValueError("Unsupported loss.")
    self._built = True

    super(Sequential, self).fit(dataset, **kwargs)
+13 −0
Original line number Diff line number Diff line
@@ -23,3 +23,16 @@ class TestSequential(unittest.TestCase):
    model.fit(dataset, loss="binary_crossentropy", nb_epoch=1000)
    prediction = np.squeeze(model.predict_on_batch(X))
    assert_true(np.all(np.isclose(prediction, y, atol=0.4)))

  def test_fit_twice(self):
    n_data_points = 20
    n_features = 2
    X = np.random.rand(n_data_points, n_features)
    y = [[0, 1] for x in range(n_data_points)]
    dataset = dc.data.NumpyDataset(X, y)
    model = dc.models.Sequential(learning_rate=0.01)
    model.add(Dense(out_channels=2))
    model.add(SoftMax())
    # Should be able to call fit twice without failure.
    model.fit(dataset, loss="binary_crossentropy", nb_epoch=1000)
    model.fit(dataset, loss="binary_crossentropy", nb_epoch=1000)