Python theano.tensor.erf() Examples

The following are 20 code examples of theano.tensor.erf(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module theano.tensor , or try the search function .
Example #1
Source File: nn_heart.py    From kaggle-heart with MIT License 5 votes vote down vote up
def cdf(sample, mu=0, sigma=1, eps=1e-6):
    div = T.sqrt(2) * sigma
    erf_arg = (sample - mu) / div
    return .5 * (1 + T.erf(erf_arg)) 
Example #2
Source File: utils.py    From kaggle-heart with MIT License 5 votes vote down vote up
def numpy_mu_sigma_erf(mu_erf, sigma_erf, eps=1e-7):
    batch_size = mu_erf.shape[0]
    x_axis = np.tile(np.arange(0, 600, dtype='float32'), (batch_size, 1))
    mu_erf = np.tile(mu_erf[:,None], (1, 600))
    sigma_erf = np.tile(sigma_erf[:,None], (1, 600))
    sigma_erf += eps

    x = (x_axis - mu_erf) / (sigma_erf * np.sqrt(2))
    return (erf(x) + 1)/2 
Example #3
Source File: utils.py    From kaggle-heart with MIT License 5 votes vote down vote up
def theano_mu_sigma_erf(mu_erf, sigma_erf, eps=1e-7):
    x_axis = theano.shared(np.arange(0, 600, dtype='float32')).dimshuffle('x',0)
    if sigma_erf.ndim==0:
        sigma_erf = T.clip(sigma_erf.dimshuffle('x','x'), eps, 1)
    elif sigma_erf.ndim==1:
        sigma_erf = T.clip(sigma_erf.dimshuffle(0,'x'), eps, 1)
    x = (x_axis - mu_erf.dimshuffle(0,'x')) / (sigma_erf * np.sqrt(2).astype('float32'))
    return (T.erf(x) + 1)/2 
Example #4
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, inputs, **kwargs):
        """

        :param inputs[0]: (batch, 600)
        :param inputs[1]: (batch, 1)
        :return:
        """
        result = (T.erf( T.erfinv( T.clip(inputs[1].dimshuffle(0,'x',1)*2-1, -1+3e-8, 1-3e-8) ) * inputs[0].dimshuffle(0,1,'x') )+1)/2
        return result[:,0,:] 
Example #5
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        x_axis = theano.shared(np.arange(0, 600, dtype='float32')).dimshuffle('x', 0)
        x = (x_axis - input[:,0].dimshuffle(0, 'x')) / (self.sigma * np.sqrt(2).astype('float32'))
        return (T.erf(x) + 1)/2 
Example #6
Source File: layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        eps = 1e-7
        x_axis = theano.shared(np.arange(0, 600, dtype='float32')).dimshuffle('x',0)
        # This needs to be clipped to avoid NaN's!
        sigma = T.exp(T.clip(input[:,1].dimshuffle(0,'x'), -10, 10))
        #theano_printer.print_me_this("sigma", sigma)
        x = (x_axis - input[:,0].dimshuffle(0,'x')) / (sigma * np.sqrt(2).astype('float32'))
        return (T.erf(x) + 1)/2 
Example #7
Source File: volume_estimation_layers.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        if input.ndim > 3:
            # input: (batch, time, axis, verti, horiz)
            # needs: (batch, time, pixels)
            input = input.flatten(ndim=3)

        eps=1e-7
        clipped_input = T.clip(input, eps, 1-eps)
        mu = T.sum(clipped_input, axis=2).dimshuffle(0,1,'x')

        sigma = T.sqrt(T.sum(clipped_input * (1-clipped_input), axis=2).dimshuffle(0,1,'x') + eps)
        x_axis = theano.shared(np.arange(0, 600, dtype='float32')).dimshuffle('x','x',0)
        x = (x_axis - mu) / sigma
        return (T.erf(x) + 1)/2 
Example #8
Source File: nn_heart.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        mu = input[0]
        sigma = input[1]

        if self.sigma_logscale:
            sigma = T.exp(sigma)
        if self.mu_logscale:
            mu = T.exp(mu)

        x_range = T.arange(0, 600).dimshuffle('x', 0)
        mu = T.repeat(mu, 600, axis=1)
        sigma = T.repeat(sigma, 600, axis=1)
        x = (x_range - mu) / (sigma * T.sqrt(2.) + 1e-16)
        cdf = (T.erf(x) + 1.) / 2.
        return cdf 
Example #9
Source File: nn_heart.py    From kaggle-heart with MIT License 5 votes vote down vote up
def get_output_for(self, input, **kwargs):
        mu = input[0]
        sigma = input[1]
        w = input[2]
        if self.log:
            sigma = T.exp(sigma)

        x_range = T.arange(0, 600).dimshuffle('x', 0, 'x')
        mu = mu.dimshuffle(0, 'x', 1)
        sigma = sigma.dimshuffle(0, 'x', 1)
        x = (x_range - mu) / (sigma * T.sqrt(2.) + 1e-16)
        cdf = (T.erf(x) + 1.) / 2.  # (bs, 600, n_mix)
        cdf = T.sum(cdf * w.dimshuffle(0, 'x', 1), axis=-1)
        return cdf 
Example #10
Source File: __init__.py    From python-mle with MIT License 5 votes vote down vote up
def __init__(self, x, mu, sigma, *args, **kwargs):
        super(Normal, self).__init__(*args, **kwargs)
        self._logp = bound(
            -(x - mu)**2 / (2 * sigma**2) + T.log(1 / T.sqrt(sigma**2 * 2*np.pi)),
            sigma > 0
        )
        self._cdf = 0.5 * (1 + T.erf((x - mu)/(sigma*T.sqrt(2))))
        self._add_expr('x', x)
        self._add_expr('mu', mu)
        self._add_expr('sigma', sigma) 
Example #11
Source File: sparse_gp_theano_internal.py    From sdvae with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #12
Source File: sparse_gp_theano_internal.py    From sdvae with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #13
Source File: sparse_gp_theano_internal.py    From sdvae with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #14
Source File: sparse_gp_theano_internal.py    From sdvae with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #15
Source File: nonlinearities.py    From kusanagi with MIT License 5 votes vote down vote up
def gelu2(x):
    '''
        similar to silu
    '''
    return x*(tt.erf(x) + 1) 
Example #16
Source File: normal.py    From carl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, mu=0.0, sigma=1.0):
        """Constructor.

        Parameters
        ----------
        * `mu` [float]:
            The distribution mean.

        * `sigma` [float]:
            The distribution standard deviation.
        """
        super(Normal, self).__init__(mu=mu, sigma=sigma)

        # pdf
        self.pdf_ = (
            (1. / np.sqrt(2. * np.pi)) / self.sigma *
            T.exp(-(self.X - self.mu) ** 2 / (2. * self.sigma ** 2))).ravel()
        self._make(self.pdf_, "pdf")

        # -log pdf
        self.nll_ = bound(
            T.log(self.sigma) + T.log(np.sqrt(2. * np.pi)) +
            (self.X - self.mu) ** 2 / (2. * self.sigma ** 2),
            np.inf,
            self.sigma > 0.).ravel()
        self._make(self.nll_, "nll")

        # cdf
        self.cdf_ = 0.5 * (1. + T.erf((self.X - self.mu) /
                                      (self.sigma * np.sqrt(2.)))).ravel()
        self._make(self.cdf_, "cdf")

        # ppf
        self.ppf_ = (self.mu +
                     np.sqrt(2.) * self.sigma * T.erfinv(2. * self.p - 1.))
        self._make(self.ppf_, "ppf", args=[self.p]) 
Example #17
Source File: rand.py    From iaf with MIT License 5 votes vote down vote up
def discretized_gaussian(mean, logvar, binsize, sample=None):
    scale = T.exp(.5*logvar)
    if sample is None:
        _y = G.rng_curand.normal(size=mean.shape)
        sample = mean + scale * _y #sample from the actual logistic
        sample = T.floor(sample/binsize)*binsize #discretize the sample
    _sample = (T.floor(sample/binsize)*binsize - mean)/scale
    def _erf(x):
        return T.erf(x/T.sqrt(2.))
    logp = T.log( _erf(_sample + binsize/scale) - _erf(_sample) + 1e-7) + T.log(.5)
    logp = logp.flatten(2).sum(axis=1)
    #raise Exception()
    entr = (.5 * (T.log(2 * math.pi) + 1 + logvar)).flatten(2).sum(axis=1)
    return RandomVariable(sample, logp, entr, mean=mean, logvar=logvar) 
Example #18
Source File: sparse_gp_theano_internal.py    From icml18-jtnn with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #19
Source File: sparse_gp_theano_internal.py    From D-VAE with MIT License 5 votes vote down vote up
def n_cdf(x):
    return 0.5 * (1.0 + T.erf(x / T.sqrt(2.0))) 
Example #20
Source File: layers.py    From kaggle-heart with MIT License 4 votes vote down vote up
def get_output_for(self, inputs, **kwargs):
        eps = 1e-7
        mu_a, sigma_a, mu_b, sigma_b = inputs

        # Rescale input
        if self.trainable_scale:
            mu_a = mu_a * T.exp(self.W_mu[0]) + T.exp(self.b_mu[0])
            sigma_a = sigma_a * T.exp(self.W_sigma[0]) + T.exp(self.b_sigma[0])

            mu_b = mu_b * T.exp(self.W_mu[0]) + T.exp(self.b_mu[0])
            sigma_b = sigma_b * T.exp(self.W_sigma[0]) + T.exp(self.b_sigma[0])

        # Compute the distance between slices
        h = 0.1  # mm to cm

        # Compute mu for each slice pair
        mu_volumes = mu_a * mu_b * h
        #  (batch, time, height)

        # Compute sigma for each slice pair
        var_a = sigma_a ** 2
        var_b = sigma_b ** 2
        var_volumes = (var_a * var_b + var_a * mu_b ** 2 + var_b * mu_a ** 2) * h ** 2
        #  (batch, time, height)

        # Compute mu and sigma per patient

        mu_volume_patient = np.pi / 4. * T.sum(mu_volumes, axis=2)
        #  (batch, time)

        sigma_volume_patient = np.pi / 4. * T.sqrt(T.clip(T.sum(var_volumes, axis=2), eps, utils.maxfloat))
        sigma_volume_patient = T.clip(sigma_volume_patient, eps, utils.maxfloat)
        #  (batch, time)

        x_axis = theano.shared(np.arange(0, 600, dtype='float32')).dimshuffle('x', 'x', 0)
        x = (x_axis - mu_volume_patient.dimshuffle(0, 1, 'x')) / (sigma_volume_patient.dimshuffle(0, 1, 'x') )
        prediction_matrix = (T.erf(x) + 1)/2

        #  (batch, time, 600)

        # max because distribution of smaller one will lie higher
        l_systole = T.max(prediction_matrix, axis=1)
        l_diastole = T.min(prediction_matrix, axis=1)
        #  (batch, 600)

        return T.concatenate([l_systole.dimshuffle(0, 1, 'x'),
                              l_diastole.dimshuffle(0, 1, 'x')], axis=2)