Commit 73dd9b9b authored by peastman's avatar peastman
Browse files

Created Log layer

parent 67bbb06e
Loading
Loading
Loading
Loading
+21 −1
Original line number Diff line number Diff line
@@ -301,7 +301,7 @@ class Dense(Layer):
    self.time_series = time_series
    try:
      parent_shape = self.in_layers[0].shape
      self._shape = (parent_shape[0], out_channels)
      self._shape = tuple(parent_shape[:-1] + [out_channels])
    except:
      pass
    self._reuse = False
@@ -931,6 +931,26 @@ class Multiply(Layer):
    return out_tensor


class Log(Layer):
  """Compute the natural log of the input."""

  def __init__(self, in_layers=None, **kwargs):
    super(Log, self).__init__(in_layers, **kwargs)
    try:
      self._shape = in_layers[0].shape
    except:
      pass

  def create_tensor(self, in_layers=None, set_tensors=True, **kwargs):
    inputs = self._get_input_tensors(in_layers)
    if len(inputs) != 1:
      raise ValueError('Log must have a single parent')
    out_tensor = tf.log(inputs[0])
    if set_tensors:
      self.out_tensor = out_tensor
    return out_tensor


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

+1 −1
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ class TensorGraph(Model):
        while True:
          yield {self._training_placeholder: 1.0}
      for d in feed_dict_generator:
        feed_dict = {k.out_tensor: v for k, v in six.iteritems(d)}
        feed_dict = dict(d)
        feed_dict[self._training_placeholder] = 1.0
        yield feed_dict

+8 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ 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 Log
from deepchem.models.tensorgraph.layers import InteratomicL2Distances
from deepchem.models.tensorgraph.layers import SoftMaxCrossEntropy
from deepchem.models.tensorgraph.layers import ReduceMean
@@ -264,6 +265,13 @@ class TestLayers(test_util.TensorFlowTestCase):
                              tf.constant(value3))
      assert np.array_equal(value1 * value2 * value3, out_tensor.eval())

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

  def test_interatomic_distances(self):
    """Test that the interatomic distance calculation works."""
    N_atoms = 5
+11 −1
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ import numpy as np
import tensorflow as tf
from deepchem.models import TensorGraph
from deepchem.models.tensorgraph.layers import Feature, Conv1D, Dense, Flatten, Reshape, Squeeze, Transpose, \
    CombineMeanStd, Repeat, Gather, GRU, L2Loss, Concat, SoftMax, Constant, Variable, Add, Multiply, InteratomicL2Distances, \
    CombineMeanStd, Repeat, Gather, GRU, L2Loss, Concat, SoftMax, Constant, Variable, Add, Multiply, Log, InteratomicL2Distances, \
    SoftMaxCrossEntropy, ReduceMean, ToFloat, ReduceSquareDifference, Conv2D, MaxPool, ReduceSum, GraphConv, GraphPool, \
    GraphGather, BatchNorm, WeightedError, \
    LSTMStep, AttnLSTMEmbedding, IterRefLSTMEmbedding
@@ -163,6 +163,16 @@ def test_Variable_pickle():
  tg.save()


def test_Log_pickle():
  tg = TensorGraph()
  feature = Feature(shape=(tg.batch_size, 1))
  layer = Log(feature)
  tg.add_output(layer)
  tg.set_loss(layer)
  tg.build()
  tg.save()


def testInteratomicL2Distances():
  """
    TODO(LESWING) what is ndim here?