<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import queue

from chainer import Chain, ChainList, cuda, initializers, link, Variable
import chainer.functions as F
import chainer.links as L
from chainer.links.connection.n_step_rnn import argsort_list_descent, permutate_list
import numpy as np


class Embed(ChainList):

    def __init__(self, *args):
        embeds = []
        for i, embeddings in enumerate(args):
            vocab_size, embed_size = embeddings.shape
            embed = L.EmbedID(
                in_size=vocab_size,
                out_size=embed_size,
                initialW=embeddings,
            )
            embeds.append(embed)
        super(Embed, self).__init__(*embeds)

    def __call__(self, *xs):
        hs = []
        batch = len(xs[0])
        for i in range(batch):
            _hs = F.concat([embed(self.xp.array(_xs[i])) for _xs, embed in zip(xs, self)])
            hs.append(_hs)
        return hs


class LSTM(L.NStepLSTM):

    def __init__(self, in_size, out_size, dropout=0.5, use_cudnn=True):
        n_layers = 1
        super(LSTM, self).__init__(n_layers, in_size, out_size, dropout, use_cudnn)
        self.state_size = out_size
        self.reset_state()

    def to_cpu(self):
        super(LSTM, self).to_cpu()
        if self.cx is not None:
            self.cx.to_cpu()
        if self.hx is not None:
            self.hx.to_cpu()

    def to_gpu(self, device=None):
        super(LSTM, self).to_gpu(device)
        if self.cx is not None:
            self.cx.to_gpu(device)
        if self.hx is not None:<commit_msg>implement MLP.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math
import queue

from chainer import Chain, ChainList, cuda, initializers, link, Variable
import chainer.functions as F
import chainer.links as L
from chainer.links.connection.n_step_rnn import argsort_list_descent, permutate_list
import numpy as np


class Embed(ChainList):

    def __init__(self, *args):
        embeds = []
        for i, embeddings in enumerate(args):
            vocab_size, embed_size = embeddings.shape
            embed = L.EmbedID(
                in_size=vocab_size,
                out_size=embed_size,
                initialW=embeddings,
            )
            embeds.append(embed)
        super(Embed, self).__init__(*embeds)

    def __call__(self, *xs):
        hs = []
        batch = len(xs[0])
        for i in range(batch):
            _hs = F.concat([embed(self.xp.array(_xs[i])) for _xs, embed in zip(xs, self)])
            hs.append(_hs)
        return hs


class MLP(ChainList):

    def __init__(self, layers):
        assert all(type(layer) == MLP.Layer for layer in layers)
        super(MLP, self).__init__(*layers)

    def __call__(self, x, train=False):
        for layer in self:
            x = layer(x, train)
        return x

    class Layer(L.Linear):

        def __init__(self, in_size, out_size, wscale=1, bias=0, nobias=False,
                     initialW=None, initial_bias=None, activation=None, dropout=0):
            super(MLP.Layer, self).__init__(in_size, out_size, wscale, bias, nobias, initialW, initial_bias)
            if activation is None:
                self._activate = lambda x: x
            else:
                assert callable(activation) and hasattr(F.activation, activation.__name__)
                self._activate = activation
            assert dropout == 0 or type(dropout) == float
            self._dropout_ratio = dropout
            if dropout > 0:
                self._dropout_func = lambda x, ratio, train: F.dropout(x, ratio, train)
            else:
                self._dropout_func = lambda x, ratio, train: x

        def __call__(self, x, train=False):
            y = super(MLP.Layer, self).__call__(x)
            return self._dropout_func(self._activate(y), self._dropout_ratio, train)


class LSTM(L.NStepLSTM):

    def __init__(self, in_size, out_size, dropout=0.5, use_cudnn=True):
        n_layers = 1
        super(LSTM, self).__init__(n_layers, in_size, out_size, dropout, use_cudnn)
        self.state_size = out_size
        self.reset_state()

    def to_cpu(self):
        super(LSTM, self).to_cpu()
        if self.cx is not None:
            self.cx.to_cpu()
        if self.hx is not None:
            self.hx.to_cpu()

    def to_gpu(self, device=None):
        super(LSTM, self).to_gpu(device)
        if self.cx is not None:
            self.cx.to_gpu(device)
        if self.hx is not None:
{'fraction_non_alphanumeric': 0.10608078118064802, 'fraction_numerical': 0.003328894806924101, 'mean_word_length': 2.1628070175438596, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '571482', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1436, 'n_words': 357}