本文整理汇总了Python中theano.tensor.ceil函数的典型用法代码示例。如果您正苦于以下问题:Python ceil函数的具体用法?Python ceil怎么用?Python ceil使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ceil函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _activation
def _activation(self, Y, L, M, W):
"""Returns the activation for a given input.
Derived from the generative model formulation of hierarchical
Poisson mixtures, the formular for the activation in the network
reads as follows:
I_c =
\sum_d \log(W_{cd})y_d + \log(M_{lc}) for labeled data
\sum_d \log(W_{cd})y_d + \log(\sum_k M_{kc}) for unlabeled data
s_c = softmax(I_c)
"""
# first: complete inference to find label
# Input integration:
I = T.tensordot(Y,T.log(W),axes=[1,1])
# recurrent term:
vM = M[L]
L_index = T.eq(L,-1).nonzero()
vM = T.set_subtensor(vM[L_index], T.sum(M, axis=0))
# numeric trick to prevent overflow in the exp-function
max_exponent = 86. - T.ceil(T.log(I.shape[1].astype('float32')))
scale = T.switch(
T.gt(T.max(I, axis=1, keepdims=True), max_exponent),
T.max(I, axis=1, keepdims=True) - max_exponent,
0.)
# numeric approximation to prevent underflow in the exp-function:
# map too low values of I to a fixed minimum value
min_exponent = -87. + T.ceil(T.log(I.shape[1].astype('float32')))
I = T.switch(
T.lt(I-scale, min_exponent),
scale+min_exponent,
I)
# activation: recurrent softmax with overflow protection
s = vM*T.exp(I-scale)/T.sum(vM*T.exp(I-scale), axis=1, keepdims=True)
return s
示例2: process
def process(self, input, tparams, BNparams):
b, f, h0, w0 = input.shape
result = []
for h, w in self.pymamid:
win_h = T.ceil(h0 / h).astype('int32')
win_w = T.ceil(w0 / w).astype('int32')
str_h = T.floor(h0 / h).astype('int32')
str_w = T.floor(w0 / w).astype('int32')
result.append(dnn_pool(
img=input, ws=(win_h, win_w), mode=self.mode,
stride=(str_h, str_w), pad=(0, 0)).reshape([b, -1]))
return T.concatenate(result, axis=1)
示例3: pool_2d_nxn_regions
def pool_2d_nxn_regions(inputs, output_size, mode='max'):
"""
Performs a pooling operation that results in a fixed size:
output_size x output_size.
Used by SpatialPyramidPoolingLayer. Refer to appendix A in [1]
Parameters
----------
inputs : a tensor with 4 dimensions (N x C x H x W)
output_size: integer
The output size of the pooling operation
mode : string
Pooling mode, one of 'max', 'average_inc_pad', 'average_exc_pad'
Defaults to 'max'.
Returns a list of tensors, for each output bin.
The list contains output_size*output_size elements, where
each element is a 3D tensor (N x C x 1)
References
----------
.. [1] He, Kaiming et al (2015):
Spatial Pyramid Pooling in Deep Convolutional Networks
for Visual Recognition.
http://arxiv.org/pdf/1406.4729.pdf.
"""
if mode == 'max':
pooling_op = T.max
elif mode in ['average_inc_pad', 'average_exc_pad']:
pooling_op = T.mean
else:
msg = "Mode must be either 'max', 'average_inc_pad' or "
msg += "'average_exc_pad'. Got '{0}'"
raise ValueError(msg.format(mode))
h, w = inputs.shape[2:]
result = []
n = float(output_size)
for row in range(output_size):
for col in range(output_size):
start_h = T.floor(row / n * h).astype('int32')
end_h = T.ceil((row + 1) / n * h).astype('int32')
start_w = T.floor(col / n * w).astype('int32')
end_w = T.ceil((col + 1) / n * w).astype('int32')
pooling_region = inputs[:, :, start_h:end_h, start_w:end_w]
this_result = pooling_op(pooling_region, axis=(2, 3))
result.append(this_result.dimshuffle(0, 1, 'x'))
return result
示例4: compute_hard_windows
def compute_hard_windows(self, image_shape, location, scale):
# find topleft(front) and bottomright(back) corners for each patch
a = location - 0.5 * (T.cast(self.patch_shape, theano.config.floatX) / scale)
b = location + 0.5 * (T.cast(self.patch_shape, theano.config.floatX) / scale)
# grow by three patch pixels
a -= self.kernel.k_sigma_radius(self.cutoff, scale)
b += self.kernel.k_sigma_radius(self.cutoff, scale)
# clip to fit inside image and have nonempty window
a = T.clip(a, 0, image_shape - 1)
b = T.clip(b, a + 1, image_shape)
if self.batched_window:
# take the bounding box of all windows; now the slices
# will have the same length for each sample and scan can
# be avoided. comes at the cost of typically selecting
# more of the input.
a = a.min(axis=0, keepdims=True)
b = b.max(axis=0, keepdims=True)
# make integer
a = T.cast(T.floor(a), 'int16')
b = T.cast(T.ceil(b), 'int16')
return a, b
示例5: __init__
def __init__(self, input_ngram, input_sm, vocab_size, emb_dim, num_section, linear_W_emb=None, fix_emb=False, nonlinear=None, activation=None):
global rng
global init_range
if linear_W_emb is None:
# random initialize
linear_W_emb = np.asarray(rng.uniform(
low=-init_range, high=init_range, size=(vocab_size, emb_dim)), dtype=theano.config.floatX)
else:
# use the given model parameter
given_vocab_size, given_emb_dim = linear_W_emb.shape
assert(given_vocab_size == vocab_size and given_emb_dim == emb_dim)
# shared variables
self.W_emb = theano.shared(value=linear_W_emb, name='W_emb')
# stack vectors
input_ngram = T.cast(input_ngram, 'int32')
input_sm = T.cast(input_sm, 'int32')
# output is a matrix where each row correponds to a context_size embedding vector, and row number equals to batch size
# output dimensions: batch_size * ((context_size + 1) * emb_dim)
output_local = self.W_emb[input_ngram[:, :-1].flatten()].reshape(
(input_ngram.shape[0], emb_dim * (input_ngram.shape[1] - 1))) # self.W_emb.shape[1]
sentence_lengths = input_sm[:,0]
sentence_matrix = input_sm[:,1:]
sentence_num = sentence_matrix.shape[0]
global_length = sentence_matrix.shape[1]
section_length = T.cast(T.ceil(global_length / float(num_section)), 'int32')
# For the first section
sentence_embeddings = T.mean(self.W_emb[sentence_matrix[:, :section_length].flatten()].reshape(
(sentence_num, section_length, emb_dim)), axis=1)
# For the rest sections
for i in xrange(1, num_section):
current_section = T.mean(self.W_emb[sentence_matrix[:, i*section_length:(i+1)*section_length].flatten()].reshape(
(sentence_num, section_length, emb_dim)), axis=1)
sentence_embeddings = T.concatenate([sentence_embeddings, current_section], axis=1)
# get the sentence index for each ngram vector, and transform it to 0-based
sentence_indeces = input_ngram[:,-1]
base_index = sentence_indeces[0]
sentence_indeces = sentence_indeces - base_index
# the last column of output should be a weighted sum of the sentence
# vectors
output_global = sentence_embeddings[sentence_indeces.flatten()].reshape((sentence_indeces.shape[0], emb_dim * num_section))
# handle non-linear layer
if nonlinear is None or activation is None:
self.output = T.concatenate([output_local, output_global], axis=1)
# params is the word embedding matrix
self.params = [self.W_emb] if not fix_emb else []
else:
self.non_linear_params, non_linear_output_global = addNonlinearLayer(output_global, emb_dim * num_section, nonlinear, activation)
self.output = T.concatenate([output_local, non_linear_output_global], axis=1)
self.params = [self.W_emb] + self.non_linear_params if not fix_emb else self.non_linear_params
示例6: spp_max_pool_axis_kwargs
def spp_max_pool_axis_kwargs(in_shape, out_shape):
symbolic = (treeano.utils.is_variable(in_shape)
or treeano.utils.is_variable(out_shape))
# maxpool requires static shape
assert not symbolic
if symbolic:
int_ceil = lambda x: T.ceil(x).astype("int32")
else:
int_ceil = lambda x: int(np.ceil(x))
# eg. if input is 5 and output is 2, each pool size should be 3
pool_size = int_ceil(in_shape / out_shape)
# stride should equal pool_size, since we want non-overlapping regions
stride = pool_size
# pad as much as possible, since ignore_border=True
padding = int_ceil((pool_size * out_shape - in_shape) / 2)
if not symbolic:
assert padding < pool_size
return dict(
ds=pool_size,
st=stride,
padding=padding,
)
示例7: encode
def encode(self, state_below):
"""
:development:
(1) may need to prepend encoding_length * padding array to the state_below to produce the same length sequence as state_below
(2) can return an offset encoding by only returing certain indices of the encoding (though this is pretty wasteful)
:type state_below: 2d tensor
:param state_below: the enitre sequence of states from the layer below the current one
:type rval: 2d tensor
:param rval: an encoding of the state_below (the entire sequence of state) to be passed to the above layer
"""
total_sequence_length = T.cast(state_below.shape[0], theano.config.floatX)
self.n_encodings = T.cast(T.ceil(total_sequence_length / self.encoding_length), 'int32')
self.n_padding_timesteps = T.cast(self.n_encodings * self.encoding_length - total_sequence_length, 'int32')
zeros = T.alloc(np.cast[theano.config.floatX](0), self.n_padding_timesteps, self.n_vis)
state_below = T.concatenate((zeros, state_below))
Wxh = self.Wxh
bxh = self.bxh
Whhe = self.Whhe
state_below = state_below.reshape((self.encoding_length, self.n_encodings, self.n_vis))
state_below = T.dot(state_below, Wxh) + bxh
# a single output will be n_encoding rows with n_hid features each
encoding_0 = T.alloc(np.cast[theano.config.floatX](0), self.n_encodings, self.n_hid)
encodings, updates = scan(fn=self.encode_step, sequences=[state_below], outputs_info=[encoding_0], non_sequences=[Whhe])
# encodings is a 3d vector (encoding_length, n_encodings, n_hid)
# returns encodings[-1] in 2d vector shape = (n_encodings, n_hid)
return encodings[-1]
示例8: get_pseudo_likelihood_cost
def get_pseudo_likelihood_cost(self, updates):
"""Stochastic approximation to the pseudo-likelihood"""
# index of bit i in expression p(x_i | x_{\i})
bit_i_idx = theano.shared(value=0, name='bit_i_idx')
# binarize the input image by rounding to nearest integer
xi = T.round(self.input)
# calculate free energy for the given bit configuration
fe_xi = self.free_energy(xi, self.scaling)
# flip bit x_i of matrix xi and preserve all other bits x_{\i}
# Equivalent to xi[:,bit_i_idx] = 1-xi[:, bit_i_idx], but assigns
# the result to xi_flip, instead of working in place on xi.
xi_flip = T.set_subtensor(xi[:, bit_i_idx], 1 - T.ceil(xi[:, bit_i_idx] / (xi[:, bit_i_idx] + 1)))
# calculate free energy with bit flipped
fe_xi_flip = self.free_energy(xi_flip, self.scaling)
# equivalent to e^(-FE(x_i)) / (e^(-FE(x_i)) + e^(-FE(x_{\i})))
cost = T.mean(self.n_visible * T.log(T.nnet.sigmoid(fe_xi_flip - fe_xi)))
# increment bit_i_idx % number as part of updates
updates[bit_i_idx] = (bit_i_idx + 1) % self.n_visible
return cost
示例9: get_output_for
def get_output_for(self, input, **kwargs):
p = self.p
k = self.k
nbatches = input.shape[0]
x_len = self.x_len
# x_len = 30
# x = input.reshape((nbatches, x_len))
x = input.reshape((nbatches, x_len))
p_floor = T.floor(p)
p_ceil = T.ceil(p)
# Deltas
p_delta = p - p_floor
ep_delta = T.exp(k*-p_delta)
p2_delta = 1 - p_delta
ep2_delta = T.exp(k*-p2_delta)
p0_delta = 1 + p_delta
ep0_delta = T.exp(k*-p0_delta)
ep_sum = ep_delta + ep2_delta + ep0_delta
perm1 = x[:, (T.cast(p_floor, 'int32'))%x_len]
perm2 = x[:, (T.cast(p_ceil, 'int32')+1)%x_len]
perm0 = x[:, (T.cast(p_floor, 'int32')-1)%x_len]
perm1_factor = ep_delta * perm1
perm2_factor = ep2_delta * perm2
perm3_factor = ep0_delta * perm0
res = (perm1_factor + perm2_factor + perm3_factor) / ep_sum
return res.reshape(input.shape)
示例10: get_output_for
def get_output_for( self, inputs ,**kwargs ):
# For each ROI R = [batch_index x1 y1 x2 y2]: max pool over R
input = inputs[0]
boxes = inputs[1]
batch = T.shape (input)[0]
channels = T.shape (input)[1]
height = T.shape( input )[2]
width = T.shape( input )[3]
num_boxes = T.shape(boxes)[0]
output = T.zeros((batch * num_boxes , channels, self.num_features))
for idbb,bb in enumerate(range(num_boxes)):
batch_ind = bb[0]
pool_list = []
#for pool_dim in self.pool_dims:
start_w = T.clip(T.floor(bb[1] * self.sp_scale),0,width)
start_h = T.clip(T.floor(bb[2] * self.sp_scale),0,heigth)
end_w = T.clip(T.ceil(bb[3] * self.sp_scale),0,width)
end_h = T.clip(T.ceil(bb[4] * self.sp_scale),0,height)
w = T.max(end_w - start_w +1,1)
h = T.amx(end_h - start_h +1,1)
start_samples_y,start_sample_x = T.floor(_meshgrid(start_h,end_h,pool_dims+1,start_w,end_w,pool_dims+1))
end_samples_y,end_sample_x = T.ceil(_meshgrid(start_h,end_h,pool_dims+1,start_w,end_w,pool_dims+1))
input[batch_ind,:,np.floor(py):np.ceil(samples_y[idy+1]),np.floor(px):np.ceil(samples_x[idx+1])]
#T.max()
#for idx,px in enumerate(samples_x[:-1]):
# for idy,py in enumerate(samples_y[:-1]):
# (pool.dnn_pool( input[batch_ind,:,np.floor(py):np.ceil(samples_y[idy+1]),np.floor(px):np.ceil(samples_x[idx+1])],(0,0),(None,None),'max', (0,0) )).flatten(2)
#sz_w = ( w - 1 ) // pool_dim
#sz_h = ( h - 1 ) // pool_dim
#str_h = w // pool_dim
#str_w = h // pool_dim
#pool = dnn.dnn_pool( input[bb[0],:,start_h:end_h+1,start_w:end_w+1], (sz_h,sz_w), (str_h,str_w), 'max', (0,0) ).flatten(2)
pool_list.append( pool )
output[idbb] = T.transpose(T.concatenate( pool_list, axis=1 )) #not efficient but for the moment is ok!
#if everything is correct this vector should be ordered as in fast RCNN
return output
示例11: compileActivation
def compileActivation(self, net, layerNum):
variable = net.x if layerNum == 0 else net.varArrayA[layerNum - 1]
#Calc shapes for reshape function on-the-fly. Assume we have square images as input.
sX = T.cast(T.sqrt(T.shape(variable)[0] / self.kernel_shape[1]), 'int16')
#Converts input from 2 to 4 dimensions
Xr = T.reshape(variable.T, (T.shape(variable)[1], self.kernel_shape[1], sX, sX))
if self.optimized:
out_size = T.cast(
T.ceil((T.shape(Xr)[-1] - T.shape(net.varWeights[layerNum]['w'])[-1] + 1) / np.float32(self.stride)),
'int32')
conv_op = FilterActs(stride=self.stride)
input_shuffled = Xr.dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_shuffled = net.varWeights[layerNum]['w'].dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_flipped = filters_shuffled[:, ::-1, ::-1, :] # flip rows and columns
contiguous_input = gpu_contiguous(input_shuffled)
contiguous_filters = gpu_contiguous(filters_flipped *
(net.dropOutVectors[layerNum].dimshuffle('x', 0, 1, 'x') if self.dropout else 1.0))
a = conv_op(contiguous_input, contiguous_filters)
a = a[:, :out_size, :out_size, :]
#Add bias
a = a + net.varWeights[layerNum]['b'].dimshuffle(0, 'x', 'x', 'x')
else:
a = T.nnet.conv2d(Xr, net.varWeights[layerNum]['w'] *
(net.dropOutVectors[layerNum].dimshuffle('x', 'x', 0, 1) if self.dropout else 1.0),
border_mode='valid',
subsample=(self.stride, self.stride))
#Add bias
a = a + net.varWeights[layerNum]['b'].dimshuffle('x', 0, 'x', 'x')
if self.pooling:
if self.optimized:
#Pooling
# ds - side of square pool window
# stride - Defines the stride size between successive pooling squares.
# Setting this parameter smaller than sizeX produces overlapping pools.
# Setting it equal to sizeX gives the usual, non-overlapping pools. Values greater than sizeX are not allowed.
pool_op = MaxPool(ds=self.pooling_shape, stride=self.pooling_shape)
contiguous_input = gpu_contiguous(a)
a = pool_op(contiguous_input)
a = a.dimshuffle(3, 0, 1, 2) # c01b to bc01
else:
#a = downsample.max_pool_2d(a, (self.pooling_shape, self.pooling_shape), ignore_border=False)
a = pool.max_pool2D(a, (self.pooling_shape, self.pooling_shape), ignore_border=False)
else:
if self.optimized:
a = a.dimshuffle(3, 0, 1, 2) # c01b to bc01
a = T.flatten(a, outdim=2).T
#Sigmoid
a = self.activation(a, self.pool_size)
net.varArrayA.append(a)
示例12: _build_expression
def _build_expression(self, input_expression=None):
if self.pool_type not in ['max', 'avg']:
raise NotImplementedError(
'Pooling only implemented for max and avg')
if input_expression is None:
self.input_ = T.tensor4(dtype=self.input_dtype)
else:
self.input_ = input_expression
# Replicating caffe style pooling means zero padding
# then strided pooling with ignore_border=True
if self.padding in [0, (0, 0)]:
padded_input = self.input_
else:
zero_padder = ZeroPad(padding=self.padding)
zero_padder._build_expression(self.input_)
padded_input = zero_padder.expression_
if self.pool_type == 'max':
pooled = fancy_max_pool(padded_input,
self.pool_shape, self.pool_stride,
ignore_border=False)
elif self.pool_type == 'avg':
# self.pool_shape needs to be a tuple
avg_kernel = T.cast(T.ones((1, 1) + self.pool_shape,
dtype=self.input_.dtype
) / np.prod(self.pool_shape),
self.input_.dtype)
n_imgs = self.input_.shape[0]
n_channels = self.input_.shape[1]
conv_output = T.nnet.conv2d(
padded_input.reshape((n_imgs * n_channels, 1,
padded_input.shape[2],
padded_input.shape[3])),
avg_kernel, subsample=self.pool_stride)
pooled = conv_output.reshape((n_imgs, n_channels,
conv_output.shape[2],
conv_output.shape[3]))
# A caffe quirk: The output shape is (for width, analogous for h:)
# ceil((w + 2 * pad_w - kernel_w) / stride_w) + 1, instead of floor
# With floor, ignore_border=True would have yielded the exact result
# With ceil, sometimes we need an extra column and/or line. So we do
# ignore_border=False and then crop to the right shape. Since the
# shape is dynamic we need to first calculate it:
# padding gotta be a tuple too
pad = T.constant(self.padding)
# pad = T.constant(zero_padder.padding_)
# supposing here that self.pool_shape is a tuple. Should check
pool_shape = T.constant(self.pool_shape)
# stride hopefully a tuple, too
pool_stride = T.constant(self.pool_stride, dtype='float64')
float_shape = (self.input_.shape[2:4] + 2 * pad
- pool_shape) / pool_stride + 1
output_shape = T.cast(T.ceil(float_shape), dtype='int64')
self.expression_ = pooled[:, :, 0:output_shape[0],
0:output_shape[1]]
示例13: gaussian_kernel_default_radius
def gaussian_kernel_default_radius(sigma, window_radius=None):
if window_radius is None:
radius = T.cast(T.max(T.ceil(3*sigma)), 'int32')
if type(sigma) in (float, int):
return int(radius.eval())
else:
return radius
else:
return window_radius
示例14: get_hidden_values
def get_hidden_values(self, input, batch_size):
self.indices_high = T.ceil(self.indices).astype('int8')
self.indices_low = T.floor(self.indices).astype('int8')
self.factors_high = self.W[self.indices_high]
self.factors_low = self.W[self.indices_low]
self.factors = (self.factors_high - self.factors_low) * (self.indices - self.indices_low) / \
(self.indices_high - self.indices_low + 1E-5) + self.factors_low
self.output = T.sum(self.x * T.transpose(self.factors).dimshuffle(0, 'x', 1), axis=2) / \
(self.length + 1.0).dimshuffle(0, 'x')
示例15: _ppf
def _ppf(self, p):
"""
The percentile point function (the inverse of the cumulative
distribution function) of the discrete Weibull distribution.
"""
q = self.q
beta = self.beta
return (tt.ceil(tt.power(tt.log(1 - p) / tt.log(q), 1. / beta)) - 1).astype('int64')