Commit 5e7c59d3 authored by Peter Eastman's avatar Peter Eastman
Browse files

Created some basic TensorGraph layers

parent 375bd681
Loading
Loading
Loading
Loading
+101 −0
Original line number Diff line number Diff line
@@ -476,6 +476,107 @@ class Concat(Layer):
    return out_tensor


class Constant(Layer):
  """Output a constant value."""

  def __init__(self, value, dtype=tf.float32, **kwargs):
    """Construct a constant layer.

    Parameters
    ----------
    value: array
      the value the layer should output
    dtype: tf.DType
      the data type of the output value.
    """
    self.value = value
    self.dtype = dtype
    super(Constant, self).__init__(**kwargs)

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    out_tensor = tf.constant(self.value, dtype=self.dtype)
    if set_tensors:
      self.out_tensor = out_tensor
    return out_tensor


class Variable(Layer):
  """Output a trainable value."""

  def __init__(self, initial_value, dtype=tf.float32, **kwargs):
    """Construct a variable layer.

    Parameters
    ----------
    initial_value: array
      the initial value the layer should output
    dtype: tf.DType
      the data type of the output value.
    """
    self.initial_value = initial_value
    self.dtype = dtype
    super(Variable, self).__init__(**kwargs)

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    out_tensor = tf.Variable(self.initial_value, dtype=self.dtype)
    if set_tensors:
      self.out_tensor = out_tensor
    return out_tensor


class Add(Layer):
  """Compute the (optionally weighted) sum of the input layers."""

  def __init__(self, weights=None, **kwargs):
    """Create an Add layer.

    Parameters
    ----------
    weights: array
      an array of length equal to the number of input layers, giving the weight
      to multiply each input by.  If None, all weights are set to 1.
    """
    super(Add, self).__init__(**kwargs)
    self.weights = weights

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    if in_layers is None:
      in_layers = self.in_layers
    in_layers = convert_to_layers(in_layers)
    weights = self.weights
    if weights is None:
      weights = [1] * len(in_layers)
    out_tensor = in_layers[0].out_tensor
    if weights[0] != 1:
      out_tensor *= weights[0]
    for layer, weight in zip(in_layers[1:], weights[1:]):
      if weight == 1:
        out_tensor += layer.out_tensor
      else:
        out_tensor += weight * layer.out_tensor
    if set_tensors:
      self.out_tensor = out_tensor
    return out_tensor


class Multiply(Layer):
  """Compute the product of the input layers."""

  def __init__(self, **kwargs):
    super(Multiply, self).__init__(**kwargs)

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    if in_layers is None:
      in_layers = self.in_layers
    in_layers = convert_to_layers(in_layers)
    out_tensor = in_layers[0].out_tensor
    for layer in in_layers[1:]:
      out_tensor *= layer.out_tensor
    if set_tensors:
      self.out_tensor = out_tensor
    return out_tensor


class InteratomicL2Distances(Layer):
  """Compute (squared) L2 Distances between atoms given neighbors."""

+39 −0
Original line number Diff line number Diff line
@@ -21,6 +21,10 @@ from deepchem.models.tensorgraph.layers import TimeSeriesDense
from deepchem.models.tensorgraph.layers import Input
from deepchem.models.tensorgraph.layers import L2Loss
from deepchem.models.tensorgraph.layers import Concat
from deepchem.models.tensorgraph.layers import Constant
from deepchem.models.tensorgraph.layers import Variable
from deepchem.models.tensorgraph.layers import Add
from deepchem.models.tensorgraph.layers import Multiply
from deepchem.models.tensorgraph.layers import InteratomicL2Distances
from deepchem.models.tensorgraph.layers import SoftMaxCrossEntropy
from deepchem.models.tensorgraph.layers import ReduceMean
@@ -213,6 +217,41 @@ class TestLayers(test_util.TensorFlowTestCase):
      out_tensor = out_tensor.eval()
      assert out_tensor.shape == (batch_size, 2 * n_features)

  def test_constant(self):
    """Test that Constant can be invoked."""
    value = np.random.uniform(size=(2, 3)).astype(np.float32)
    with self.test_session() as sess:
      out_tensor = Constant(value)()
      assert np.array_equal(value, out_tensor.eval())

  def test_variable(self):
    """Test that Variable can be invoked."""
    value = np.random.uniform(size=(2, 3)).astype(np.float32)
    with self.test_session() as sess:
      out_tensor = Variable(value)()
      sess.run(tf.global_variables_initializer())
      assert np.array_equal(value, out_tensor.eval())

  def test_add(self):
    """Test that Add can be invoked."""
    value1 = np.random.uniform(size=(2, 3)).astype(np.float32)
    value2 = np.random.uniform(size=(2, 3)).astype(np.float32)
    value3 = np.random.uniform(size=(2, 3)).astype(np.float32)
    with self.test_session() as sess:
      out_tensor = Add(weights=[1, 2, 1])(
          tf.constant(value1), tf.constant(value2), tf.constant(value3))
      assert np.array_equal(value1 + 2 * value2 + value3, out_tensor.eval())

  def test_multiply(self):
    """Test that Multiply can be invoked."""
    value1 = np.random.uniform(size=(2, 3)).astype(np.float32)
    value2 = np.random.uniform(size=(2, 3)).astype(np.float32)
    value3 = np.random.uniform(size=(2, 3)).astype(np.float32)
    with self.test_session() as sess:
      out_tensor = Multiply()(tf.constant(value1), tf.constant(value2),
                              tf.constant(value3))
      assert np.array_equal(value1 * value2 * value3, out_tensor.eval())

  def test_interatomic_distances(self):
    """Test that the interatomic distance calculation works."""
    N_atoms = 5
+2 −0
Original line number Diff line number Diff line
@@ -111,6 +111,8 @@ class A3C(object):
        use_queue=False,
        graph=tf_graph,
        model_dir=model_dir)
    for f in features:
      graph._add_layer(f)
    graph.add_output(action_prob)
    graph.add_output(value)
    graph.set_loss(loss)
+3 −3
Original line number Diff line number Diff line
import deepchem as dc
from deepchem.models.tensorgraph.layers import Reshape, Dense, SoftMax
from deepchem.models.tensorgraph.layers import Reshape, Variable, SoftMax
import numpy as np
import tensorflow as tf
import unittest
@@ -44,10 +44,10 @@ class TestA3C(unittest.TestCase):
    class TestPolicy(dc.rl.Policy):

      def create_layers(self, state, **kwargs):
        action = Dense(in_layers=state, out_channels=env.n_actions)
        action = Variable(np.ones(env.n_actions))
        output = SoftMax(
            in_layers=[Reshape(in_layers=[action], shape=(-1, env.n_actions))])
        value = Dense(in_layers=state, out_channels=1)
        value = Variable([0.0])
        return {'action_prob': output, 'value': value}

    # Optimize it.