本文整理汇总了Python中theano.tensor.shape_padleft方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.shape_padleft方法的具体用法?Python tensor.shape_padleft怎么用?Python tensor.shape_padleft使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.shape_padleft方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_output_for
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def get_output_for(self, inputs, *args, **kwargs):
input_, W = inputs
border_mode = 'half' if self.pad == 'same' else self.pad
conved, updates = theano.scan(fn=lambda input_, W:
self.convolution(input_[None, :, :, :],
W,
(1,) + self.input_shape[1:], self.get_W_shape(),
subsample=self.stride,
filter_dilation=self.filter_dilation,
border_mode=border_mode,
filter_flip=self.flip_filters).dimshuffle(*range(4)[1:]),
outputs_info=None,
sequences=[input_, W])
if self.b is None:
activation = conved
elif self.untie_biases:
activation = conved + T.shape_padleft(self.b, 1)
else:
activation = conved + self.b.dimshuffle(('x', 0) + ('x',) * self.n)
return self.nonlinearity(activation)
示例2: sym_logdensity
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def sym_logdensity(self, x):
""" x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
def density_given_previous_a_and_x(x, w, V_alpha, b_alpha, V_mu, b_mu, V_sigma, b_sigma, activations_factor, p_prev, a_prev, x_prev):
a = a_prev + T.dot(T.shape_padright(x_prev, 1), T.shape_padleft(w, 1))
h = self.nonlinearity(a * activations_factor) # BxH
Alpha = T.nnet.softmax(T.dot(h, V_alpha) + T.shape_padleft(b_alpha)) # BxC
Mu = T.dot(h, V_mu) + T.shape_padleft(b_mu) # BxC
Sigma = T.exp((T.dot(h, V_sigma) + T.shape_padleft(b_sigma))) # BxC
p = p_prev + log_sum_exp(T.log(Alpha) - T.log(2 * Sigma) - T.abs_(Mu - T.shape_padright(x, 1)) / Sigma)
return (p, a, x)
# First element is different (it is predicted from the bias only)
a0 = T.zeros_like(T.dot(x.T, self.W)) # BxH
p0 = T.zeros_like(x[0])
x0 = T.ones_like(x[0])
([ps, _as, _xs], updates) = theano.scan(density_given_previous_a_and_x,
sequences=[x, self.W, self.V_alpha, self.b_alpha, self.V_mu, self.b_mu, self.V_sigma, self.b_sigma, self.activation_rescaling],
outputs_info=[p0, a0, x0])
return (ps[-1], updates)
示例3: sym_logdensity
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def sym_logdensity(self, x):
""" x is a matrix of column datapoints (VxB) V = n_visible, B = batch size """
def density_given_previous_a_and_x(x, w, v, b, activations_factor, p_prev, a_prev, x_prev):
a = a_prev + T.dot(T.shape_padright(x_prev, 1), T.shape_padleft(w, 1))
h = self.nonlinearity(a * activations_factor) # BxH
t = T.dot(h, v) + b
p_xi_is_one = T.nnet.sigmoid(t) * constantX(0.9999) + constantX(0.0001 * 0.5) # Make logistic regression more robust by having the sigmoid saturate at 0.00005 and 0.99995
p = p_prev + x * T.log(p_xi_is_one) + (1 - x) * T.log(1 - p_xi_is_one)
return (p, a, x)
# First element is different (it is predicted from the bias only)
a0 = T.zeros_like(T.dot(x.T, self.W)) # BxH
p0 = T.zeros_like(x[0])
x0 = T.ones_like(x[0])
([ps, _, _], updates) = theano.scan(density_given_previous_a_and_x,
sequences=[x, self.W, self.V, self.b, self.activation_rescaling],
outputs_info=[p0, a0, x0])
return (ps[-1], updates)
示例4: log_prob
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def log_prob(self, X, Y):
""" Evaluate the log-probability for the given samples.
Parameters
----------
Y: T.tensor
samples from the upper layer
X: T.tensor
samples from the lower layer
Returns
-------
log_p: T.tensor
log-probabilities for the samples in X and Y
"""
n_X, n_Y = self.get_hyper_params(['n_X', 'n_Y'])
b, W, U = self.get_model_params(['b', 'W', 'U'])
W = T.tril(W, k=-1)
prob_X = self.sigmoid(T.dot(X, W) + T.dot(Y, U) + T.shape_padleft(b))
log_prob = X*T.log(prob_X) + (1-X)*T.log(1-prob_X)
log_prob = T.sum(log_prob, axis=1)
return log_prob
示例5: sample
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def sample(self, n_samples):
'''
Inspired by jbornschein's implementation.
'''
z0 = T.zeros((n_samples, self.dim,)).astype(floatX) + T.shape_padleft(self.b)
rs = self.trng.uniform((self.dim, n_samples), dtype=floatX)
def _step_sample(i, W_i, r_i, z):
p_i = T.nnet.sigmoid(z[:, i]) * 0.9999 + 0.000005
x_i = (r_i <= p_i).astype(floatX)
z = z + T.outer(x_i, W_i)
return z, x_i
seqs = [T.arange(self.dim), self.W, rs]
outputs_info = [z0, None]
non_seqs = []
(zs, x), updates = scan(_step_sample, seqs, outputs_info, non_seqs,
self.dim)
return x.T, updates
示例6: do_layer
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def do_layer(activation, ipt, weights, biases):
"""
Perform a layer operation, i.e. out = activ( xW + b )
activation: An activation function
ipt: Tensor of shape (n_batch, X)
weights: Tensor of shape (X, Y)
biases: Tensor of shape (Y)
Returns: Tensor of shape (n_batch, Y)
"""
xW = T.dot(ipt, weights)
b = T.shape_padleft(biases)
return activation( xW + b )
示例7: make_dropout_mask
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def make_dropout_mask(shape, keep_frac, srng):
return T.shape_padleft(T.cast(srng.binomial(shape, p=keep_frac), 'float32') / keep_frac)
示例8: process
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def process(self, ipt, dropout_masks=Ellipsis):
if dropout_masks is Ellipsis:
dropout_masks = None
append_masks = False
else:
append_masks = True
if self.dropout_keep != 1 and dropout_masks not in ([], None):
ipt = apply_dropout(ipt, dropout_masks[0])
dropout_masks = dropout_masks[1:]
xW = T.dot(ipt, self._W)
b = T.shape_padleft(self._b)
if append_masks:
return self.activation( xW + b ), dropout_masks
else:
return self.activation( xW + b )
示例9: get_dropout_masks
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def get_dropout_masks(self, srng, keep_frac):
"""
Get dropout masks for the GRU.
"""
return [T.shape_padleft(T.cast(srng.binomial((self._input_width,), p=keep_frac), 'float32') / keep_frac),
T.shape_padleft(T.cast(srng.binomial((self._output_width,), p=keep_frac), 'float32') / keep_frac)]
示例10: set_output
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def set_output(self):
self._output = tensor.sum(tensor.shape_padright(self._prev_layer.output) *
tensor.shape_padleft(self.W.val),
axis=-2)
if self._bias:
self._output += tensor.shape_padleft(self.b.val)
示例11: test_vector
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def test_vector(self):
y_idx = [3]
def f(a):
return crossentropy_softmax_1hot(T.shape_padleft(a), y_idx)[0]
utt.verify_grad(f, [numpy.random.rand(4)])
示例12: test_vectors
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def test_vectors(self):
y_idx = [3]
def f(a, b):
return crossentropy_softmax_1hot(T.shape_padleft(a) + b, y_idx)[0]
utt.verify_grad(f, [numpy.random.rand(4), numpy.random.rand(4)])
示例13: fprop
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def fprop(self, all_states):
if self.ntimes:
stateshape0 = all_states.shape[0]
shape0 = TT.switch(TT.gt(self.n, 0), self.n, all_states.shape[0])
single_frame = TT.shape_padleft(all_states[stateshape0-1])
mask = TT.alloc(numpy.float32(1), shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
single_frame = all_states[all_states.shape[0]-1]
self.out = single_frame
return single_frame
示例14: gaussian_chol
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def gaussian_chol(mean, logvar, chol, sample=None):
if sample != None:
raise Exception('Not implemented')
diag = gaussian_diag(mean, logvar)
mask = T.shape_padleft(T.triu(T.ones_like(chol[0]), 1))
sample = diag.sample + T.batched_dot(diag.sample, chol * mask)
return RandomVariable(sample, diag.logp, diag.entr, mean=mean, logvar=logvar)
示例15: fprop
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import shape_padleft [as 别名]
def fprop(self, x):
# This is black magic based on broadcasting,
# that's why variable names don't make any sense.
a = TT.shape_padleft(x)
padding = [1] * x.ndim
b = TT.alloc(numpy.float32(1), self.n_times, *padding)
self.out = a * b
return self.out