# Path: util.py
# def numpy_floatX(data):
#     """
#         Returns the source as an appropriate float type.
#     """
#     return numpy.asarray(data, dtype=config.floatX)


import theano
from theano import tensor
from util import numpy_floatX
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
    n_batch = x.shape[2]

    dist = Wemb[x.flatten()].reshape([n_words, n_max_letters_in_word, n_batch, dim_proj])
    return dist


def lstm_mask_layer(proj, mask):
    """
    Removes any spurious output from the LSTM that's not covered by a label
    or doesn't correspond to any real input.
    :param proj: Output of the LSTM layer
    :param mask: 1 if the position is valid, 0 otherwise
    :return: The masked values
    """

    return proj * mask[:, :, None]

def per_word_averaging_layer(dist, dist_mask):
    """
    :param proj: Output of the LSTM layer
    :param dist_mask: 4D index tensor
    :return: The per-word averages.
    """

    dist = dist * dist_mask

    dist = dist.dimshuffle(1, 2, 0, 3)
    dist_mask = tensor.cast(dist_mask, theano.config.floatX)
    dist_mask = dist_mask.dimshuffle(1, 2, 0, 3)
    divider = dist_mask.sum(axis=0)

# Path: util.py
# def numpy_floatX(data):
#     """
#         Returns the source as an appropriate float type.
#     """
#     return numpy.asarray(data, dtype=config.floatX)


# Path: nn_layers.py
import theano
from theano import tensor
from util import numpy_floatX
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

    n_batch = x.shape[2]

    dist = Wemb[x.flatten()].reshape([n_words, n_max_letters_in_word, n_batch, dim_proj])
    return dist


def lstm_mask_layer(proj, mask):
    """
    Removes any spurious output from the LSTM that's not covered by a label
    or doesn't correspond to any real input.
    :param proj: Output of the LSTM layer
    :param mask: 1 if the position is valid, 0 otherwise
    :return: The masked values
    """

    return proj * mask[:, :, None]

def per_word_averaging_layer(dist, dist_mask):
    """
    :param proj: Output of the LSTM layer
    :param dist_mask: 4D index tensor
    :return: The per-word averages.
    """

    dist = dist * dist_mask

    dist = dist.dimshuffle(1, 2, 0, 3)
    dist_mask = tensor.cast(dist_mask, theano.config.floatX)
    dist_mask = dist_mask.dimshuffle(1, 2, 0, 3)
    divider = dist_mask.sum(axis=0)

    divider += tensor.eq(divider, numpy_floatX(0.0))