Python theano.tensor 模块,floor() 实例源码

我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用theano.tensor.floor()

项目:Buffe    作者:bentzinir    | 项目源码 | 文件源码
def create_learning_rate_func(solver_params):
    base = tt.fscalar('base')
    gamma = tt.fscalar('gamma')
    power = tt.fscalar('power')
    itrvl = tt.fscalar('itrvl')
    iter = tt.scalar('iter')

    if solver_params['lr_type']=='inv':
        lr_ = base * tt.pow(1 + gamma * iter, -power)

        lr = t.function(
            inputs=[iter, t.Param(base, default=solver_params['base']), t.Param(gamma, default=solver_params['gamma']), t.Param(power, default=solver_params['power'])],
            outputs=lr_)

    elif solver_params['lr_type']=='fixed':
        lr_ = base

        lr = t.function(
            inputs=[iter, t.Param(base, default=solver_params['base'])],
            outputs=lr_,
            on_unused_input='ignore')

    elif solver_params['lr_type']=='episodic':
        lr_ = base / (tt.floor(iter/itrvl) + 1)

        lr = t.function(
            inputs=[iter, t.Param(base, default=solver_params['base']), t.Param(itrvl, default=solver_params['interval'])],
            outputs=lr_,
            on_unused_input='ignore')
    return lr
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def inv(self, output):

        output = output.dimshuffle(0,1,2,'x').repeat(self.pool_shape[0], axis=3)

        if self.depooler == 'random':
            mask = self.theano_rng.uniform(size=output.shape, dtype=theano.config.floatX) 
            mask = T.floor(mask / mask.max(axis=3).dimshuffle(0,1,2,'x'))
            output = mask * output
        elif self.depooler == 'first':
            mask_np = np.zeros(self.pool_shape, dtype=theano.config.floatX)
            mask_np[0] = 1.0
            mask = theano.shared(mask_np, borrow=True).dimshuffle('x','x','x',0)
            output = mask * output
        else:
            output = self.depooler(output, axis=3)

        return output.reshape(self.input_shape)
项目:iaf    作者:openai    | 项目源码 | 文件源码
def discretized_laplace(mean, logscale, binsize, sample=None):
    scale = .5*T.exp(logscale)
    if sample is None:
        u = G.rng_curand.uniform(size=mean.shape) - .5
        sample = mean - scale * T.sgn(u) * T.log(1-2*abs(u))
        sample = T.floor(sample/binsize)*binsize #discretize the sample

    d = .5*binsize
    def cdf(x):
        z = x-mean
        return .5 + .5 * T.sgn(z) * (1.-T.exp(-abs(z)/scale))
    def logmass1(x):
        # General method for probability mass, but numerically unstable for large |x-mean|/scale
        return T.log(cdf(x+d) - cdf(x-d) + 1e-7)
    def logmass2(x):
        # Only valid for |x-mean| >= d
        return -abs(x-mean)/scale + T.log(T.exp(d/scale)-T.exp(-d/scale)) - np.log(2.).astype(G.floatX) 
    def logmass_stable(x):
        switch = (abs(x-mean) < d)
        return switch * logmass1(x) + (1-switch) * logmass2(x)

    logp = logmass_stable(sample).flatten(2).sum(axis=1)
    entr = None #(1 + logscale).flatten(2).sum(axis=1)
    return RandomVariable(sample, logp, entr, mean=mean, scale=scale)
项目:sesame-paste-noodle    作者:aissehust    | 项目源码 | 文件源码
def forward(self, inputtensor):
        X = inputtensor[0]
        ret = abs((X/2 - T.floor(X/2))*2-1)*2-1
        return (ret,)
项目:keras-tf-Super-Resolution    作者:olgaliak    | 项目源码 | 文件源码
def depth_to_scale(x, scale, output_shape, dim_ordering=K.image_dim_ordering(), name=None):
    ''' Uses phase shift algorithm [1] to convert channels/depth for spacial resolution '''

    import theano.tensor as T

    scale = int(scale)

    if dim_ordering == "tf":
        x = x.transpose((0, 3, 1, 2))
        out_row, out_col, out_channels = output_shape
    else:
        out_channels, out_row, out_col = output_shape

    b, k, r, c = x.shape
    out_b, out_k, out_r, out_c = b, k // (scale * scale), r * scale, c * scale

    out = K.reshape(x, (out_b, out_k, out_r, out_c))

    for channel in range(out_channels):
        channel += 1

        for i in range(out_row):
            for j in range(out_col):
                a = i // scale #T.floor(i / scale).astype('int32')
                b = j // scale #T.floor(j / scale).astype('int32')
                d = channel * scale * (j % scale) + channel * (i % scale)

                T.set_subtensor(out[:, channel - 1, i, j], x[:, d, a, b], inplace=True)

    if dim_ordering == 'tf':
        out = out.transpose((0, 2, 3, 1))

    return out
项目:QuantizedRNN    作者:ottj    | 项目源码 | 文件源码
def quantize_weights(W,srng=None,bitlimit=None,deterministic=False):
    """
    Exponential quantization
    :param W: Weights
    :param srng: random number generator
    :param bitlimit: limit values to be in power of 2 range, e.g. for values in 2^-22 to 2^9 set it to [-22, 9]
    :param deterministic: deterministic rounding
    :return: quantized weights
    """
    bitlimit=[-22, 9] #hardcoded for experiments
    if srng is None:
        rng = np.random.RandomState(666)
        srng = theano.sandbox.rng_mrg.MRG_RandomStreams(rng.randint(999999))

    if bitlimit:
        index_low = T.clip(
            T.switch(W > 0., T.floor(T.log2(W)), T.floor(T.log2(-W))),
            bitlimit[0], bitlimit[1])
    else:
        index_low = T.switch(
            W > 0., T.floor(T.log2(W)), T.floor(T.log2(-W)))
    sign = T.switch(W > 0., 1., -1.)
    p_up = sign * W / 2 ** (index_low) - 1  # percentage of upper index.
    if deterministic:
        index_deterministic = index_low + T.switch(p_up > 0.5, 1, 0)
        quantized_W = sign * 2 ** index_deterministic
    else:
        index_random = index_low + srng.binomial(
            n=1, p=p_up, size=T.shape(W), dtype=theano.config.floatX)
        quantized_W = sign * 2 ** index_random
    return quantized_W
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def inv(self, output):

        output = (output.dimshuffle(0,1,2,'x',3,'x')
            .repeat(self.pool_shape[1], axis=5)
            .repeat(self.pool_shape[0], axis=3))

        if self.depooler == 'random':
            unpooled = (
                self.input_shape[0], self.input_shape[1], 
                self.input_shape[2]//self.pool_shape[0], self.pool_shape[0],
                self.input_shape[3]//self.pool_shape[1], self.pool_shape[1])

            pooled = (
                self.input_shape[0], self.input_shape[1], 
                self.input_shape[2]//self.pool_shape[0], 1,
                self.input_shape[3]//self.pool_shape[1], 1)

            output_mask = self.theano_rng.uniform(size=unpooled, dtype=theano.config.floatX)
            output_mask = output_mask / output_mask.max(axis=5).max(axis=3).dimshuffle(0,1,2,'x',3,'x')
            output_mask = T.floor(output_mask)

            return (output_mask * output).reshape(self.input_shape)
        else:
            output = self.depooler(output, axis=5)
            output = self.depooler(output, axis=3)
            return output
项目:deep-motion-analysis    作者:Brimborough    | 项目源码 | 文件源码
def inv(self, output):

        output = (output.dimshuffle(0,1,2,'x',3,'x',4,'x')
            .repeat(self.pool_shape[0], axis=7),
            .repeat(self.pool_shape[1], axis=5),
            .repeat(self.pool_shape[2], axis=3))

        if self.depooler == 'random':
            unpooled = (
                self.input_shape[0], self.input_shape[1], 
                self.input_shape[2]//self.pool_shape[0], self.pool_shape[0],
                self.input_shape[3]//self.pool_shape[1], self.pool_shape[1],
                self.input_shape[4]//self.pool_shape[2], self.pool_shape[2])

            pooled = (
                self.input_shape[0], self.input_shape[1], 
                self.input_shape[2]//self.pool_shape[0], 1,
                self.input_shape[3]//self.pool_shape[1], 1,
                self.input_shape[4]//self.pool_shape[2], 1)

            output_mask = self.theano_rng.uniform(size=unpooled, dtype=theano.config.floatX)
            output_mask = output_mask / output_mask.max(axis=7).max(axis=5).max(axis=3).dimshuffle(0,1,2,'x',3,'x',4,'x')
            output_mask = T.floor(output_mask)

            return (output_mask * output).reshape(self.input_shape)
        else:
            output = self.depooler(output, axis=3)
项目:Theano-Deep-learning    作者:GeekLiB    | 项目源码 | 文件源码
def floor(x):
    """
    Elemwise floor of `x`.

    """
    # see decorator for function body
项目:iaf    作者:openai    | 项目源码 | 文件源码
def discretized_logistic(mean, logscale, binsize, sample=None):
    scale = T.exp(logscale)
    if sample is None:
        u = G.rng_curand.uniform(size=mean.shape)
        _y = T.log(-u/(u-1)) #inverse CDF of the logistic
        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
    logps = T.log( T.nnet.sigmoid(_sample + binsize/scale) - T.nnet.sigmoid(_sample) + 1e-7)
    logp = logps.flatten(2).sum(axis=1)
    #raise Exception()
    entr = logscale.flatten(2)
    entr = entr.sum(axis=1) + 2. * entr.shape[1].astype(G.floatX)
    return RandomVariable(sample, logp, entr, mean=mean, logscale=logscale, logps=logps)
项目:iaf    作者:openai    | 项目源码 | 文件源码
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)
项目:nature_methods_multicut_pipeline    作者:ilastik    | 项目源码 | 文件源码
def inferoutshape(self, inpshape=None, checkinput=False):
        # Parse
        if inpshape is None:
            inpshape = self.inpshape

        # Return input shape if encoder not active
        if not self.encoderactive:
            return inpshape

        # Process
        if self.inpdim == 4:
            if self.ignoreborder:
                y, x = [int(np.floor((inpshape[sid] + 2 * self.padding[kid] - self.ds[kid] + self.stride[kid]) /
                                     self.stride[kid])) if inpshape[sid] is not None else None
                        for sid, kid in zip([2, 3], [0, 1])]
            else:
                plen = [None, None]
                for sid, kid in zip([2, 3], [0, 1]):
                    if self.stride[kid] >= self.ds[kid]:
                        plen[kid] = int(np.floor((inpshape[sid] + self.stride[kid] - 1) / self.stride[kid])) \
                            if inpshape[sid] is not None else None
                    else:
                        plen[kid] = np.maximum(0, np.floor((inpshape[sid] - self.ds[kid] + self.stride[kid] - 1) /
                                                           self.stride[kid])) if inpshape[sid] is not None else None
                y, x = plen

            fmapsout = inpshape[1]
            batchsize = inpshape[0]

            return [batchsize, fmapsout, y, x]

        elif self.inpdim == 5:
            if self.ignoreborder:
                y, x, z = [int(np.floor((inpshape[sid] + 2 * self.padding[kid] - self.ds[kid] + self.stride[kid]) /
                                        self.stride[kid])) if inpshape[sid] is not None else None
                           for sid, kid in zip([3, 4, 1], [0, 1, 2])]
            else:
                plen = [None, None, None]
                for sid, kid in zip([3, 4, 1], [0, 1, 2]):
                    if self.stride[kid] >= self.ds[kid]:
                        plen[kid] = int(np.floor((inpshape[sid] + self.stride[kid] - 1) / self.stride[kid])) \
                            if inpshape[sid] is not None else None
                    else:
                        plen[kid] = np.maximum(0, np.floor((inpshape[sid] - self.ds[kid] + self.stride[kid] - 1) /
                                                           self.stride[kid])) if inpshape[sid] is not None else None
                y, x, z = plen

            fmapsout = inpshape[2]
            batchsize = inpshape[0]

            return [batchsize, z, fmapsout, y, x]
项目:IQA_BIECON_release    作者:jongyookim    | 项目源码 | 文件源码
def get_updates_sgd_momentum(self, cost, params,
                                 decay_mode=None, decay=0.,
                                 momentum=0.9, nesterov=False,
                                 grad_clip=None, constant_clip=True):
        print(' - SGD: lr = %.2e' % (self.lr.get_value(borrow=True)), end='')
        print(', decay = %.2f' % (decay), end='')
        print(', momentum = %.2f' % (momentum), end='')
        print(', nesterov =', nesterov, end='')
        print(', grad_clip =', grad_clip)

        self.grad_clip = grad_clip
        self.constant_clip = constant_clip
        self.iterations = theano.shared(
            np.asarray(0., dtype=theano.config.floatX), borrow=True)

        # lr = self.lr_float
        lr = self.lr * (1.0 / (1.0 + decay * self.iterations))
        # lr = self.lr * (decay ** T.floor(self.iterations / decay_step))

        updates = [(self.iterations, self.iterations + 1.)]

        # Get gradients and apply clipping
        if self.grad_clip is None:
            grads = T.grad(cost, params)
        else:
            assert self.grad_clip > 0
            if self.constant_clip:
                # Constant clipping using theano.gradient.grad_clip
                clip = self.grad_clip
                grads = T.grad(
                    theano.gradient.grad_clip(cost, -clip, clip),
                    params)
            else:
                # Adaptive clipping
                clip = self.grad_clip / lr
                grads_ = T.grad(cost, params)
                grads = [T.clip(g, -clip, clip) for g in grads_]

        for p, g in zip(params, grads):
            # v_prev = theano.shared(p.get_value(borrow=True) * 0.)
            p_val = p.get_value(borrow=True)
            v_prev = theano.shared(np.zeros(p_val.shape, dtype=p_val.dtype),
                                   broadcastable=p.broadcastable)
            v = momentum * v_prev - lr * g
            updates.append((v_prev, v))

            if nesterov:
                new_p = p + momentum * v - lr * g
            else:
                new_p = p + v

            updates.append((p, new_p))
        return updates
项目:sigma-delta    作者:petered    | 项目源码 | 文件源码
def get_all_signals(self, input_, corruption_type = 'round', rng = None):
        scale = self.get_scale()
        scaled_input = input_*scale
        if corruption_type == 'round':
            epsilon = tt.round(scaled_input) - scaled_input
        elif corruption_type == 'randround':
            rng = get_theano_rng(rng)
            epsilon = tt.where(rng.uniform(scaled_input.shape)>(scaled_input % 1), tt.floor(scaled_input), tt.ceil(scaled_input))-scaled_input
            print 'STOCH ROUNDING'
        elif corruption_type == 'rand':
            rng = get_theano_rng(1234)
            epsilon = rng.uniform(scaled_input.shape)-.5
        else:
            raise Exception('fdsfsd')
        spikes = scaled_input + epsilon
        output = spikes / scale
        signals = dict(
            input=input_,
            scaled_input=scaled_input,
            spikes=spikes,
            epsilon=epsilon,
            output=output,
            )
        return signals

    # def get_all_signals(self, input_):
    #     scale = self.get_scale()
    #
    #
    #
    #     scaled_input = input_*scale
    #
    #
    #
    #     # epsilon = tt.round(scaled_input) - scaled_input
    #
    #     rng = get_theano_rng(1234)
    #     epsilon = rng.uniform(scaled_input.shape)-.5
    #
    #     spikes = scaled_input + epsilon
    #     output = spikes / scale
    #     signals = dict(
    #         input=input_,
    #         scaled_input=scaled_input,
    #         spikes=spikes,
    #         epsilon=epsilon,
    #         output=output,
    #         )
    #     return signals