Commit 6387e4c4 authored by Bharath Ramsundar's avatar Bharath Ramsundar
Browse files

Some progress in refactor

parent 4309e9ac
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -49,13 +49,14 @@ class TestGraphModels(test_util.TensorFlowTestCase):
    graph_model.add(BatchNormalization(epsilon=1e-5, mode=1))
    graph_model.add(GraphPool())

    # Gather Projection
    graph_model.add(Dense(128, activation='relu'))
    ## Gather Projection
    #graph_model.add(Dense(128, activation='relu'))
    graph_model.add(BatchNormalization(epsilon=1e-5, mode=1))
    graph_model.add(GraphGather(batch_size, activation="tanh"))

    # There should be 8 layers in graph_model
    assert len(graph_model.layers) == 6
    #assert len(graph_model.layers) == 6
    assert len(graph_model.layers) == 5

  def test_sample_attn_lstm_architecture(self):
    """Tests that an attention architecture can be created without crash."""
@@ -83,7 +84,7 @@ class TestGraphModels(test_util.TensorFlowTestCase):
      support_model.join(AttnLSTMEmbedding(n_test, n_support, max_depth))

      # Gather Projection
      support_model.add(Dense(128, activation='relu'))
      #support_model.add(Dense(128, activation='relu'))
      support_model.add_test(BatchNormalization(epsilon=1e-5, mode=1))
      support_model.add_support(BatchNormalization(epsilon=1e-5, mode=1))
      support_model.add(GraphGather(batch_size, activation="tanh"))
+1 −2
Original line number Diff line number Diff line
#from __future__ import absolute_import
from keras import backend as K
#from .utils.generic_utils import get_from_module

def get_from_module(identifier, module_params, module_name,
                    instantiate=False, kwargs=None):
@@ -25,7 +24,7 @@ def get_from_module(identifier, module_params, module_name,
    # Raises
        ValueError: if the identifier cannot be found.
    """
    if isinstance(identifier, str):
    if isinstance(identifier, str) or isinstance(identifier, unicode):
        res = module_params.get(identifier)
        if not res:
            raise ValueError('Invalid ' + str(module_name) + ': ' +
+103 −0
Original line number Diff line number Diff line
from __future__ import absolute_import
from keras import backend as K
from .activations import get_from_module


class Constraint(object):

    def __call__(self, p):
        return p

    def get_config(self):
        return {'name': self.__class__.__name__}


class MaxNorm(Constraint):
    """MaxNorm weight constraint.

    Constrains the weights incident to each hidden unit
    to have a norm less than or equal to a desired value.

    # Arguments
        m: the maximum norm for the incoming weights.
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Convolution2D` layer with `dim_ordering="tf"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.

    # References
        - [Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
    """

    def __init__(self, m=2, axis=0):
        self.m = m
        self.axis = axis

    def __call__(self, p):
        norms = K.sqrt(K.sum(K.square(p), axis=self.axis, keepdims=True))
        desired = K.clip(norms, 0, self.m)
        p *= (desired / (K.epsilon() + norms))
        return p

    def get_config(self):
        return {'name': self.__class__.__name__,
                'm': self.m,
                'axis': self.axis}


class NonNeg(Constraint):
    """Constrains the weights to be non-negative.
    """

    def __call__(self, p):
        p *= K.cast(p >= 0., K.floatx())
        return p


class UnitNorm(Constraint):
    """Constrains the weights incident to each hidden unit to have unit norm.

    # Arguments
        axis: integer, axis along which to calculate weight norms.
            For instance, in a `Dense` layer the weight matrix
            has shape `(input_dim, output_dim)`,
            set `axis` to `0` to constrain each weight vector
            of length `(input_dim,)`.
            In a `Convolution2D` layer with `dim_ordering="tf"`,
            the weight tensor has shape
            `(rows, cols, input_depth, output_depth)`,
            set `axis` to `[0, 1, 2]`
            to constrain the weights of each filter tensor of size
            `(rows, cols, input_depth)`.
    """

    def __init__(self, axis=0):
        self.axis = axis

    def __call__(self, p):
        return p / (K.epsilon() + K.sqrt(K.sum(K.square(p),
                                               axis=self.axis,
                                               keepdims=True)))

    def get_config(self):
        return {'name': self.__class__.__name__,
                'axis': self.axis}


# Aliases.

maxnorm = MaxNorm
nonneg = NonNeg
unitnorm = UnitNorm


def get(identifier, kwargs=None):
    return get_from_module(identifier, globals(), 'constraint',
                           instantiate=True, kwargs=kwargs)
+4 −0
Original line number Diff line number Diff line
@@ -9,6 +9,10 @@ __author__ = "Bharath Ramsundar"
__copyright__ = "Copyright 2016, Stanford University"
__license__ = "GPL"

from . import initializations
from . import regularizers
from . import activations
from . import constraints
from keras import backend as K

def to_list(x):
+16 −0
Original line number Diff line number Diff line
@@ -249,6 +249,22 @@ class GraphConv(Layer):
    # Generate the nb_affine weights and biases
    atom_features_shape = input_shape[0]
    n_features = atom_features_shape[1]
    ############################################################### DEBUG
    print("self.nb_affine")
    print(self.nb_affine)
    print("type(self.nb_affine)")
    print(type(self.nb_affine))
    print("self.nb_filter, type(self.nb_filter)")
    print(self.nb_filter, type(self.nb_filter))
    print("range(self.nb_affine)")
    print(range(self.nb_affine))
    print("K.zeros(shape=[self.nb_filter,])")
    print(K.zeros(shape=[self.nb_filter,]))
    print("self.init")
    print(self.init)
    print("type(self.init)")
    print(type(self.init))
    ############################################################### DEBUG
    self.W_list = [self.init([n_features, self.nb_filter]) 
                   for k in range(self.nb_affine)]
    self.b_list = [K.zeros(shape=[self.nb_filter,])
Loading