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

Fixing broken syntax from mask removal

parent 2c3623fb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ 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

+6 −4
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ class Node(object):
        tensor_indices = [0 for _ in range(len(inbound_layers))]

    input_tensors = []
    input_shapes = []

    for inbound_layer, node_index, tensor_index in zip(
        inbound_layers, node_indices, tensor_indices):
@@ -157,7 +158,7 @@ class Node(object):

    if len(input_tensors) == 1:
      output_tensors = to_list(outbound_layer.call(
          input_tensors[0])
          input_tensors[0]))
      # TODO: try to auto-infer shape
      # if exception is raised by get_output_shape_for.
      output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes[0]))
@@ -1045,14 +1046,15 @@ class Dropout(Layer):
  a fraction `p` of input units to 0 at each update during training time,
  which helps prevent overfitting.

  # Arguments
  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
      (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)`.
      you can use noise_shape=(batch_size, 1, features).
  seed: A Python integer to use as random seed.

  # References
+33 −29
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ import numpy as np
import tensorflow as tf
from tensorflow.python.training import moving_averages
from collections import defaultdict
# TODO(rbharath): What does this line do?
py_all = all

# TODO(rbharath): REMOVE GLOBAL VARS! BREAKS DEEPCHEM STYLE! 
# This is the default internal TF session used by Keras.
@@ -85,28 +87,20 @@ def in_train_phase(x, alt):
  x._uses_learning_phase = True
  return x

def _cond(condition, then_lambda, else_lambda):
  """Backwards compatible interface to tf.cond prior to public introduction.
  """
  try:
      cond_fn = tf.cond
  except AttributeError:
      from tensorflow.python.ops import control_flow_ops
      cond_fn = control_flow_ops.cond
  return cond_fn(condition, then_lambda, else_lambda)

def switch(condition, then_expression, else_expression):
  """Switches between two operations
  depending on a scalar value (`int` or `bool`).
  Note that both `then_expression` and `else_expression`
  should be symbolic tensors of the *same shape*.

  # Arguments
  Parameters
  ----------
  condition: scalar tensor.
  then_expression: either a tensor, or a callable that returns a tensor.
  else_expression: either a tensor, or a callable that returns a tensor.

  # Returns
  Returns
  -------
  The selected tensor.
  """
  if condition.dtype != tf.bool:
@@ -121,17 +115,16 @@ def switch(condition, then_expression, else_expression):
        return else_expression
  else:
    else_expression_fn = else_expression
  x = _cond(condition,
            then_expression_fn,
            else_expression_fn)
  x = tf.cond(condition, then_expression_fn, else_expression_fn)
  return x

def normalize_batch_in_training(x, gamma, beta,
                                reduction_axes, epsilon=1e-3):
  """Computes mean and std for batch then apply batch_normalization on batch.

  # Returns
      A tuple length of 3, `(normalized_tensor, mean, variance)`.
  Returns
  -------
  A tuple length of 3, (normalized_tensor, mean, variance).
  """
  mean, var = tf.nn.moments(x, reduction_axes,
                            shift=None, name=None, keep_dims=False)
@@ -142,7 +135,7 @@ def normalize_batch_in_training(x, gamma, beta,
  else:
    # need broadcasting
    target_shape = []
    for axis in range(ndim(x)):
    for axis in range(get_ndim(x)):
      if axis in reduction_axes:
        target_shape.append(1)
      else:
@@ -162,20 +155,13 @@ def eval(x):
  """Evaluates the value of a variable.
  Returns a Numpy array.

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

  # Returns
  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())

@@ -372,6 +358,24 @@ def batch_set_value(tuples):
      feed_dict[assign_placeholder] = value
    get_session().run(assign_ops, feed_dict=feed_dict)

def _initialize_variables():
  if hasattr(tf, 'global_variables'):
    variables = tf.global_variables()
  else:
    variables = tf.all_variables()

  uninitialized_variables = []
  for v in variables:
    if not hasattr(v, '_keras_initialized') or not v._keras_initialized:
      uninitialized_variables.append(v)
      v._keras_initialized = True
  if uninitialized_variables:
    sess = get_session()
    if hasattr(tf, 'variables_initializer'):
      sess.run(tf.variables_initializer(uninitialized_variables))
    else:
      sess.run(tf.initialize_variables(uninitialized_variables))

# TODO(rbharath): DANGEROUS! THIS IS LEAKY AND BREAKS DEEPCHEM STYLE!
def get_session():
  """Returns the TF session to be used by the backend.