Unverified Commit d975b876 authored by Bharath Ramsundar's avatar Bharath Ramsundar Committed by GitHub
Browse files

Merge pull request #1966 from deepchem/tf_optional

Make tensorflow_probability a soft dependency
parents 036edfe6 26bf0d9c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ DeepChem has a number of "soft" requirements. These are packages which are neede
- [RDKit](http://www.rdkit.org/docs/Install.html)
- [simdna](https://github.com/kundajelab/simdna)
- [XGBoost](https://xgboost.readthedocs.io/en/latest/)
- [Tensorflow Probability](https://www.tensorflow.org/probability)

## Installation

+17 −1
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
import tensorflow as tf
import tensorflow_probability as tfp
import numpy as np
import collections
from tensorflow.keras import activations, initializers, backend
@@ -2184,6 +2183,17 @@ class WeaveLayer(tf.keras.layers.Layer):


class WeaveGather(tf.keras.layers.Layer):
  """Implements the weave-gathering section of weave convolutions.

  Implements the gathering layer from the following paper:

  Kearnes, Steven, et al. "Molecular graph convolutions: moving beyond
  fingerprints." Journal of computer-aided molecular design 30.8 (2016): 595-608.

  The weave gathering layer gathers	per-atom features to create a
  molecule-level fingerprint in a weave convolutional network. This layer can
  also perform Gaussian histogram expansion as detailed in the original paper.
  """

  def __init__(self,
               batch_size,
@@ -2208,6 +2218,11 @@ class WeaveGather(tf.keras.layers.Layer):
    activation: str, optional
      Activation function applied
    """
    try:
      import tensorflow_probability as tfp
    except ModuleNotFoundError:
      raise ValueError(
          "This class requires tensorflow-probability to be installed.")
    super(WeaveGather, self).__init__(**kwargs)
    self.n_input = n_input
    self.batch_size = batch_size
@@ -2252,6 +2267,7 @@ class WeaveGather(tf.keras.layers.Layer):
    return output_molecules

  def gaussian_histogram(self, x):
    import tensorflow_probability as tfp
    gaussian_memberships = [(-1.645, 0.283), (-1.080, 0.170), (-0.739, 0.134),
                            (-0.468, 0.118), (-0.228, 0.114), (0., 0.114),
                            (0.228, 0.114), (0.468, 0.118), (0.739, 0.134),
+17 −2
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ from deepchem.models import KerasModel
from deepchem.models.optimizers import Adam
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
import collections
import copy
import multiprocessing
@@ -40,10 +39,20 @@ class A2CLossDiscrete(object):


class A2CLossContinuous(object):
  """This class computes the loss function for A2C with continuous action spaces."""
  """This class computes the loss function for A2C with continuous action spaces.

  Note
  ----
  This class requires tensorflow-probability to be installed.
  """

  def __init__(self, value_weight, entropy_weight, mean_index, std_index,
               value_index):
    try:
      import tensorflow_probability as tfp
    except ModuleNotFoundError:
      raise ValueError(
          "This class requires tensorflow-probability to be installed.")
    self.value_weight = value_weight
    self.entropy_weight = entropy_weight
    self.mean_index = mean_index
@@ -51,6 +60,7 @@ class A2CLossContinuous(object):
    self.value_index = value_index

  def __call__(self, outputs, labels, weights):
    import tensorflow_probability as tfp
    mean = outputs[self.mean_index]
    std = outputs[self.std_index]
    value = outputs[self.value_index]
@@ -112,6 +122,11 @@ class A2C(object):
  except specifying the new goal.  It should return that list of states, and the rewards that would
  have been received for taking the specified actions from those states.  The output arrays may be
  shorter than the input ones, if the modified rollout would have terminated sooner.


  Note
  ----
  Using this class on continuous action spaces requires that `tensorflow_probability` be installed.
  """

  def __init__(self,