Commit 1e9100ab authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

More cleanup and bugfixes

parent cf2e94ed
Loading
Loading
Loading
Loading
+14 −22
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@ from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

import six
import tensorflow as tf
from deepchem.nn import model_ops
from deepchem.nn.model_ops import get_ndim
@@ -16,12 +15,13 @@ def get_from_module(identifier, module_params, module_name,
                    instantiate=False, kwargs=None):
  """Retrieves a class of function member of a module.

  # Arguments
  Parameters
  ----------
  identifier: the object to retrieve. It could be specified
    by name (as a string), or by dict. In any other case,
          `identifier` itself will be returned without any changes.
    identifier itself will be returned without any changes.
  module_params: the members of a module
          (e.g. the output of `globals()`).
    (e.g. the output of globals()).
  module_name: string; the name of the target module. Only used
    to format error messages.
  instantiate: whether to instantiate the returned object
@@ -29,13 +29,19 @@ def get_from_module(identifier, module_params, module_name,
  kwargs: a dictionary of keyword arguments to pass to the
    class constructor if `instantiate` is `True`.

  # Returns
  Returns
  -------
  The target object.

  # Raises
  Raises
  ------
  ValueError: if the identifier cannot be found.
 """
  if isinstance(identifier, six.string_types):
  try:
    basestring
  except NameError:
    basestring = str
  if isinstance(identifier, basestring):
    res = module_params.get(identifier)
    if not res:
        raise ValueError('Invalid ' + str(module_name) + ': ' +
@@ -46,14 +52,7 @@ def get_from_module(identifier, module_params, module_name,
      return res(**kwargs)
    else:
      return res
  elif isinstance(identifier, dict):
    name = identifier.pop('name')
    res = module_params.get(name)
    if res:
      return res(**identifier)
    else:
      raise ValueError('Invalid ' + str(module_name) + ': ' +
                       str(identifier))

  return identifier

def softmax(x):
@@ -75,31 +74,24 @@ def elu(x, alpha=1.0):
def softplus(x):
  return tf.nn.softplus(x)


def softsign(x):
  return tf.nn.softsign(x)


def relu(x, alpha=0., max_value=None):
  return model_ops.relu(x, alpha=alpha, max_value=max_value)


def tanh(x):
  return tf.nn.tanh(x)


def sigmoid(x):
  return tf.nn.sigmoid(x)


def hard_sigmoid(x):
  return model_ops.hard_sigmoid(x)


def linear(x):
  return x


def get(identifier):
  if identifier is None:
    return linear
+6 −15
Original line number Diff line number Diff line
@@ -19,19 +19,14 @@ class MaxNorm(Constraint):
  Constrains the weights incident to each hidden unit
  to have a norm less than or equal to a desired value.

  # Arguments
  Parameters
  ----------
  m: the maximum norm for the incoming weights.
  axis: integer, axis along which to calculate weight norms.
    For instance, in a `Dense` layer the weight matrix
        has shape `(input_dim, output_dim)`,
        set `axis` to `0` to constrain each weight vector
    has shape (input_dim, output_dim),
    set axis to 0 to constrain each weight vector
    of length `(input_dim,)`.
        In a `Convolution2D` layer with `dim_ordering="tf"`,
        the weight tensor has shape
        `(rows, cols, input_depth, output_depth)`,
        set `axis` to `[0, 1, 2]`
        to constrain the weights of each filter tensor of size
        `(rows, cols, input_depth)`.

  # References
    - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
@@ -48,11 +43,9 @@ class MaxNorm(Constraint):
    p *= (desired / (model_ops.epsilon() + norms))
    return p


class NonNeg(Constraint):
  """Constrains the weights to be non-negative.
  """

  def __call__(self, p):
    p *= tf.cast(p >= 0., tf.float32)
    return p
@@ -79,12 +72,10 @@ class UnitNorm(Constraint):
    self.axis = axis

  def __call__(self, p):
    return p / (1e-7 + model_ops.sqrt(model_ops.sum(tf.square(p),
                                      axis=self.axis,
                                      keepdims=True)))
    return p / (model_ops.epsilon() + model_ops.sqrt(
        model_ops.sum(tf.square(p), axis=self.axis, keepdims=True)))

# Aliases.

maxnorm = MaxNorm
nonneg = NonNeg
unitnorm = UnitNorm
+2 −14
Original line number Diff line number Diff line
@@ -996,36 +996,24 @@ class Dropout(Layer):
  Parameters
  ----------
  p: float between 0 and 1. Fraction of the input units to drop.
  noise_shape: 1D integer tensor representing the shape of the
      binary dropout mask that will be multiplied with the input.
      For instance, if your inputs have shape
      (batch_size, timesteps, features) and
      you want the dropout mask to be the same for all timesteps,
      you can use noise_shape=(batch_size, 1, features).
  seed: A Python integer to use as random seed.

  # References
      - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
  """

  def __init__(self, p, noise_shape=None, seed=None, **kwargs):
  def __init__(self, p, seed=None, **kwargs):
    self.p = p
    self.noise_shape = noise_shape
    self.seed = seed
    if 0. < self.p < 1.:
        self.uses_learning_phase = True
    super(Dropout, self).__init__(**kwargs)

  def _get_noise_shape(self, _):
    return self.noise_shape

  def call(self, x):
    if 0. < self.p < 1.:
      noise_shape = self._get_noise_shape(x)

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

+7 −15
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ def eval(x):
  -------
  A Numpy array.
  """
  return to_dense(x).eval(session=get_session())
  return x.eval(session=get_session())

def ones(shape, dtype=None, name=None):
  """Instantiates an all-ones tensor variable and returns it.
@@ -321,9 +321,9 @@ def concatenate(tensors, axis=-1):
      axis = 0

  try:
    return tf.concat_v2([to_dense(x) for x in tensors], axis)
    return tf.concat_v2([x for x in tensors], axis)
  except AttributeError:
    return tf.concat(axis, [to_dense(x) for x in tensors])
    return tf.concat(axis, [x for x in tensors])

def _normalize_axis(axis, ndim):
  if isinstance(axis, tuple):
@@ -551,23 +551,15 @@ def sum(x, axis=None, keepdims=False):
def zeros(shape, dtype=tf.float32, name=None):
  """Instantiates an all-zeros variable and returns it.

    # Arguments
  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable
  dtype: String, data type of returned Keras variable
  name: String, name of returned Keras variable

    # Returns
  Returns
  -------
  A variable (including Keras metadata), filled with `0.0`.

    # Example
    ```python
        >>> from keras import backend as K
        >>> kvar = K.zeros((3,4))
        >>> K.eval(kvar)
        array([[ 0.,  0.,  0.,  0.],
               [ 0.,  0.,  0.,  0.],
               [ 0.,  0.,  0.,  0.]], dtype=float32)
    ```
  """
  shape = tuple(map(int, shape))
  tf_dtype = _convert_string_dtype(dtype)
+0 −8
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ class Regularizer(object):
  def __call__(self, x):
    return 0


class EigenvalueRegularizer(Regularizer):
  """Regularizer based on the eignvalues of a weight matrix.

@@ -78,31 +77,24 @@ class L1L2Regularizer(Regularizer):
WeightRegularizer = L1L2Regularizer
ActivityRegularizer = L1L2Regularizer


def l1(l=0.01):
  return L1L2Regularizer(l1=l)


def l2(l=0.01):
  return L1L2Regularizer(l2=l)


def l1l2(l1=0.01, l2=0.01):
  return L1L2Regularizer(l1=l1, l2=l2)


def activity_l1(l=0.01):
  return L1L2Regularizer(l1=l)


def activity_l2(l=0.01):
  return L1L2Regularizer(l2=l)


def activity_l1l2(l1=0.01, l2=0.01):
  return L1L2Regularizer(l1=l1, l2=l2)


def get(identifier, kwargs=None):
  return get_from_module(identifier, globals(), 'regularizer',
                         instantiate=True, kwargs=kwargs)