Commit 44819eb1 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Slowly start integrating with test suite

parent 39e8732b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ from __future__ import unicode_literals

from deepchem.nn.copy import Input
from deepchem.nn.copy import Dense
from deepchem.nn.copy import Dropout
from deepchem.nn.copy import BatchNormalization
from deepchem.nn.layers import GraphConv
from deepchem.nn.layers import GraphPool
+10 −30
Original line number Diff line number Diff line
@@ -336,7 +336,7 @@ class Layer(object):
      elif 'input_shape' in kwargs:
        batch_input_shape = (None,) + tuple(kwargs['input_shape'])
      self.batch_input_shape = batch_input_shape
      input_dtype = kwargs.get('input_dtype', K.floatx())
      input_dtype = kwargs.get('input_dtype', tf.float32())
      self.input_dtype = input_dtype

  @property
@@ -1006,20 +1006,6 @@ class Layer(object):
    """
    return cls(**config)

  def count_params(self):
    """Returns the total number of floats (or ints)
    composing the weights of the layer.
    """
    if not self.built:
      if self.__class__.__name__ == 'Sequential':
        self.build()
      else:
        raise RuntimeError('You tried to call `count_params` on ' +
                           self.name + ', but the layer isn\'t built. '
                           'You can build it manually via: `' +
                           self.name + '.build(batch_input_shape)`.')
    return sum([K.count_params(p) for p in self.weights])

class InputLayer(Layer):
    """Layer to be used as an entry point into a graph.
    It can either wrap an existing tensor (pass an `input_tensor` argument)
@@ -1031,13 +1017,11 @@ class InputLayer(Layer):
        input_dtype: Datatype of the input.
        input_tensor: Optional tensor to use as layer input
            instead of creating a placeholder.
        sparse: Boolean, whether the placeholder created
            is meant to be sparse.
        name: Name of the layer (string).
    """

    def __init__(self, input_shape=None, batch_input_shape=None,
                 input_dtype=None, input_tensor=None, sparse=False, name=None):
                 input_dtype=None, input_tensor=None, name=None):
      self.input_spec = None
      self.supports_masking = False
      self.uses_learning_phase = False
@@ -1048,10 +1032,11 @@ class InputLayer(Layer):
      self.inbound_nodes = []
      self.outbound_nodes = []
      self.constraints = {}
      self.sparse = sparse

      if not name:
        prefix = 'input'
        # TODO(rbharath): Keras uses a global var here to maintain
        # unique counts. This seems dangerous. How does tensorflow handle?
        name = prefix + '_' + str(K.get_uid(prefix))
      self.name = name

@@ -1082,7 +1067,7 @@ class InputLayer(Layer):

      if not input_dtype:
        if input_tensor is None:
          input_dtype = K.floatx()
          input_dtype = tf.float32()
        else:
          input_dtype = K.dtype(input_tensor)

@@ -1090,9 +1075,8 @@ class InputLayer(Layer):
      self.input_dtype = input_dtype

      if input_tensor is None:
        input_tensor = K.placeholder(shape=batch_input_shape,
                                     dtype=input_dtype,
                                     sparse=self.sparse,
        input_tensor = K.placeholder(dtype=input_dtype,
                                     shape=batch_input_shape,
                                     name=self.name)
      else:
        input_tensor._keras_shape = batch_input_shape
@@ -1114,14 +1098,12 @@ class InputLayer(Layer):
    def get_config(self):
      config = {'batch_input_shape': self.batch_input_shape,
                'input_dtype': self.input_dtype,
                'sparse': self.sparse,
                'name': self.name}
      return config


def Input(shape=None, batch_shape=None,
          name=None, dtype=K.floatx(), sparse=False,
          tensor=None):
          name=None, dtype=K.floatx(), tensor=None):
  """`Input()` is used to instantiate a Keras tensor.
  A Keras tensor is a tensor object from the underlying backend
  (Theano or TensorFlow), which we augment with certain
@@ -1150,8 +1132,6 @@ def Input(shape=None, batch_shape=None,
          It will be autogenerated if it isn't provided.
      dtype: The data type expected by the input, as a string
          (`float32`, `float64`, `int32`...)
      sparse: A boolean specifying whether the placeholder
          to be created is sparse.
  # Example
      ```python
      # this is a logistic regression in Keras
@@ -1169,7 +1149,6 @@ def Input(shape=None, batch_shape=None,
    batch_shape = (None,) + tuple(shape)
  input_layer = InputLayer(batch_input_shape=batch_shape,
                           name=name, input_dtype=dtype,
                           sparse=sparse,
                           input_tensor=tensor)
  # Return tensor including _keras_shape and _keras_history.
  # Note that in this case train_output and test_output are the same pointer.
@@ -1358,7 +1337,8 @@ class Dropout(Layer):
      noise_shape = self._get_noise_shape(x)

      def dropped_inputs():
        return K.dropout(x, self.p, noise_shape, seed=self.seed)
        retain_prob = 1 - self.p
        return tf.nn.dropout(x * 1., retain_prob, noise_shape, seed=self.seed)
      x = K.in_train_phase(dropped_inputs, lambda: x)
    return x

+18 −0
Original line number Diff line number Diff line
@@ -25,6 +25,24 @@ class TestLayers(test_util.TensorFlowTestCase):
    super(TestLayers, self).setUp()
    self.root = '/tmp'

  def test_dense(self):
    """Tests dense layer class can be initialized."""
    with self.test_session() as sess:
      dense = dc.nn.Dense(32, input_dim=16)

  def test_dropout(self):
    """Tests that dropout can be initialized."""
    with self.test_session() as sess:
      dropout = dc.nn.Dropout(.5)

  def test_input(self):
    """Tests that inputs can be created."""
    with self.test_session() as sess:
      input_layer = dc.nn.Input(shape=(32,))

  #def test_batch_normalization(self):
  #  """Tests that batch normalization layers can be created."""

  def test_graph_convolution(self):
    """Tests that Graph Convolution transforms shapes correctly."""
    n_atoms = 5