<commit_before>import functools

import tensorflow as tf
import tensorflow.contrib.rnn as rnn
import tensorflow.contrib.seq2seq as s2s

from act_wrapper import ACTWrapper


def lazy_property(func):
    attribute = '_cache_' + func.__name__

    @property
    @functools.wraps(func)
    def decorator(self):
        if not hasattr(self, attribute):
            setattr(self, attribute, func(self))
        return getattr(self, attribute)

    return decorator


class ACTModel:
    def __init__(self, data, target, time_steps, num_classes, rnn_cell,
                 time_penalty=0.001, seq_length=None, optimizer=None):

        self.data = data
        self.target = target
        self.time_steps = time_steps
        self.num_classes = num_classes
        self.cell = rnn_cell
        self.time_penalty = time_penalty
        self.seq_length = seq_length
        self.optimizer = optimizer if optimizer \
            else tf.train.AdamOptimizer(0.001)

        self.num_hidden = rnn_cell.output_size

        self._softmax_loss = None
        self._ponder_loss = None
        self._ponder_steps = None
        self._boolean_mask = None
        self._numerical_mask = None

        if self.seq_length is not None:
            self.boolean_mask = tf.sequence_mask(self.seq_length, self.time_steps)
            self.numeric_mask = tf.cast(self.boolean_mask, data.dtype)

        self.logits
        self.training
        self.evaluation

    @lazy_property
    def logits(self):
        rnn_outputs, rnn_state = rnn.static_rnn(
            self.cell, tf.unstack(self.data, axis=1),
            dtype=tf.float32
        )

        rnn_outputs = tf.reshape(rnn_outputs, [-1, self.num_hidden])
        initial_weights = tf.truncated_normal([self.num_hidden, self.num_classes], stddev=0.01)
        initial_biases = tf.constant(0.1, shape=[self.num_classes])
        output_weights = tf.Variable(initial_weights, name="output_weights")
        output_biases = tf.Variable(initial_biases, name="output_biases")
        logits = tf.matmul(rnn_outputs, output_weights) + output_biases

        return tf.reshape(logits, [-1, self.time_steps, self.num_classes])

    @lazy_property
    def evaluation(self):
        if self.seq_length is not None:
            mistakes = tf.reduce_any(
                tf.logical_or(
                    tf.not_equal(self.target, tf.argmax(self.logits, 2)),
                    tf.logical_not(self.boolean_mask)
                ), axis=1
            )
        else:
            mistakes = tf.reduce_any(
                tf.not_equal(self.target, tf.argmax(self.logits, 2)), axis=1<commit_msg>Error in tensor transposition between hidden RNN outputs and final affine layer has been fixed. Error in computing model accuracy for multi-step variable length batches has been fixed.<commit_after>import functools

import tensorflow as tf
import tensorflow.contrib.rnn as rnn
import tensorflow.contrib.seq2seq as s2s

from act_wrapper import ACTWrapper


def lazy_property(func):
    attribute = '_cache_' + func.__name__

    @property
    @functools.wraps(func)
    def decorator(self):
        if not hasattr(self, attribute):
            setattr(self, attribute, func(self))
        return getattr(self, attribute)

    return decorator


class ACTModel:
    def __init__(self, data, target, time_steps, num_classes, rnn_cell,
                 time_penalty=0.001, seq_length=None, optimizer=None):

        self.data = data
        self.target = target
        self.time_steps = time_steps
        self.num_classes = num_classes
        self.cell = rnn_cell
        self.time_penalty = time_penalty
        self.seq_length = seq_length
        self.optimizer = optimizer if optimizer \
            else tf.train.AdamOptimizer(0.001)

        self.num_hidden = rnn_cell.output_size

        self._softmax_loss = None
        self._ponder_loss = None
        self._ponder_steps = None
        self._boolean_mask = None
        self._numerical_mask = None

        if self.seq_length is not None:
            self.boolean_mask = tf.sequence_mask(self.seq_length, self.time_steps)
            self.numeric_mask = tf.cast(self.boolean_mask, data.dtype)

        self.logits
        self.training
        self.evaluation

    @lazy_property
    def logits(self):
        rnn_outputs, rnn_state = rnn.static_rnn(
            self.cell, tf.unstack(self.data, axis=1),
            dtype=tf.float32
        )

        rnn_outputs = tf.reshape(rnn_outputs, [-1, self.num_hidden])
        initial_weights = tf.truncated_normal([self.num_hidden, self.num_classes], stddev=0.01)
        initial_biases = tf.constant(0.1, shape=[self.num_classes])
        output_weights = tf.Variable(initial_weights, name="output_weights")
        output_biases = tf.Variable(initial_biases, name="output_biases")
        logits = tf.matmul(rnn_outputs, output_weights) + output_biases
        reshaped = tf.reshape(logits, [self.time_steps, -1, self.num_classes])

        return tf.transpose(reshaped, perm=(1, 0, 2))

    @lazy_property
    def evaluation(self):
        if self.seq_length is not None:
            mistakes = tf.reduce_any(
                tf.logical_and(
                    tf.not_equal(self.target, tf.argmax(self.logits, 2)),
                    self.boolean_mask
                ), axis=1
            )
        else:
            mistakes = tf.reduce_any(
                tf.not_equal(self.target, tf.argmax(self.logits, 2)), axis=1
{'fraction_non_alphanumeric': 0.08957415565345081, 'fraction_numerical': 0.009361233480176211, 'mean_word_length': 2.3992514036182158, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1140763', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1668, 'n_words': 343}