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

Cleanup

parent 49682077
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -162,8 +162,10 @@ class Dataset(object):
    """Get an object that iterates over the samples in the dataset.

    Example:

    >>> for x, y, w, id in dataset.itersamples():
    >>>   print(x, y, w, id)

    """
    raise NotImplementedError()

@@ -317,8 +319,10 @@ class NumpyDataset(Dataset):
    """Get an object that iterates over the samples in the dataset.

    Example:

    >>> for x, y, w, id in dataset.itersamples():
    >>>   print(x, y, w, id)

    """
    n_samples = self._X.shape[0]
    return ((self._X[i], self._y[i], self._w[i], self._ids[i])
@@ -580,6 +584,7 @@ class DiskDataset(Dataset):
    """Get an object that iterates over the samples in the dataset.

    Example:

    >>> for x, y, w, id in dataset.itersamples():
    >>>   print(x, y, w, id)
    """
+37 −29
Original line number Diff line number Diff line
@@ -492,7 +492,8 @@ class Layer(object):
    an input shape (assumes that the layer will be built
    to match that input shape).

    # Arguments
    Parameters
    ----------
    input_shape: Shape tuple (tuple of integers)
      or list of shape tuples (one per output tensor of the layer).
      Shape tuples can include None for free dimensions,
@@ -504,7 +505,8 @@ class Layer(object):
    """Creates the layer weights.
    Must be implemented on all layers that have weights.

    # Arguments
    Parameters
    ----------
    input_shape: tensor (future input to layer)
      or list/tuple of tensors to reference
      for weight shape computations.
@@ -514,7 +516,8 @@ class Layer(object):
  def _get_node_attribute_at_index(self, node_index, attr, attr_name):
    """Retrieves an attribute (e.g. input_tensors) from a node.

    # Arguments
    Parameters
    ----------
    node_index: Integer index of the node from which
        to retrieve the attribute.
    attr: Exact node attribute name.
@@ -735,7 +738,9 @@ class InputLayer(Layer):
    It can either wrap an existing tensor (pass an `input_tensor` argument)
    or create its a placeholder tensor (pass arguments `input_shape`
    or `batch_input_shape` as well as `input_dtype`).
    # Arguments

    Parameters
    ----------
    input_shape: Shape tuple, not including the batch axis.
    batch_input_shape: Shape tuple, including the batch axis.
    input_dtype: Datatype of the input.
@@ -832,7 +837,9 @@ def Input(shape=None, batch_shape=None,
      ._keras_history: Last layer applied to the tensor.
          the entire layer graph is retrievable from that layer,
          recursively.
  # Arguments

  Parameters
  ----------
      shape: A shape tuple (integer), not including the batch size.
          For instance, `shape=(32,)` indicates that the expected input
          will be batches of 32-dimensional vectors.
@@ -846,13 +853,14 @@ 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`...)
  # Example
      ```python
      # this is a logistic regression in Keras
      a = Input(shape=(32,))
      b = Dense(16, activation='softmax')(a)
      model = Model(input=a, output=b)
      ```

  # TODO(rbharath): Support this type of functional API.
  Example:

  >>> # this is a logistic regression in Keras
  >>> a = Input(shape=(32,))
  >>> b = Dense(16, activation='softmax')(a)
  >>> model = Model(input=a, output=b)
  """
  if not batch_shape and tensor is None:
    assert shape, ('Please provide to Input either a `shape`'
@@ -875,23 +883,23 @@ def Input(shape=None, batch_shape=None,
class Dense(Layer):
  """Just your regular densely-connected NN layer.

  # Example
  TODO(rbharath): Make this functional in deepchem

  ```python
      # as first layer in a sequential model:
      model = Sequential()
      model.add(Dense(32, input_dim=16))
      # now the model will take as input arrays of shape (*, 16)
      # and output arrays of shape (*, 32)
  Example:

      # this is equivalent to the above:
      model = Sequential()
      model.add(Dense(32, input_shape=(16,)))
  >>> # as first layer in a sequential model:
  >>> model = Sequential()
  >>> model.add(Dense(32, input_dim=16))
  >>> # now the model will take as input arrays of shape (*, 16)
  >>> # and output arrays of shape (*, 32)

      # after the first layer, you don't need to specify
      # the size of the input anymore:
      model.add(Dense(32))
  ```
  >>> # this is equivalent to the above:
  >>> model = Sequential()
  >>> model.add(Dense(32, input_shape=(16,)))

  >>> # after the first layer, you don't need to specify
  >>> # the size of the input anymore:
  >>> model.add(Dense(32))

  Parameters
  ----------
@@ -1056,15 +1064,15 @@ class BatchNormalization(Layer):
  beta_regularizer: instance of [WeightRegularizer](../regularizers.md),
    applied to the beta vector.

  # Input shape
  Input shape:
  Arbitrary. Use the keyword argument `input_shape`
  (tuple of integers, does not include the samples axis)
  when using this layer as the first layer in a model.

  # Output shape
  Output shape:
  Same shape as input.

  # References
  References:
    - [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
  """

+74 −73
Original line number Diff line number Diff line
"""Ops for graph construction.

Large amounts of code borrowed from Keras.
Large amounts of code borrowed from Keras. Will try to incorporate into
DeepChem properly.
"""
from __future__ import print_function
from __future__ import division
@@ -23,27 +24,6 @@ _UID_PREFIXES = defaultdict(int)
# either train mode (learning_phase == 1) or test mode (learning_phase == 0).
_GRAPH_LEARNING_PHASES = {}

# TODO(rbharath): Can this be improved.
def _convert_string_dtype(dtype):
  if dtype == 'float16':
    return tf.float16
  if dtype == 'float32':
    return tf.float32
  elif dtype == 'float64':
    return tf.float64
  elif dtype == 'int16':
    return tf.int16
  elif dtype == 'int32':
    return tf.int32
  elif dtype == 'int64':
    return tf.int64
  elif dtype == 'uint8':
    return tf.int8
  elif dtype == 'uint16':
    return tf.uint16
  else:
    raise ValueError('Unsupported dtype:', dtype)

def _to_tensor(x, dtype):
  x = tf.convert_to_tensor(x)
  if x.dtype != dtype:
@@ -59,8 +39,7 @@ def learning_phase():
  """
  graph = tf.get_default_graph()
  if graph not in _GRAPH_LEARNING_PHASES:
    phase = tf.placeholder(dtype='bool',
                           name='keras_learning_phase')
    phase = tf.placeholder(dtype='bool', name='keras_learning_phase')
    _GRAPH_LEARNING_PHASES[graph] = phase
  return _GRAPH_LEARNING_PHASES[graph]

@@ -68,7 +47,8 @@ def in_train_phase(x, alt):
  """Selects `x` in train phase, and `alt` otherwise.
  Note that `alt` should have the *same shape* as `x`.

  # Returns
  Returns
  -------
  Either `x` or `alt` based on `K.learning_phase`.
  """
  if learning_phase() is 1:
@@ -150,7 +130,7 @@ def ones(shape, dtype=None, name=None):
  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable.
  dtype: String, data type of returned Keras variable.
  dtype: Tensorflow dtype 
  name: String, name of returned Keras variable.

  Returns
@@ -160,8 +140,7 @@ def ones(shape, dtype=None, name=None):
  if dtype is None:
    dtype = tf.float32 
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  return variable(tf.constant_initializer(1., dtype=tf_dtype)(shape),
  return variable(tf.constant_initializer(1., dtype=dtype)(shape),
                  dtype, name)

def cast_to_floatx(x):
@@ -309,10 +288,12 @@ def dot(x, y):
def get_ndim(x):
  """Returns the number of axes in a tensor, as an integer.

  # Arguments
  Parameters
  ----------
  x: Tensor or variable.

  # Returns
  Returns
  -------
  Integer (scalar), number of axes.
  """
  dims = x.get_shape()._dims
@@ -323,10 +304,12 @@ def get_ndim(x):
def get_dtype(x):
  """Returns the dtype of a Keras tensor or variable, as a string.

  # Arguments
  Parameters
  ----------
  x: Tensor or variable.

  # Returns
  Returns
  -------
  String, dtype of `x`.
  """
  return x.dtype.name
@@ -348,7 +331,8 @@ def epsilon():
  """Returns the value of the fuzz
  factor used in numeric expressions.

  # Returns
  Returns
  -------
  A float.
  """
  return 1e-7 
@@ -356,15 +340,17 @@ def epsilon():
def variable(value, dtype=tf.float32, name=None):
  """Instantiates a variable and returns it.

  # Arguments
  Parameters
  ----------
  value: Numpy array, initial value of the tensor.
  dtype: Tensor type.
  name: Optional name string for the tensor.

  # Returns
  Returns
  -------
  A variable instance (with Keras metadata included).
  """
  v = tf.Variable(value, dtype=_convert_string_dtype(dtype), name=name)
  v = tf.Variable(value, dtype=dtype, name=name)
  if hasattr(value, 'get_shape'):
    v._keras_shape = tuple(map(int, value.get_shape()))
  v._uses_learning_phase = False
@@ -375,24 +361,25 @@ def random_uniform_variable(shape, low, high, dtype=tf.float32,
  """Instantiates an Keras variable filled with
  samples drawn from a uniform distribution and returns it.

  # Arguments
  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable.
  low: Float, lower boundary of the output inteval.
  high: Float, upper boundary of the output interval.
    dtype: String, dtype of returned Keras variable.
  dtype: Tensorflow dtype
  name: String, name of returned Keras variable.
  seed: Integer, random seed.

  # Returns
      A Keras variable, filled with drawn samples.
  Returns
  -------
  A tf.Variable, filled with drawn samples.
  """
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  if seed is None:
      # ensure that randomness is conditioned by the Numpy RNG
      seed = np.random.randint(10e8)
  value = tf.random_uniform_initializer(
      low, high, dtype=tf_dtype, seed=seed)(shape)
      low, high, dtype=dtype, seed=seed)(shape)
  return variable(value, dtype=dtype, name=name)

def random_normal_variable(shape, mean, scale, dtype=tf.float32,
@@ -400,30 +387,32 @@ def random_normal_variable(shape, mean, scale, dtype=tf.float32,
  """Instantiates an Keras variable filled with
  samples drawn from a normal distribution and returns it.

  # Arguments
  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable.
  mean: Float, mean of the normal distribution.
  scale: Float, standard deviation of the normal distribution.
      dtype: String, dtype of returned Keras variable.
  dtype: Tensorflow dtype
  name: String, name of returned Keras variable.
  seed: Integer, random seed.

  # Returns
      A Keras variable, filled with drawn samples.
  Returns
  -------
  A tf.Variable, filled with drawn samples.
  """
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  if seed is None:
    # ensure that randomness is conditioned by the Numpy RNG
    seed = np.random.randint(10e8)
  value = tf.random_normal_initializer(
      mean, scale, dtype=tf_dtype, seed=seed)(shape)
      mean, scale, dtype=dtype, seed=seed)(shape)
  return variable(value, dtype=dtype, name=name)

def max(x, axis=None, keepdims=False):
  """Maximum value in a tensor.

  # Arguments
  Parameters
  ----------
  x: A tensor or variable.
  axis: An integer, the axis to find maximum values.
  keepdims: A boolean, whether to keep the dimensions or not.
@@ -431,7 +420,8 @@ def max(x, axis=None, keepdims=False):
      by 1. If `keepdims` is `True`,
      the reduced dimension is retained with length 1.

  # Returns
  Returns
  -------
  A tensor with maximum values of `x`.
  """
  axis = _normalize_axis(axis, get_ndim(x))
@@ -440,16 +430,18 @@ def max(x, axis=None, keepdims=False):
def sum(x, axis=None, keepdims=False):
  """Sum of the values in a tensor, alongside the specified axis.

  # Arguments
  Parameters
  ----------
  x: A tensor or variable.
  axis: An integer, the axis to sum over.
  keepdims: A boolean, whether to keep the dimensions or not.
      If `keepdims` is `False`, the rank of the tensor is reduced
      by 1. If `keepdims` is `True`,
    If keepdims is False, the rank of the tensor is reduced
    by 1. If keepdims is True,
    the reduced dimension is retained with length 1.

  # Returns
    A tensor with sum of `x`.
  Returns
  -------
  A tensor with sum of x.
  """
  axis = _normalize_axis(axis, get_ndim(x))
  return tf.reduce_sum(x, reduction_indices=axis, keep_dims=keepdims)
@@ -462,7 +454,7 @@ def zeros(shape, dtype=tf.float32, name=None):
  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable
  dtype: String, data type of returned Keras variable
  dtype: Tensorflow dtype 
  name: String, name of returned Keras variable

  Returns
@@ -470,8 +462,7 @@ def zeros(shape, dtype=tf.float32, name=None):
  A variable (including Keras metadata), filled with `0.0`.
  """
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
  return variable(tf.constant_initializer(0., dtype=tf_dtype)(shape),
  return variable(tf.constant_initializer(0., dtype=dtype)(shape),
                  dtype, name)

def cosine_distances(test, support):
@@ -504,11 +495,13 @@ def cosine_distances(test, support):
def elu(x, alpha=1.):
  """Exponential linear unit.

  # Arguments
      x: A tenor or variable to compute the activation function for.
  Parameters
  ----------
  x: A tensor or variable to compute the activation function for.
  alpha: A scalar, slope of positive section.

  # Returns
  Returns
  -------
  A tensor.
  """
  res = tf.nn.elu(x)
@@ -521,12 +514,14 @@ def relu(x, alpha=0., max_value=None):
  """Rectified linear unit.
  With default values, it returns element-wise `max(x, 0)`.

  # Arguments
  Parameters
  ----------
  x: A tensor or variable.
  alpha: A scalar, slope of negative section (default=`0.`).
  max_value: Saturation threshold.

  # Returns
  Returns
  -------
  A tensor.
  """
  if alpha != 0.:
@@ -544,13 +539,15 @@ def relu(x, alpha=0., max_value=None):
def hard_sigmoid(x):
  """Segment-wise linear approximation of sigmoid.
  Faster than sigmoid.
  Returns `0.` if `x < -2.5`, `1.` if `x > 2.5`.
  In `-2.5 <= x <= 2.5`, returns `0.2 * x + 0.5`.
  Returns 0. if x < -2.5, 1. if x > 2.5.
  In -2.5 <= x <= 2.5, returns 0.2 * x + 0.5.

  # Arguments
  Parameters
  ----------
  x: A tensor or variable.

  # Returns
  Returns
  -------
  A tensor.
  """
  x = (0.2 * x) + 0.5
@@ -562,10 +559,12 @@ def hard_sigmoid(x):
def sqrt(x):
  """Element-wise square root.

  # Arguments
  Parameters
  ----------
  x: input tensor.

  # Returns
  Returns
  -------
  A tensor.
  """
  zero = _to_tensor(0., x.dtype.base_dtype)
@@ -576,15 +575,17 @@ def sqrt(x):
def var(x, axis=None, keepdims=False):
  """Variance of a tensor, alongside the specified axis.

  # Arguments
  Parameters
  ----------
  x: A tensor or variable.
  axis: An integer, the axis to compute the variance.
  keepdims: A boolean, whether to keep the dimensions or not.
          If `keepdims` is `False`, the rank of the tensor is reduced
          by 1. If `keepdims` is `True`,
      If keepdims is False, the rank of the tensor is reduced
      by 1. If keepdims is True,
      the reduced dimension is retained with length 1.

  # Returns
  Returns
  -------
  A tensor with the variance of elements of `x`.
  """
  axis = _normalize_axis(axis, get_ndim(x))