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

Removing Keras dependencies

parent 5a398b28
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ from __future__ import unicode_literals
import sys
import traceback
import tensorflow as tf
from keras import backend as K

def cosine_distances(test, support):
  """Computes pairwise cosine distances between provided tensors
@@ -24,9 +23,9 @@ def cosine_distances(test, support):
    Of shape (n_test, n_support)
  """
  rnorm_test = tf.rsqrt(tf.reduce_sum(tf.square(test), 1,
                     keep_dims=True)) + K.epsilon()
                     keep_dims=True)) + 1e-7 
  rnorm_support = tf.rsqrt(tf.reduce_sum(tf.square(support), 1,
                           keep_dims=True)) + K.epsilon()
                           keep_dims=True)) + 1e-7 
  test_normalized = test * rnorm_test
  support_normalized = support * rnorm_support

+7 −12
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import sklearn
import shutil
import tensorflow as tf
import deepchem as dc
from keras import backend as K
from tensorflow.python.framework import test_util
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
@@ -468,8 +467,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
    tf.set_random_seed(123)
    g = tf.Graph()
    sess = tf.Session(graph=g)
    K.set_session(sess)
    with g.as_default():
    n_tasks = 1
    n_samples = 10
    n_features = 3
@@ -488,6 +485,8 @@ class TestOverfit(test_util.TensorFlowTestCase):

    n_feat = 75
    batch_size = 10

    with g.as_default():
      graph_model = dc.nn.SequentialGraph(n_feat)
      graph_model.add(dc.nn.GraphConv(64, activation='relu'))
      graph_model.add(dc.nn.BatchNormalization(epsilon=1e-5, mode=1))
@@ -518,8 +517,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
    tf.set_random_seed(123)
    g = tf.Graph()
    sess = tf.Session(graph=g)
    K.set_session(sess)
    with g.as_default():
    n_tasks = 1
    n_samples = 10
    n_features = 3
@@ -539,6 +536,8 @@ class TestOverfit(test_util.TensorFlowTestCase):

    n_feat = 75
    batch_size = 10

    with g.as_default():
      graph_model = dc.nn.SequentialGraph(n_feat)
      graph_model.add(dc.nn.GraphConv(64, activation='relu'))
      graph_model.add(dc.nn.BatchNormalization(epsilon=1e-5, mode=1))
@@ -569,8 +568,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
    tf.set_random_seed(123)
    g = tf.Graph()
    sess = tf.Session(graph=g)
    K.set_session(sess)
    with g.as_default():
    n_tasks = 1
    n_feat = 75
    max_depth = 4
@@ -590,6 +587,7 @@ class TestOverfit(test_util.TensorFlowTestCase):

    classification_metric = dc.metrics.Metric(dc.metrics.accuracy_score)

    with g.as_default():
      support_model = dc.nn.SequentialSupportGraph(n_feat)
      
      # Add layers
@@ -634,8 +632,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
    tf.set_random_seed(123)
    g = tf.Graph()
    sess = tf.Session(graph=g)
    K.set_session(sess)
    with g.as_default():
    n_tasks = 1
    n_feat = 75
    max_depth = 4
@@ -652,9 +648,9 @@ class TestOverfit(test_util.TensorFlowTestCase):
    loader = dc.data.CSVLoader(
        tasks=tasks, smiles_field="smiles", featurizer=featurizer)
    dataset = loader.featurize(input_file)

    classification_metric = dc.metrics.Metric(dc.metrics.accuracy_score)

    with g.as_default():
      support_model = dc.nn.SequentialSupportGraph(n_feat)
      
      # Add layers
@@ -701,8 +697,6 @@ class TestOverfit(test_util.TensorFlowTestCase):
    """Test resi-lstm multitask overfits tiny data."""
    g = tf.Graph()
    sess = tf.Session(graph=g)
    K.set_session(sess)
    with g.as_default():
    n_tasks = 1
    n_feat = 75
    max_depth = 4
@@ -723,6 +717,7 @@ class TestOverfit(test_util.TensorFlowTestCase):
    classification_metric = dc.metrics.Metric(
        dc.metrics.accuracy_score)

    with g.as_default():
      support_model = dc.nn.SequentialSupportGraph(n_feat)
      
      # Add layers
+0 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import sklearn
import shutil
import tensorflow as tf
import deepchem as dc
from keras import backend as K
from tensorflow.python.framework import test_util
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
+0 −1
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@ import tempfile
import numpy as np
import deepchem as dc
import tensorflow as tf
from keras import backend as K
from sklearn.ensemble import RandomForestClassifier

class TestReload(unittest.TestCase):
+8 −7
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ import numpy as np
import tensorflow as tf
import sys 
import time
from keras import backend as K
from deepchem.models import Model
from deepchem.data import pad_batch
from deepchem.data import NumpyDataset
@@ -50,7 +49,7 @@ class SupportGraphClassifier(Model):
    self.support_batch_size = support_batch_size

    self.learning_rate = learning_rate
    self.epsilon = K.epsilon()
    self.epsilon = 1e-7 

    self.add_placeholders()
    self.pred_op, self.scores_op, self.loss_op = self.add_training_loss()
@@ -69,17 +68,19 @@ class SupportGraphClassifier(Model):
  def add_placeholders(self):
    """Adds placeholders to graph."""
    self.test_label_placeholder = Input(
        tensor=K.placeholder(shape=(self.test_batch_size), dtype='float32',
        tensor=tf.placeholder(dtype='float32', shape=(self.test_batch_size),
        name="label_placeholder"))
    self.test_weight_placeholder = Input(
        tensor=K.placeholder(shape=(self.test_batch_size), dtype='float32',
        tensor=tf.placeholder(dtype='float32', shape=(self.test_batch_size),
        name="weight_placeholder"))

    # TODO(rbharath): Should weights for the support be used?
    # Support labels
    self.support_label_placeholder = Input(
        tensor=K.placeholder(shape=[self.support_batch_size], dtype='float32',
        tensor=tf.placeholder(dtype='float32', shape=[self.support_batch_size],
        name="support_label_placeholder"))
    self.phase = tf.placeholder(dtype='bool',
                                name='keras_learning_phase')

  def construct_feed_dict(self, test, support, training=True, add_phase=False):
    """Constructs tensorflow feed from test/support sets."""
@@ -96,7 +97,7 @@ class SupportGraphClassifier(Model):
    feed_dict[self.test_weight_placeholder] = np.squeeze(test.w)

    if add_phase:
      feed_dict[K.learning_phase()] = training
      feed_dict[self.phase] = training
    return feed_dict

  def old_fit(self, dataset, n_trials=1000, n_steps_per_trial=1, n_pos=1,
@@ -281,7 +282,7 @@ class SupportGraphClassifier(Model):

    # Clip softmax probabilities to range [epsilon, 1-epsilon]
    # Shape (n_test,)
    pred = tf.clip_by_value(pred, K.epsilon(), 1.-K.epsilon())
    pred = tf.clip_by_value(pred, 1e-7, 1.-1e-7)

    # Convert to logit space using inverse sigmoid (logit) function
    # logit function: log(pred) - log(1-pred)
Loading