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

Fixed queue issues (hopefully)

parent dd8ae4e4
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -76,8 +76,9 @@ class Layer(object):
class TensorWrapper(Layer):
  """Used to wrap a tensorflow tensor."""

  def __init__(self, out_tensor):
  def __init__(self, out_tensor, **kwargs):
    self.out_tensor = out_tensor
    super(TensorWrapper, self).__init__(**kwargs)

  def create_tensor(self, in_layers=None):
    """Take no actions."""
@@ -606,7 +607,7 @@ class InputFifoQueue(Layer):
      self.dtypes = [tf.float32] * len(self.shapes)
    self.queue = tf.FIFOQueue(
        self.capacity, self.dtypes, shapes=self.shapes, names=self.names)
    feed_dict = {x.out_tensor.name: x.out_tensor for x in in_layers}
    feed_dict = {x.name: x.out_tensor for x in in_layers}
    self.out_tensor = self.queue.enqueue(feed_dict)
    self.close_op = self.queue.close()
    self.out_tensors = self.queue.dequeue()
+1 −1
Original line number Diff line number Diff line
import tensorflow as tf
from deepchem.models.tensorgraph.tensor_graph import TensorGraph
from deepchem.models.tensorgraph.layers import Input, Dense, Concat, SoftMax, SoftMaxCrossEntropy, Layer, \
  GraphConv, BatchNormLayer, GraphPoolLayer, GraphGather, WeightedError
  GraphConv, BatchNorm, GraphPool, GraphGather, WeightedError
from deepchem.metrics import to_one_hot
from deepchem.feat.mol_graphs import ConvMol
import time
+3 −3
Original line number Diff line number Diff line
@@ -99,7 +99,7 @@ class WeaveTensorGraph(TensorGraph):

        label = Label(shape=(None, 1))
        self.labels_fd.append(label)
        cost = L2LossLayer(in_layers=[label, regression])
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)

    all_cost = Concat(in_layers=costs)
@@ -283,7 +283,7 @@ class DTNNTensorGraph(TensorGraph):

      label = Label(shape=(None, 1))
      self.labels_fd.append(label)
      cost = L2LossLayer(in_layers=[label, regression])
      cost = L2Loss(in_layers=[label, regression])
      costs.append(cost)

    all_cost = Concat(in_layers=costs)
@@ -464,7 +464,7 @@ class DAGTensorGraph(TensorGraph):

        label = Label(shape=(None, 1))
        self.labels_fd.append(label)
        cost = L2LossLayer(in_layers=[label, regression])
        cost = L2Loss(in_layers=[label, regression])
        costs.append(cost)

    all_cost = Concat(in_layers=costs)
+5 −3
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ from deepchem.models.tensorgraph.layers import SoftMax
from deepchem.models.tensorgraph.layers import WeightedError
from deepchem.models.tensorgraph.layers import VinaFreeEnergy
from deepchem.models.tensorgraph.layers import WeightedLinearCombo
from deepchem.models.tensorgraph.layers import TensorWrapper

import deepchem as dc

@@ -324,10 +325,11 @@ class TestLayers(test_util.TensorFlowTestCase):
    batch_size = 10
    n_features = 5
    in_tensor = np.random.rand(batch_size, n_features)
    tf.reset_default_graph()
    with self.test_session() as sess:
      in_tensor = tf.convert_to_tensor(
          in_tensor, dtype=tf.float32, name="input")
      InputFifoQueue([(batch_size, n_features)], ["input:0"])(in_tensor)
      in_tensor = TensorWrapper(
          tf.convert_to_tensor(in_tensor, dtype=tf.float32), name="input")
      InputFifoQueue([(batch_size, n_features)], ["input"])(in_tensor)

  def test_graph_conv(self):
    """Test that GraphConv can be invoked."""
+36 −41
Original line number Diff line number Diff line
@@ -12,9 +12,6 @@ from deepchem.models.tensorgraph.layers import Feature, Label
from deepchem.models.tensorgraph.layers import ReduceSquareDifference
from deepchem.models.tensorgraph.tensor_graph import TensorGraph

# TODO(rbharath): Queues were causing strange test failures.
# Turned off all queues for now.


class TestTensorGraph(unittest.TestCase):
  """
@@ -33,7 +30,7 @@ class TestTensorGraph(unittest.TestCase):
    label = Label(shape=(None, 2))
    smce = SoftMaxCrossEntropy(in_layers=[label, dense])
    loss = ReduceMean(in_layers=[smce])
    tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    tg.add_output(output)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=10)
@@ -69,7 +66,7 @@ class TestTensorGraph(unittest.TestCase):

    total_loss = ReduceMean(in_layers=entropies)

    tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    for output in outputs:
      tg.add_output(output)
    tg.set_loss(total_loss)
@@ -93,7 +90,7 @@ class TestTensorGraph(unittest.TestCase):
    dense = Dense(out_channels=1, in_layers=[features])
    label = Label(shape=(None, 1))
    loss = ReduceSquareDifference(in_layers=[dense, label])
    tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    tg.add_output(dense)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=10)
@@ -128,7 +125,7 @@ class TestTensorGraph(unittest.TestCase):

    total_loss = ReduceMean(in_layers=losses)

    tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    for output in outputs:
      tg.add_output(output)
    tg.set_loss(total_loss)
@@ -174,7 +171,6 @@ class TestTensorGraph(unittest.TestCase):
    smce = SoftMaxCrossEntropy(in_layers=[label, dense])
    loss = ReduceMean(in_layers=[smce])
    tg = dc.models.TensorGraph(
        use_queue=False,
        tensorboard=True,
        tensorboard_log_frequency=1,
        learning_rate=0.1,
@@ -201,7 +197,7 @@ class TestTensorGraph(unittest.TestCase):
    label = Label(shape=(None, 2))
    smce = SoftMaxCrossEntropy(in_layers=[label, dense])
    loss = ReduceMean(in_layers=[smce])
    tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    tg.add_output(output)
    tg.set_loss(loss)
    tg.fit(dataset, nb_epoch=1)
@@ -212,44 +208,43 @@ class TestTensorGraph(unittest.TestCase):
    prediction2 = np.squeeze(tg1.predict_proba_on_batch(X))
    assert_true(np.all(np.isclose(prediction, prediction2, atol=0.01)))

  # TODO(rbharath): Failing with strange bugs. Turn test back on!
  #def test_shared_layer(self):
  #  n_data_points = 20
  #  n_features = 2
  def test_shared_layer(self):
    n_data_points = 20
    n_features = 2

  #  X = np.random.rand(n_data_points, n_features)
  #  y1 = np.array([[0, 1] for x in range(n_data_points)])
  #  X = NumpyDataset(X)
  #  ys = [NumpyDataset(y1)]
    X = np.random.rand(n_data_points, n_features)
    y1 = np.array([[0, 1] for x in range(n_data_points)])
    X = NumpyDataset(X)
    ys = [NumpyDataset(y1)]

  #  databag = Databag()
    databag = Databag()

  #  features = Feature(shape=(None, n_features))
  #  databag.add_dataset(features, X)
    features = Feature(shape=(None, n_features))
    databag.add_dataset(features, X)

  #  outputs = []
    outputs = []

  #  label = Label(shape=(None, 2))
  #  dense1 = Dense(out_channels=2, in_layers=[features])
  #  dense2 = dense1.shared(in_layers=[features])
  #  output1 = SoftMax(in_layers=[dense1])
  #  output2 = SoftMax(in_layers=[dense2])
  #  smce = SoftMaxCrossEntropy(in_layers=[label, dense1])
    label = Label(shape=(None, 2))
    dense1 = Dense(out_channels=2, in_layers=[features])
    dense2 = dense1.shared(in_layers=[features])
    output1 = SoftMax(in_layers=[dense1])
    output2 = SoftMax(in_layers=[dense2])
    smce = SoftMaxCrossEntropy(in_layers=[label, dense1])

  #  outputs.append(output1)
  #  outputs.append(output2)
  #  databag.add_dataset(label, ys[0])
    outputs.append(output1)
    outputs.append(output2)
    databag.add_dataset(label, ys[0])

  #  total_loss = ReduceMean(in_layers=[smce])
    total_loss = ReduceMean(in_layers=[smce])

  #  tg = dc.models.TensorGraph(learning_rate=0.1, use_queue=False)
  #  for output in outputs:
  #    tg.add_output(output)
  #  tg.set_loss(total_loss)
    tg = dc.models.TensorGraph(learning_rate=0.1)
    for output in outputs:
      tg.add_output(output)
    tg.set_loss(total_loss)

  #  tg.fit_generator(
  #      databag.iterbatches(
  #          epochs=1, batch_size=tg.batch_size, pad_batches=True))
  #  prediction = tg.predict_proba_on_generator(databag.iterbatches())
  #  assert_true(
  #      np.all(np.isclose(prediction[:, 0], prediction[:, 1], atol=0.01)))
    tg.fit_generator(
        databag.iterbatches(
            epochs=1, batch_size=tg.batch_size, pad_batches=True))
    prediction = tg.predict_proba_on_generator(databag.iterbatches())
    assert_true(
        np.all(np.isclose(prediction[:, 0], prediction[:, 1], atol=0.01)))