<commit_before>__authors__ = "<NAME>"
__copyright__ = "Copyright 2011, Universite de Montreal"
__credits__ = ["<NAME>"]
__license__ = "3-clause BSD"
__maintainer__ = "<NAME>"


import time
from pylearn2.models import Model
from theano import config, function, shared
import theano.tensor as T
import numpy as np
import warnings
from theano.gof.op import get_debug_values, debug_error_message
from pylearn2.utils import make_name, sharedX, as_floatX
from pylearn2.expr.information_theory import entropy_binary_vector
from theano.tensor.shared_randomstreams import RandomStreams

warnings.warn('s3c changing the recursion limit')
import sys
sys.setrecursionlimit(50000)

from pylearn2.models.s3c import numpy_norms
from pylearn2.models.s3c import theano_norms
from pylearn2.models.s3c import full_min
from pylearn2.models.s3c import full_max
from theano.printing import min_informative_str

warnings.warn("""
TODO/NOTES
The sampler ought to store the state of all but the topmost hidden layer
learning updates will be based on marginalizing out this topmost layer
to reduce noise a bit
each round of negative phase sampling should start by sampling the topmost
layer, then sampling downward from there
""")

class Sampler:
    def __init__(self, theano_rng):
        self.theano_rng = theano_rng

    def __call__(self, P):
        return self.theano_rng.binomial(size = P.shape, n = 1, p = P, dtype = P.dtype)

class DBM(Model):

    def __init__(self, rbms,
                    use_cd = False,
                        negative_chains = 0,
                       inference_procedure = None,
                       monitor_params = False,
                       print_interval = 10000):
        """
            rbms: list of rbms to stack
                    all rbms must be of type pylearn2.models.rbm, and not a subclass
                    first entry is the visible rbm
                    DBM may destroy these rbms-- it won't delete them,
                    but it may do terrible things to them

                    the DBM parameters will be constructed by taking the visible biases
                    and weights from each RBM. only the topmost RBM will additionally
                    donate its hidden biases.
            negative_chains: the number of negative chains to simulate
            inference_procedure: a pylearn2.models.dbm.InferenceProcedure object
                (if None, assumes the model is not meant to run on its own)
            print_interval: every print_interval examples, print out a status summary

        """

        self.monitor_params = monitor_params

        self.use_cd = use_cd

        if use_cd:
            assert negative_chains == 0
        else:
            assert negative_chains > 0

        warnings.warn("""The DBM class is still under development, and currently mostly
                only supports use as a component of a larger model that remains
                private. Contact <NAME> if you have questions about the current
                status of this class.""")

        super(DBM,self).__init__()

        self.autonomous = False

        self.rbms = rbms
        self.negative_chains = negative_chains

        self.monitoring_channel_prefix = ""

        if inference_procedure is None:
            self.autonomous = False
            inference_procedure = InferenceProcedure()

        self.inference_procedure = inference_procedure
        self.inference_procedure.register_model(self)
        self.autonomous = False
        if self.inference_procedure.autonomous:
            raise NotImplementedError("No such thing as an autonomous DBM yet")

        self.print_interval = print_interval

        #copy parameters from RBM to DBM, ignoring bias_hid of all but last RBM
        self.W = [ rbm.weights for rbm in self.rbms]
        for i, W in enumerate(self.W):
            W.name = 'dbm_W[%d]' % (i,)
        self.bias_vis = rbms[0].bias_vis
        self.bias_hid = [ rbm.bias_vis for rbm in self.rbms[1:] ]
        self.bias_hid.append(self.rbms[-1].bias_hid)
        for i, bias_hid in enumerate(self.bias_hid):
            bias_hid.name = 'dbm_bias_hid[%d]' % (i,)

        self.reset_rng()

        self.redo_everything()

    def reset_rng(self):
        self.rng = np.random.RandomState([1,2,3])

    def redo_everything(self):
        """ compiles learn_func if necessary
            makes new negative chains
            does not reset weights or biases
        """

        #compile learn_func if necessary
        if self.autonomous:
            self.redo_theano()

        #make the negative chains
        if not self.use_cd:<commit_msg>made DBM support Spaces<commit_after>__authors__ = "<NAME>"
__copyright__ = "Copyright 2011, Universite de Montreal"
__credits__ = ["<NAME>"]
__license__ = "3-clause BSD"
__maintainer__ = "<NAME>"


import time
from pylearn2.models import Model
from theano import config, function, shared
import theano.tensor as T
import numpy as np
import warnings
from theano.gof.op import get_debug_values, debug_error_message
from pylearn2.utils import make_name, sharedX, as_floatX
from pylearn2.expr.information_theory import entropy_binary_vector
from theano.tensor.shared_randomstreams import RandomStreams

warnings.warn('s3c changing the recursion limit')
import sys
sys.setrecursionlimit(50000)

from pylearn2.models.s3c import numpy_norms
from pylearn2.models.s3c import theano_norms
from pylearn2.models.s3c import full_min
from pylearn2.models.s3c import full_max
from theano.printing import min_informative_str

warnings.warn("""
TODO/NOTES
The sampler ought to store the state of all but the topmost hidden layer
learning updates will be based on marginalizing out this topmost layer
to reduce noise a bit
each round of negative phase sampling should start by sampling the topmost
layer, then sampling downward from there
""")

class Sampler:
    def __init__(self, theano_rng):
        self.theano_rng = theano_rng

    def __call__(self, P):
        return self.theano_rng.binomial(size = P.shape, n = 1, p = P, dtype = P.dtype)

class DBM(Model):

    def __init__(self, rbms,
                    use_cd = False,
                        negative_chains = 0,
                       inference_procedure = None,
                       monitor_params = False,
                       print_interval = 10000):
        """
            rbms: list of rbms to stack
                    all rbms must be of type pylearn2.models.rbm, and not a subclass
                    first entry is the visible rbm
                    DBM may destroy these rbms-- it won't delete them,
                    but it may do terrible things to them

                    the DBM parameters will be constructed by taking the visible biases
                    and weights from each RBM. only the topmost RBM will additionally
                    donate its hidden biases.
            negative_chains: the number of negative chains to simulate
            inference_procedure: a pylearn2.models.dbm.InferenceProcedure object
                (if None, assumes the model is not meant to run on its own)
            print_interval: every print_interval examples, print out a status summary

        """

        self.monitor_params = monitor_params

        self.use_cd = use_cd

        if use_cd:
            assert negative_chains == 0
        else:
            assert negative_chains > 0

        warnings.warn("""The DBM class is still under development, and currently mostly
                only supports use as a component of a larger model that remains
                private. Contact <NAME> if you have questions about the current
                status of this class.""")

        super(DBM,self).__init__()

        self.autonomous = False

        self.rbms = rbms
        self.negative_chains = negative_chains

        self.monitoring_channel_prefix = ""

        if inference_procedure is None:
            self.autonomous = False
            inference_procedure = InferenceProcedure()

        self.inference_procedure = inference_procedure
        self.inference_procedure.register_model(self)
        self.autonomous = False
        if self.inference_procedure.autonomous:
            raise NotImplementedError("No such thing as an autonomous DBM yet")

        self.print_interval = print_interval

        #copy parameters from RBM to DBM, ignoring bias_hid of all but last RBM
        self.W = []
        for rbm in self.rbms:
            weights ,= rbm.transformer.get_params()
            self.W.append( weights )

        for i, W in enumerate(self.W):
            W.name = 'dbm_W[%d]' % (i,)
        self.bias_vis = rbms[0].bias_vis
        self.bias_hid = [ rbm.bias_vis for rbm in self.rbms[1:] ]
        self.bias_hid.append(self.rbms[-1].bias_hid)
        for i, bias_hid in enumerate(self.bias_hid):
            bias_hid.name = 'dbm_bias_hid[%d]' % (i,)

        self.reset_rng()

        self.redo_everything()

    def reset_rng(self):
        self.rng = np.random.RandomState([1,2,3])

    def redo_everything(self):
        """ compiles learn_func if necessary
            makes new negative chains
            does not reset weights or biases
        """

        #compile learn_func if necessary
        if self.autonomous:
            self.redo_theano()

        #make the negative chains
        if not self.use_cd:
{'fraction_non_alphanumeric': 0.065033693443149, 'fraction_numerical': 0.008343138303561879, 'mean_word_length': 2.3404787424080027, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '1965487', 'n_tokens_mistral': 2748, 'n_tokens_neox': 2562, 'n_words': 867}