<commit_before>import theano
import lasagne

### ALIASES ###
T = theano.tensor
L = lasagne.layers
nl = lasagne.nonlinearities


### NONLINEARITIES ETC###
def binarize(input_tensor):
    """
    Convert input to el {0, 1}

    Might be better to achieve this by
    forcing boolean shared on ConvLayer subclass
    """
    return T.cast(input_tensor >= .5, theano.config.floatX)


def sum_count_conv(input, W, input_shape, W_shape,
                    subsample=None, border_mode=None, filter_flip=None):
    """
    After convolving, checks each filter map (per channel) to determine if
    feature activation equals weight sum

    Meant for use with binarized filters,
    resulting in an on/off "feature detector"

    Not sure what happens without binarized features, but likely not what you
    wanted!

    Does not currently work due to dim mismatch error; debugging soon
    """
    W_sum = W.sum(axis=-1).sum(axis=-1)
    conved = T.nnet.conv2d(
        input, W, input_shape, W_shape,
        subsample=subsample, border_mode=border_mode, filter_flip=filter_flip
    )

    comparand = T.tile(
        W_sum,
        (conved.shape[0], conved.shape[2], conved.shape[3], 1)
    )

    comparand = comparand.dimshuffle(0, 3, 2, 1)

    return T.eq(conved, comparand)


### LAYERS ###


def make_FixLayer(input_var):
    """
    Creates a layer that "fixes" a value distribution for a given input size

    This is used to enforce (m, n, k) game rules - it is not legal to move
    at an occupied location, so in a value/choice distribution, occupied locations
    should be set to 0.

    Should return to this to make a proper class using super to pass input_shape
    (low priority)
    """

    class FixLayer(L.Layer):
        def get_output_for(self, input, **kwargs):
            corrector = (1 - input_var.sum(axis=1))
            reshape = (input_var.shape[0], 36)
            corrector = corrector.reshape(reshape)
            numer = input * corrector
            return numer

    return FixLayer


class BinConvLayer(L.Conv2DLayer):
    """
    Binarizes weights before computing convolution
    """

    def convolve(self, input, **kwargs):
        border_mode = 'half' if self.pad == 'same' else self.pad
        binarized = binarize(self.W)<commit_msg>binconv is lame<commit_after>import theano
import lasagne

### ALIASES ###
T = theano.tensor
L = lasagne.layers
nl = lasagne.nonlinearities


### NONLINEARITIES ETC###
def binarize(input_tensor):
    """
    Convert input to el {0, 1}

    Might be better to achieve this by
    forcing boolean shared on ConvLayer subclass
    """
    return 2 * T.cast(input_tensor >= .5, theano.config.floatX) - 1


def sum_count_conv(input, W, input_shape, W_shape,
                    subsample=None, border_mode=None, filter_flip=None):
    """
    After convolving, checks each filter map (per channel) to determine if
    feature activation equals weight sum

    Meant for use with binarized filters,
    resulting in an on/off "feature detector"

    Not sure what happens without binarized features, but likely not what you
    wanted!

    Does not currently work due to dim mismatch error; debugging soon
    """
    W_sum = W.sum(axis=-1).sum(axis=-1)
    conved = T.nnet.conv2d(
        input, W, input_shape, W_shape,
        subsample=subsample, border_mode=border_mode, filter_flip=filter_flip
    )

    comparand = T.tile(
        W_sum,
        (conved.shape[0], conved.shape[2], conved.shape[3], 1)
    )

    comparand = comparand.dimshuffle(0, 3, 2, 1)

    return T.eq(conved, comparand)


### LAYERS ###


def make_FixLayer(input_var):
    """
    Creates a layer that "fixes" a value distribution for a given input size

    This is used to enforce (m, n, k) game rules - it is not legal to move
    at an occupied location, so in a value/choice distribution, occupied locations
    should be set to 0.

    Should return to this to make a proper class using super to pass input_shape
    (low priority)
    """

    class FixLayer(L.Layer):
        def get_output_for(self, input, **kwargs):
            corrector = (1 - input_var.sum(axis=1))
            reshape = (input_var.shape[0], 36)
            corrector = corrector.reshape(reshape)
            numer = input * corrector
            return numer

    return FixLayer


class BinConvLayer(L.Conv2DLayer):
    """
    Binarizes weights before computing convolution

    This does not work very well; gradient is 0 at all locations, so backprop
    does not work correctly. Consider using tanh or sigmoid activation function
    instead.
    """

    def convolve(self, input, **kwargs):
        border_mode = 'half' if self.pad == 'same' else self.pad
        binarized = binarize(self.W)
{'fraction_non_alphanumeric': 0.09217758985200845, 'fraction_numerical': 0.009513742071881607, 'mean_word_length': 2.8122481869460114, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5726007', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1438, 'n_words': 487}