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

Bugfix

parent 5f2896d8
Loading
Loading
Loading
Loading
+128 −1
Original line number Diff line number Diff line
"""Ops for graph construction."""
"""Ops for graph construction.

Large amounts of code borrowed from Keras.
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
@@ -13,6 +16,7 @@ from collections import defaultdict
# This is the default internal TF session used by Keras.
# It can be set manually via `set_session(sess)`.
_SESSION = None
_FLOATX = 'float32'
_EPSILON = 10e-8
# This boolean flag can be set to True to leave variable initialization
# up to the user.
@@ -107,6 +111,107 @@ def normalize_batch_in_training(x, gamma, beta,
                                       epsilon)
  return normed, mean, var

def eval(x):
  """Evaluates the value of a variable.
  Returns a Numpy array.

  # Arguments
      x: A variable.

  # Returns
      A Numpy array.

  # Examples
  ```python
      >>> from keras import backend as K
      >>> kvar = K.variable(np.array([[1, 2], [3, 4]]), dtype='float32')
      >>> K.eval(kvar)
      array([[ 1.,  2.],
             [ 3.,  4.]], dtype=float32)
  ```
  """
  return to_dense(x).eval(session=get_session())

def ones(shape, dtype=None, name=None):
  """Instantiates an all-ones tensor variable and returns it.

  # Arguments
      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
      A Keras variable, filled with `1.0`.

  # Example
  ```python
      >>> from keras import backend as K
      >>> kvar = K.ones((3,4))
      >>> K.eval(kvar)
      array([[ 1.,  1.,  1.,  1.],
             [ 1.,  1.,  1.,  1.],
             [ 1.,  1.,  1.,  1.]], dtype=float32)
  ```
  """
  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),
                  dtype, name)

def cast_to_floatx(x):
  """Cast a Numpy array to the default Keras float type.

  # Arguments
      x: Numpy array.

  # Returns
      The same Numpy array, cast to its new type.

  # Example
  ```python
      >>> from keras import backend as K
      >>> K.floatx()
      'float32'
      >>> arr = numpy.array([1.0, 2.0], dtype='float64')
      >>> arr.dtype
      dtype('float64')
      >>> new_arr = K.cast_to_floatx(arr)
      >>> new_arr
      array([ 1.,  2.], dtype=float32)
      >>> new_arr.dtype
      dtype('float32')
  ```
  """
  return np.asarray(x, dtype=_FLOATX)

def to_dense(tensor):
  """Converts a sparse tensor into a dense tensor
  and returns it.

  # Arguments
      tensor: A tensor instance (potentially sparse).

  # Returns
      A dense tensor.

  # Examples
  ```python
      >>> from keras import backend as K
      >>> b = K.placeholder((2, 2), sparse=True)
      >>> print(K.is_sparse(b))
      True
      >>> c = K.to_dense(b)
      >>> print(K.is_sparse(c))
      False
  ```
  """
  if is_sparse(tensor):
    return tf.sparse_tensor_to_dense(tensor)
  else:
    return tensor

def moving_average_update(variable, value, momentum):
  try:
    return moving_averages.assign_moving_average(
@@ -115,6 +220,28 @@ def moving_average_update(variable, value, momentum):
    return moving_averages.assign_moving_average(
        variable, value, momentum)

def is_sparse(tensor):
  """Returns whether a tensor is a sparse tensor.

  # Arguments
      tensor: A tensor instance.

  # Returns
      A boolean.

  # Example
  ```python
      >>> from keras import backend as K
      >>> a = K.placeholder((2, 2), sparse=False)
      >>> print(K.is_sparse(a))
      False
      >>> b = K.placeholder((2, 2), sparse=True)
      >>> print(K.is_sparse(b))
      True
  ```
  """
  return isinstance(tensor, tf.SparseTensor)

def int_shape(x):
  """Returns the shape of a Keras tensor or a Keras variable as a tuple of
  integers or None entries.
+24 −35
Original line number Diff line number Diff line
"""Ops for regularizers

Code borrowed from Keras.
"""
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
from __future__ import absolute_import
from keras import backend as K
from .activations import get_from_module

import warnings
from deepchem.nn import model_ops
from deepchem.nn.activations import get_from_module


class Regularizer(object):
@@ -9,19 +17,6 @@ class Regularizer(object):
  def __call__(self, x):
    return 0

    def get_config(self):
        return {'name': self.__class__.__name__}

    def set_param(self, _):
        warnings.warn('The `set_param` method on regularizers is deprecated. '
                      'It no longer does anything, '
                      'and it will be removed after 06/2017.')

    def set_layer(self, _):
        warnings.warn('The `set_layer` method on regularizers is deprecated. '
                      'It no longer does anything, '
                      'and it will be removed after 06/2017.')


class EigenvalueRegularizer(Regularizer):
  """Regularizer based on the eignvalues of a weight matrix.
@@ -36,26 +31,26 @@ class EigenvalueRegularizer(Regularizer):
    self.k = k

  def __call__(self, x):
        if K.ndim(x) != 2:
    if model_ops.get_ndim(x) != 2:
      raise ValueError('EigenvalueRegularizer '
                       'is only available for tensors of rank 2.')
        covariance = K.dot(K.transpose(x), x)
        dim1, dim2 = K.eval(K.shape(covariance))
    covariance = model_ops.dot(tf.transpose(x), x)
    dim1, dim2 = model_ops.eval(tf.shape(covariance))

    # Power method for approximating the dominant eigenvector:
    power = 9  # Number of iterations of the power method.
        o = K.ones([dim1, 1])  # Initial values for the dominant eigenvector.
        main_eigenvect = K.dot(covariance, o)
    o = model_ops.ones([dim1, 1])  # Initial values for the dominant eigenvector.
    main_eigenvect = model_ops.dot(covariance, o)
    for n in range(power - 1):
            main_eigenvect = K.dot(covariance, main_eigenvect)
        covariance_d = K.dot(covariance, main_eigenvect)
      main_eigenvect = model_ops.dot(covariance, main_eigenvect)
    covariance_d = model_ops.dot(covariance, main_eigenvect)

    # The corresponding dominant eigenvalue:
        main_eigenval = (K.dot(K.transpose(covariance_d), main_eigenvect) /
                         K.dot(K.transpose(main_eigenvect), main_eigenvect))
    main_eigenval = (model_ops.dot(tf.transpose(covariance_d), main_eigenvect) /
                     model_ops.dot(tf.transpose(main_eigenvect), main_eigenvect))
    # Multiply by the given regularization gain.
    regularization = (main_eigenval ** 0.5) * self.k
        return K.sum(regularization)
    return model_ops.sum(regularization)


class L1L2Regularizer(Regularizer):
@@ -67,25 +62,19 @@ class L1L2Regularizer(Regularizer):
  """

  def __init__(self, l1=0., l2=0.):
        self.l1 = K.cast_to_floatx(l1)
        self.l2 = K.cast_to_floatx(l2)
    self.l1 = model_ops.cast_to_floatx(l1)
    self.l2 = model_ops.cast_to_floatx(l2)

  def __call__(self, x):
    regularization = 0
    if self.l1:
            regularization += K.sum(self.l1 * K.abs(x))
        regularization += model_ops.sum(self.l1 * tf.abs(x))
    if self.l2:
            regularization += K.sum(self.l2 * K.square(x))
        regularization += model_ops.sum(self.l2 * tf.square(x))
    return regularization

    def get_config(self):
        return {'name': self.__class__.__name__,
                'l1': float(self.l1),
                'l2': float(self.l2)}


# Aliases.

WeightRegularizer = L1L2Regularizer
ActivityRegularizer = L1L2Regularizer