<commit_before>import numpy as np
import tensorflow as tf

from h_RNN.RNN import Generator
from h_RNN.SpRNN import SparseRNN


class EmbedRNN(SparseRNN):
    def __init__(self, **kwargs):
        super(EmbedRNN, self).__init__(**kwargs)
        self._embedding_size = kwargs.get("embedding_size", 200)

    def _define_input(self, im, om):
        self._tfx = tf.placeholder(tf.int32, shape=[None, None])
        embeddings = tf.Variable(tf.random_uniform([im, self._embedding_size], -1.0, 1.0))
        self._input = tf.nn.embedding_lookup(embeddings, self._tfx)
        self._tfy = tf.placeholder(tf.int32, shape=[None])

    def _get_loss(self, eps):
        return tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self._tfy, logits=self._output)
        )

    def _get_output(self, rnn_outputs, rnn_states, n_history):
        if not self._squeeze:
            raise ValueError("Please squeeze the outputs when using SparseRNN")
        super(SparseRNN, self)._get_output(rnn_outputs, rnn_states, n_history)


class EmbedRNNForOp(EmbedRNN):
    def __init__(self, **kwargs):
        super(EmbedRNNForOp, self).__init__(**kwargs)
        if not self._params["generator_params"]:
            self._params["generator_params"] = {"n_digit": 2}
        self._params["boost"] = 0
        self._op = ""

    def _verbose(self):
        x_test, y_test = self._generator.gen(1, boost=self._params["boost"])<commit_msg>Update RNN with Embedding<commit_after>import numpy as np
import tensorflow as tf

from h_RNN.RNN import Generator
from h_RNN.SpRNN import SparseRNN


class EmbedRNN(SparseRNN):
    def __init__(self, **kwargs):
        super(EmbedRNN, self).__init__(**kwargs)
        self._embedding_size = kwargs.get("embedding_size", 200)

    def _define_input(self, im, om):
        self._tfx = tf.placeholder(tf.int32, shape=[None, None])
        embeddings = tf.Variable(tf.random_uniform([im, self._embedding_size], -1.0, 1.0))
        self._input = tf.nn.embedding_lookup(embeddings, self._tfx)
        self._tfy = tf.placeholder(tf.int32, shape=[None])


class EmbedRNNForOp(EmbedRNN):
    def __init__(self, **kwargs):
        super(EmbedRNNForOp, self).__init__(**kwargs)
        if not self._params["generator_params"]:
            self._params["generator_params"] = {"n_digit": 2}
        self._params["boost"] = 0
        self._op = ""

    def _verbose(self):
        x_test, y_test = self._generator.gen(1, boost=self._params["boost"])
{'fraction_non_alphanumeric': 0.12726540475231574, 'fraction_numerical': 0.011276681433749497, 'mean_word_length': 3.427807486631016, 'pattern_counts': {'":': 2, '<': 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': '4495917', 'n_tokens_mistral': 869, 'n_tokens_neox': 836, 'n_words': 163}