<commit_before>'''
Created on Mar 7, 2011

@author: johnsalvatier
'''
import theano
from theano.tensor import sum, grad, TensorType, TensorVariable
from theano import function
import numpy as np 
from __builtin__ import sum as buitin_sum
import time 

def FreeVariable( name, shape, dtype):
    """creates a TensorVariable of the given shape and type"""
    shape = np.atleast_1d(shape)
    var = TensorType(str(dtype), shape == 1)(name)
    var.dshape = tuple(shape)
    var.dsize = int(np.prod(shape))
    return var

class Model(object):
    def __init__(self):
       self.vars = []
       self.factors = [] 


def AddData(model, data, distribution):
    model.factors.append(distribution(data))

def AddVar(model, var, distribution):
    model.vars.append(var)
    model.factors.append(distribution(var))
    
def AddVarIndirect(model, var,proximate_variable, distribution):
    model.vars.append(var)
    model.factors.append(distribution(proximate_variable) * grad(proximate_variable, variable))

class ModelView(object):
    """
    encapsulates the probability model
    """
    def __init__(self, model, derivative_vars = []):

        self.model = model
        
        if derivative_vars == 'all' :
            derivative_vars = [ var for var in model.vars if var.dtype in continuous_types]
            
        self.derivative_vars = derivative_vars 
        
        
        logp_calculation = buitin_sum((sum(factor) for factor in model.factors))
        
        self.mapping = VariableMapping(derivative_vars)
        self.derivative_order = [str(var) for var in derivative_vars]
        
        
        calculations = [logp_calculation] + [grad(logp_calculation, var) for var in derivative_vars]
            
        self.function = function(model.vars, calculations, allow_input_downcast = True)
        
        
    def _evaluate(self,d_repr, chain_state):
        """
        returns logp, derivative1, derivative2...
        does not currently do beyond derivative1
        """
        results = iter(self.function(**chain_state))
        if self.derivative_vars:<commit_msg>added a mode that allows easier debugging for the computational graph<commit_after>'''
Created on Mar 7, 2011

@author: johnsalvatier
'''
import theano
from theano.tensor import sum, grad, TensorType, TensorVariable
from theano import function
import numpy as np 
from __builtin__ import sum as buitin_sum
import time 

def FreeVariable( name, shape, dtype):
    """creates a TensorVariable of the given shape and type"""
    shape = np.atleast_1d(shape)
    var = TensorType(str(dtype), shape == 1)(name)
    var.dshape = tuple(shape)
    var.dsize = int(np.prod(shape))
    return var

class Model(object):
    def __init__(self):
       self.vars = []
       self.factors = [] 


def AddData(model, data, distribution):
    model.factors.append(distribution(data))

def AddVar(model, var, distribution):
    model.vars.append(var)
    model.factors.append(distribution(var))
    
def AddVarIndirect(model, var,proximate_variable, distribution):
    model.vars.append(var)
    model.factors.append(distribution(proximate_variable) * grad(proximate_variable, variable))

class ModelView(object):
    """
    encapsulates the probability model
    """
    def __init__(self, model, derivative_vars = [], mode = None ):

        self.model = model
        
        if derivative_vars == 'all' :
            derivative_vars = [ var for var in model.vars if var.dtype in continuous_types]
            
        self.derivative_vars = derivative_vars 
        
        
        logp_calculation = buitin_sum((sum(factor) for factor in model.factors))
        
        self.mapping = VariableMapping(derivative_vars)
        self.derivative_order = [str(var) for var in derivative_vars]
        
        
        calculations = [logp_calculation] + [grad(logp_calculation, var) for var in derivative_vars]
            
        self.function = function(model.vars, calculations, allow_input_downcast = True, mode = mode)
        
        
    def _evaluate(self,d_repr, chain_state):
        """
        returns logp, derivative1, derivative2...
        does not currently do beyond derivative1
        """
        results = iter(self.function(**chain_state))
        if self.derivative_vars:
{'fraction_non_alphanumeric': 0.09610450198273851, 'fraction_numerical': 0.004665267086540704, 'mean_word_length': 2.9267399267399266, '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': '3770619', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1221, 'n_words': 323}