Unverified Commit 6255b3b8 authored by Abhigyan's avatar Abhigyan Committed by GitHub
Browse files

Merge pull request #5 from deepchem/master

update fork
parents 0534ef2e a1410118
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -17,15 +17,17 @@ from deepchem.models.tensorgraph.IRV import TensorflowMultiTaskIRVClassifier
from deepchem.models.tensorgraph.robust_multitask import RobustMultitaskClassifier
from deepchem.models.tensorgraph.robust_multitask import RobustMultitaskRegressor
from deepchem.models.tensorgraph.progressive_multitask import ProgressiveMultitaskRegressor, ProgressiveMultitaskClassifier
from deepchem.models.tensorgraph.models.graph_models import WeaveModel, DTNNTensorGraph, DAGTensorGraph, GraphConvModel, MPNNTensorGraph
from deepchem.models.tensorgraph.models.graph_models import WeaveModel, DTNNModel, DAGModel, GraphConvModel, MPNNModel
from deepchem.models.tensorgraph.models.symmetry_function_regression import BPSymmetryFunctionRegression, ANIRegression
from deepchem.models.tensorgraph.models.scscore import ScScoreModel

from deepchem.models.tensorgraph.models.seqtoseq import SeqToSeq
from deepchem.models.tensorgraph.models.gan import GAN, WGAN
from deepchem.models.tensorgraph.models.text_cnn import TextCNNTensorGraph
from deepchem.models.tensorgraph.models.text_cnn import TextCNNModel
from deepchem.models.tensorgraph.sequential import Sequential
from deepchem.models.tensorgraph.models.sequence_dnn import SequenceDNN

#################### Compatibility imports for renamed TensorGraph models. Remove below with DeepChem 3.0. ####################

from deepchem.models.tensorgraph.models.graph_models import WeaveTensorGraph, GraphConvTensorGraph
 No newline at end of file
from deepchem.models.tensorgraph.models.text_cnn import TextCNNTensorGraph
from deepchem.models.tensorgraph.models.graph_models import WeaveTensorGraph, DTNNTensorGraph, DAGTensorGraph, GraphConvTensorGraph, MPNNTensorGraph
+5 −6
Original line number Diff line number Diff line
@@ -4,8 +4,7 @@ from __future__ import unicode_literals

import numpy as np
import tensorflow as tf
from deepchem.models.tensorgraph.model_ops import random_uniform_variable
from deepchem.models.tensorgraph.model_ops import random_normal_variable
from deepchem.models.tensorgraph.model_ops import random_uniform_variable, random_normal_variable, create_variable
from deepchem.models.tensorgraph.activations import get_from_module


@@ -94,7 +93,7 @@ def orthogonal(shape, scale=1.1, name=None):
  # Pick the one with the correct shape.
  q = u if u.shape == flat_shape else v
  q = q.reshape(shape)
  return tf.Variable(
  return create_variable(
      scale * q[:shape[0], :shape[1]], dtype=tf.float32, name=name)


@@ -103,16 +102,16 @@ def identity(shape, scale=1, name=None):
    raise ValueError('Identity matrix initialization can only be used '
                     'for 2D square matrices.')
  else:
    return tf.Variable(
    return create_variable(
        scale * np.identity(shape[0]), dtype=tf.float32, name=name)


def zero(shape, name=None):
  return tf.Variable(tf.zeros(shape), dtype=tf.float32, name=name)
  return create_variable(tf.zeros(shape), dtype=tf.float32, name=name)


def one(shape, name=None):
  return tf.Variable(tf.ones(shape), dtype=tf.float32, name=name)
  return create_variable(tf.ones(shape), dtype=tf.float32, name=name)


def get(identifier, **kwargs):
+504 −191

File changed.

Preview size limit exceeded, changes collapsed.

+31 −22
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import sys
import traceback
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
from tensorflow.python.training import moving_averages
from collections import defaultdict

@@ -22,28 +23,36 @@ def _to_tensor(x, dtype):
  return x


def create_variable(value, dtype=None, name=None):
  """Create a tf.Variable or tfe.Variable, depending on the current mode."""
  if tfe.in_eager_mode():
    return tfe.Variable(value, dtype=dtype, name=name)
  else:
    return tf.Variable(value, dtype=dtype, name=name)


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

  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable.
  shape: Tuple of integers, shape of returned Tensorflow variable.
  dtype: Tensorflow dtype
  name: String, name of returned Keras variable.
  name: String, name of returned Tensorflow variable.

  Returns
  -------
  A Keras variable, filled with `1.0`.
  A Tensorflow variable, filled with `1.0`.
  """
  if dtype is None:
    dtype = tf.float32
  shape = tuple(map(int, shape))
  return tf.Variable(
  return create_variable(
      tf.constant_initializer(1., dtype=dtype)(shape), dtype, name)


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

  Parameters
  ----------
@@ -65,7 +74,7 @@ def moving_average_update(variable, value, momentum):


def int_shape(x):
  """Returns the shape of a Keras tensor or a Keras variable as a tuple of
  """Returns the shape of a Tensorflow tensor or a Tensorflow variable as a tuple of
  integers or None entries.

  Arguments
@@ -193,7 +202,7 @@ def get_ndim(x):


def get_dtype(x):
  """Returns the dtype of a Keras tensor or variable, as a string.
  """Returns the dtype of a Tensorflow tensor or variable, as a string.

  Parameters
  ----------
@@ -259,7 +268,7 @@ def random_uniform_variable(shape,
    seed = np.random.randint(10e8)
  value = tf.random_uniform_initializer(
      low, high, dtype=dtype, seed=seed)(shape)
  return tf.Variable(value, dtype=dtype, name=name)
  return create_variable(value, dtype=dtype, name=name)


def random_normal_variable(shape,
@@ -268,16 +277,16 @@ def random_normal_variable(shape,
                           dtype=tf.float32,
                           name=None,
                           seed=None):
  """Instantiates an Keras variable filled with
  """Instantiates an Tensorflow variable filled with
  samples drawn from a normal distribution and returns it.

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

  Returns
@@ -290,7 +299,7 @@ def random_normal_variable(shape,
    seed = np.random.randint(10e8)
  value = tf.random_normal_initializer(
      mean, scale, dtype=dtype, seed=seed)(shape)
  return tf.Variable(value, dtype=dtype, name=name)
  return create_variable(value, dtype=dtype, name=name)


def max(x, axis=None, keepdims=False):
@@ -338,7 +347,7 @@ def categorical_crossentropy(output, target, from_logits=False):
  # TODO(rbharath): Should probably swap this over to tf mode.
  """
  # Note: tf.nn.softmax_cross_entropy_with_logits
  # expects logits, Keras expects probabilities.
  # expects logits, Tensorflow expects probabilities.
  if not from_logits:
    # scale preds so that the class probas of each sample sum to 1
    output /= tf.reduce_sum(
@@ -362,7 +371,7 @@ def sparse_categorical_crossentropy(output, target, from_logits=False):
  and a target tensor, where the target is an integer tensor.
  """
  # Note: tf.nn.softmax_cross_entropy_with_logits
  # expects logits, Keras expects probabilities.
  # expects logits, Tensorflow expects probabilities.
  if not from_logits:
    epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
    output = tf.clip_by_value(output, epsilon, 1 - epsilon)
@@ -398,7 +407,7 @@ def binary_crossentropy(output, target, from_logits=False):
      A tensor.
  """
  # Note: tf.nn.softmax_cross_entropy_with_logits
  # expects logits, Keras expects probabilities.
  # expects logits, Tensorflow expects probabilities.
  if not from_logits:
    # transform back to logits
    epsilon = _to_tensor(_EPSILON, output.dtype.base_dtype)
@@ -437,16 +446,16 @@ def zeros(shape, dtype=tf.float32, name=None):

  Parameters
  ----------
  shape: Tuple of integers, shape of returned Keras variable
  shape: Tuple of integers, shape of returned Tensorflow variable
  dtype: Tensorflow dtype
  name: String, name of returned Keras variable
  name: String, name of returned Tensorflow variable

  Returns
  -------
  A variable (including Keras metadata), filled with `0.0`.
  A variable (including Tensorflow metadata), filled with `0.0`.
  """
  shape = tuple(map(int, shape))
  return tf.Variable(
  return create_variable(
      tf.constant_initializer(0., dtype=dtype)(shape), dtype, name)


@@ -674,7 +683,7 @@ def add_bias(tensor, init=None, name=None):
  if init is None:
    init = tf.zeros([tensor.get_shape()[-1].value])
  with tf.name_scope(name, tensor.op.name, [tensor]):
    b = tf.Variable(init, name='b')
    b = create_variable(init, name='b')
    return tf.nn.bias_add(tensor, b)


@@ -752,8 +761,8 @@ def fully_connected_layer(tensor,
    bias_init = tf.zeros([size])

  with tf.name_scope(name, 'fully_connected', [tensor]):
    w = tf.Variable(weight_init, name='w', dtype=tf.float32)
    b = tf.Variable(bias_init, name='b', dtype=tf.float32)
    w = create_variable(weight_init, name='w', dtype=tf.float32)
    b = create_variable(bias_init, name='b', dtype=tf.float32)
    return tf.nn.xw_plus_b(tensor, w, b)


+57 −13
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ from deepchem.models.tensorgraph.graph_layers import WeaveGather, \
from deepchem.models.tensorgraph.graph_layers import WeaveLayerFactory
from deepchem.models.tensorgraph.layers import Dense, SoftMax, \
    SoftMaxCrossEntropy, GraphConv, BatchNorm, \
    GraphPool, GraphGather, WeightedError, Dropout, BatchNormalization, Stack, Flatten, GraphCNN, GraphCNNPool
    GraphPool, GraphGather, WeightedError, Dropout, BatchNorm, Stack, Flatten, GraphCNN, GraphCNNPool
from deepchem.models.tensorgraph.layers import L2Loss, Label, Weights, Feature
from deepchem.models.tensorgraph.tensor_graph import TensorGraph
from deepchem.trans import undo_transforms
@@ -86,7 +86,7 @@ class WeaveModel(TensorGraph):
        out_channels=self.n_graph_feat,
        activation_fn=tf.nn.tanh,
        in_layers=weave_layer2A)
    batch_norm1 = BatchNormalization(epsilon=1e-5, mode=1, in_layers=[dense1])
    batch_norm1 = BatchNorm(epsilon=1e-5, in_layers=[dense1])
    weave_gather = WeaveGather(
        self.batch_size,
        n_input=self.n_graph_feat,
@@ -194,7 +194,7 @@ class WeaveModel(TensorGraph):
    return out


class DTNNTensorGraph(TensorGraph):
class DTNNModel(TensorGraph):

  def __init__(self,
               n_tasks,
@@ -237,7 +237,7 @@ class DTNNTensorGraph(TensorGraph):
    self.steps = np.expand_dims(self.steps, 0)
    self.output_activation = output_activation
    self.mode = mode
    super(DTNNTensorGraph, self).__init__(**kwargs)
    super(DTNNModel, self).__init__(**kwargs)
    assert self.mode == "regression"
    self.build_graph()

@@ -351,14 +351,14 @@ class DTNNTensorGraph(TensorGraph):
    if transformers != [] and not isinstance(outputs, collections.Sequence):
      raise ValueError(
          "DTNN does not support single tensor output with transformers")
    retval = super(DTNNTensorGraph, self).predict(dataset, outputs=outputs)
    retval = super(DTNNModel, self).predict(dataset, outputs=outputs)
    if not isinstance(outputs, collections.Sequence):
      return retval
    retval = np.concatenate(retval, axis=-1)
    return undo_transforms(retval, transformers)


class DAGTensorGraph(TensorGraph):
class DAGModel(TensorGraph):

  def __init__(self,
               n_tasks,
@@ -390,7 +390,7 @@ class DAGTensorGraph(TensorGraph):
    self.n_graph_feat = n_graph_feat
    self.n_outputs = n_outputs
    self.mode = mode
    super(DAGTensorGraph, self).__init__(**kwargs)
    super(DAGModel, self).__init__(**kwargs)
    self.build_graph()

  def build_graph(self):
@@ -508,7 +508,7 @@ class DAGTensorGraph(TensorGraph):
        yield feed_dict

  def predict_on_generator(self, generator, transformers=[], outputs=None):
    out = super(DAGTensorGraph, self).predict_on_generator(
    out = super(DAGModel, self).predict_on_generator(
        generator, transformers=[], outputs=outputs)
    if outputs is None:
      outputs = self.outputs
@@ -519,7 +519,7 @@ class DAGTensorGraph(TensorGraph):
    return out


class PetroskiSuchTensorGraph(TensorGraph):
class PetroskiSuchModel(TensorGraph):
  """
      Model from Robust Spatial Filtering with Graph Convolutional Neural Networks
      https://arxiv.org/abs/1703.00792
@@ -545,7 +545,7 @@ class PetroskiSuchTensorGraph(TensorGraph):
    self.error_bars = True if 'error_bars' in kwargs and kwargs['error_bars'] else False
    self.dropout = dropout
    kwargs['use_queue'] = False
    super(PetroskiSuchTensorGraph, self).__init__(**kwargs)
    super(PetroskiSuchModel, self).__init__(**kwargs)
    self.build_graph()

  def build_graph(self):
@@ -952,7 +952,7 @@ class GraphConvModel(TensorGraph):
    return y_


class MPNNTensorGraph(TensorGraph):
class MPNNModel(TensorGraph):
  """ Message Passing Neural Network,
      default structures built according to https://arxiv.org/abs/1511.06391 """

@@ -987,7 +987,7 @@ class MPNNTensorGraph(TensorGraph):
    self.T = T
    self.M = M
    self.mode = mode
    super(MPNNTensorGraph, self).__init__(**kwargs)
    super(MPNNModel, self).__init__(**kwargs)
    self.build_graph()

  def build_graph(self):
@@ -1172,3 +1172,47 @@ class WeaveTensorGraph(WeaveModel):
  def __init__(self, *args, **kwargs):

    super(WeaveModel, self).__init__(*args, **kwargs)


class DTNNTensorGraph(DTNNModel):

  warnings.warn(
      TENSORGRAPH_DEPRECATION.format("DTNNTensorGraph", "DTNNModel"),
      FutureWarning)

  def __init__(self, *args, **kwargs):

    super(DTNNModel, self).__init__(*args, **kwargs)


class DAGTensorGraph(DAGModel):

  warnings.warn(
      TENSORGRAPH_DEPRECATION.format("DAGTensorGraph", "DAGModel"),
      FutureWarning)

  def __init__(self, *args, **kwargs):

    super(DAGModel, self).__init__(*args, **kwargs)


class PetroskiSuchTensorGraph(PetroskiSuchModel):

  warnings.warn(
      TENSORGRAPH_DEPRECATION.format("PetroskiSuchTensorGraph",
                                     "PetroskiSuchModel"), FutureWarning)

  def __init__(self, *args, **kwargs):

    super(PetroskiSuchModel, self).__init__(*args, **kwargs)


class MPNNTensorGraph(MPNNModel):

  warnings.warn(
      TENSORGRAPH_DEPRECATION.format("MPNNTensorGraph", "MPNNModel"),
      FutureWarning)

  def __init__(self, *args, **kwargs):

    super(MPNNModel, self).__init__(*args, **kwargs)
Loading