本文整理汇总了Python中theano.tensor.fill函数的典型用法代码示例。如果您正苦于以下问题:Python fill函数的具体用法?Python fill怎么用?Python fill使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fill函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_exp_over_1_plus_exp
def test_exp_over_1_plus_exp(self):
m = self.get_mode(excluding=['local_elemwise_fusion'])
x = T.dvector()
# tests exp_over_1_plus_exp
f = theano.function([x], T.exp(x)/(1+T.exp(x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid]
# tests inv_1_plus_exp
f = theano.function([x], T.fill(x,1.0) / (1+T.exp(-x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid]
# tests inv_1_plus_exp with neg
f = theano.function([x], T.fill(x,-1.0) / (1+T.exp(-x)), mode=m)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid,
theano.tensor.inplace.neg_inplace]
# tests double inv_1_plus_exp with neg
# (-1)(exp(x)) / (1+exp(x))(1+exp(-x))
# = (-1)/(1+exp(-x)) * exp(x)/(1+exp(x))
# = - (sigm(x) * sigm(x))
f = theano.function([x], (T.fill(x,-1.0)*T.exp(x)) / ((1+T.exp(x))*(1+T.exp(-x))), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [sigmoid,
T.mul, theano.tensor.inplace.neg_inplace]
示例2: output_probabilistic
def output_probabilistic(self, m_x, v_x):
m_linear = T.dot(m_x, self.m_W[ 0, :, : ]) + T.tile(self.m_b[ 0, :, : ], [ m_x.shape[ 0 ], 1 ])
v_linear = T.dot(m_x**2, self.v_W[ 0, :, : ]) + T.dot(v_x, self.m_W[ 0, :, : ]**2) + T.dot(v_x, self.v_W[ 0, :, : ]) + \
T.tile(self.v_b[ 0, :, : ], [ m_x.shape[ 0 ], 1 ])
if not self.output_layer:
# We compute the mean and variance after the ReLU activation
alpha = m_linear / T.sqrt(v_linear)
gamma = Network_layer.gamma(-alpha)
gamma_robust = -alpha - 1.0 / alpha + 2.0 / alpha**3
gamma_final = T.switch(T.lt(-alpha, T.fill(alpha, 30)), gamma, gamma_robust)
v_aux = m_linear + T.sqrt(v_linear) * gamma_final
m_a = Network_layer.n_cdf(alpha) * v_aux
v_a = m_a * v_aux * Network_layer.n_cdf(-alpha) + Network_layer.n_cdf(alpha) * v_linear * (1 - gamma_final * (gamma_final + alpha))
return (m_a, v_a)
else:
return (m_linear, v_linear)
开发者ID:Riashat,项目名称:Active-Learning-Bayesian-Convolutional-Neural-Networks,代码行数:25,代码来源:network_layer.py
示例3: dlogp
def dlogp(inputs, gradients):
g_logp, = gradients
cov, delta = inputs
g_logp.tag.test_value = floatX(1.)
n, k = delta.shape
chol_cov = cholesky(cov)
diag = tt.nlinalg.diag(chol_cov)
ok = tt.all(diag > 0)
chol_cov = tt.switch(ok, chol_cov, tt.fill(chol_cov, 1))
delta_trans = solve_lower(chol_cov, delta.T).T
inner = n * tt.eye(k) - tt.dot(delta_trans.T, delta_trans)
g_cov = solve_upper(chol_cov.T, inner)
g_cov = solve_upper(chol_cov.T, g_cov.T)
tau_delta = solve_upper(chol_cov.T, delta_trans.T)
g_delta = tau_delta.T
g_cov = tt.switch(ok, g_cov, -np.nan)
g_delta = tt.switch(ok, g_delta, -np.nan)
return [-0.5 * g_cov * g_logp, -g_delta * g_logp]
示例4: _FindB_best
def _FindB_best(lPLcl, lPprev, dVLcl):
srtLcl = tensor.argsort(-lPLcl)
srtLcl = srtLcl[:beam_size]
deltaVec = tensor.fill( lPLcl[srtLcl], numpy_floatX(-10000.))
deltaVec = tensor.set_subtensor(deltaVec[0], lPprev)
lProbBest = ifelse(tensor.eq( dVLcl, tensor.zeros_like(dVLcl)), lPLcl[srtLcl] + lPprev, deltaVec)
xWIdxBest = ifelse(tensor.eq( dVLcl, tensor.zeros_like(dVLcl)), srtLcl, tensor.zeros_like(srtLcl))
return lProbBest, xWIdxBest
示例5: transform_targets
def transform_targets(targets):
"""Transform targets into a format suitable for passing to cost()."""
reshaped = T.shape_padleft(targets)
blanks = T.fill(reshaped, _BLANK)
result = T.concatenate([blanks, reshaped]).dimshuffle(1, 0, 2).reshape((2*targets.shape[0], targets.shape[1]))
result = T.concatenate([result, T.shape_padleft(result[0])])
return result
示例6: __init__
def __init__(self, mean, var, rng=None):
self.mean = mean
# This allows to use var with shape (1, 1, n)
self.var = T.fill(mean, var)
self.stt = T.concatenate((mean, self.var), -1)
self.maximum = self.mean
super(DiagGauss, self).__init__(rng)
示例7: chain_crf_loss
def chain_crf_loss(energies, targets, masks):
"""
compute minus log likelihood of chain crf as chain crf loss.
:param energies: Theano 4D tensor
energies of each step. the shape is [batch_size, n_time_steps, num_labels, num_labels],
where the pad label index is at last.
:param targets: Theano 2D tensor
targets in the shape [batch_size, n_time_steps]
:param masks: Theano 2D tensor
masks in the shape [batch_size, n_time_steps]
:return: Theano 1D tensor
an expression for minus log likelihood loss.
"""
assert energies.ndim == 4
assert targets.ndim == 2
assert masks.ndim == 2
def inner_function(energies_one_step, targets_one_step, mask_one_step, prior_partition, prev_label, tg_energy):
"""
:param energies_one_step: [batch_size, t, t]
:param targets_one_step: [batch_size]
:param prior_partition: [batch_size, t]
:param prev_label: [batch_size]
:param tg_energy: [batch_size]
:return:
"""
partition_shuffled = prior_partition.dimshuffle(0, 1, 'x')
partition_t = T.switch(mask_one_step.dimshuffle(0, 'x'),
theano_logsumexp(energies_one_step + partition_shuffled, axis=1),
prior_partition)
return [partition_t, targets_one_step,
tg_energy + energies_one_step[T.arange(energies_one_step.shape[0]), prev_label, targets_one_step]]
# Input should be provided as (n_batch, n_time_steps, num_labels, num_labels)
# but scan requires the iterable dimension to be first
# So, we need to dimshuffle to (n_time_steps, n_batch, num_labels, num_labels)
energies_shuffled = energies.dimshuffle(1, 0, 2, 3)
targets_shuffled = targets.dimshuffle(1, 0)
masks_shuffled = masks.dimshuffle(1, 0)
# initials should be energies_shuffles[0, :, -1, :]
init_label = T.cast(T.fill(energies[:, 0, 0, 0], -1), 'int32')
energy_time0 = energies_shuffled[0]
target_time0 = targets_shuffled[0]
initials = [energies_shuffled[0, :, -1, :], target_time0,
energy_time0[T.arange(energy_time0.shape[0]), init_label, target_time0]]
[partitions, _, target_energies], _ = theano.scan(fn=inner_function, outputs_info=initials,
sequences=[energies_shuffled[1:], targets_shuffled[1:],
masks_shuffled[1:]])
partition = partitions[-1]
target_energy = target_energies[-1]
loss = theano_logsumexp(partition, axis=1) - target_energy
return loss
示例8: output_deterministic
def output_deterministic(self, output_previous):
# We add an additional input with value 1
output_previous_with_bias = \
T.concatenate([ output_previous, T.alloc(1, 1) ], 0) / \
T.sqrt(self.n_inputs)
# We compute the mean and variance after the linear operation
a = T.dot(self.w, output_previous_with_bias)
if (self.non_linear):
# We compute the ReLU activation
a = T.switch(T.lt(a, T.fill(a, 0)), T.fill(a, 0), a)
return a
开发者ID:Riashat,项目名称:Active-Learning-Bayesian-Convolutional-Neural-Networks,代码行数:19,代码来源:network_layer.py
示例9: MRR_loss
def MRR_loss(y_true, y_pred):
'''
Training data have to be Xloop, Xtap, target = utils.MakeTrainingDataRank(Loop, Tap)
Batch size have to be 40
'''
comp = T.zeros_like(y_true)
comp = T.fill(comp, T.mean(y_pred[T.argmax(y_true)]))
Rank = T.sum(T.gt(comp, y_pred))
#T.dot(y_pred[39].T, T.ones_like(y_true).T)
return Rank + T.mean(y_true) * 0 + T.mean(y_pred) * 0
示例10: _gen_exprs
def _gen_exprs(self, inpt):
"""Return the exprssions of the recognition model."""
P = self.parameters.gen
n_layers = len(self.n_hiddens_gen)
hidden_to_hiddens = [getattr(P, 'hidden_to_hidden_%i' % i)
for i in range(n_layers - 1)]
hidden_biases = [getattr(P, 'hidden_bias_%i' % i)
for i in range(n_layers)]
initial_hidden_means = [getattr(P, 'initial_hidden_means_%i' % i)
for i in range(n_layers)]
initial_hidden_vars = [getattr(P, 'initial_hidden_vars_%i' % i)
for i in range(n_layers)]
recurrents = [getattr(P, 'recurrent_%i' % i)
for i in range(n_layers)]
shortcut_size = self.n_hiddens_recog[-1]
p_dropout_inpt = T.zeros_like(inpt[:, :, :self.n_latent])
p_dropout_inpt = T.fill(p_dropout_inpt, self.p_dropout_inpt)
p_dropout_shortcut = T.zeros_like(inpt[:, :, self.n_latent:])
p_dropout_shortcut = T.fill(p_dropout_shortcut, self.p_dropout_inpt)
p_dropout_inpt = T.concatenate([p_dropout_inpt, p_dropout_shortcut],
axis=2)
p_dropouts = [p_dropout_inpt] + self.p_dropout_hiddens
if self.p_dropout_hidden_to_out is None:
p_dropouts.append(self.p_dropout_hiddens[-1])
else:
p_dropouts.append(self.p_dropout_hidden_to_out)
exprs = vprnn.exprs(
inpt, T.zeros_like(inpt), P.in_to_hidden, hidden_to_hiddens, P.hidden_to_out,
hidden_biases, [1 for _ in hidden_biases],
initial_hidden_means, initial_hidden_vars,
recurrents,
P.out_bias, 1, self.gen_transfers, self.assumptions.statify_visible,
p_dropouts=p_dropouts)
return exprs
示例11: test_1msigmoid
def test_1msigmoid(self):
if not register_local_1msigmoid:
return
m = self.get_mode()
x = T.fmatrix()
# tests exp_over_1_plus_exp
f = theano.function([x], 1 - T.exp(x) / (1 + T.exp(x)), mode=m)
assert [node.op for node in f.maker.fgraph.toposort()] == [tensor.neg, sigmoid_inplace]
# tests inv_1_plus_exp
f = theano.function([x], 1 - T.fill(x, 1.0) / (1 + T.exp(-x)), mode=m)
assert [node.op for node in f.maker.fgraph.toposort()] == [tensor.neg, sigmoid_inplace]
示例12: __init__
def __init__(self, input, input_sm, vocab_size, emb_dim, local_context_size, global_context_size):
# initialize W_emb
global rng
global init_range
if pretrain_file:
linear_W_emb = load_pretrain_emb(pretrain_file)
print "* Using pretrained linear_W_emb ..."
assert(len(linear_W_emb) == vocab_size)
else:
linear_W_emb = np.asarray(rng.uniform(
low=-init_range, high=init_range, size=(vocab_size, emb_dim)), dtype=theano.config.floatX)
# shared variables
self.W_emb = theano.shared(value=linear_W_emb, name='W_emb')
# stack vectors
input = T.cast(input, '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[:, :local_context_size].flatten()].reshape(
(input.shape[0], local_context_size * emb_dim)) # self.W_emb.shape[1]
# define symbolic functions for calculating the mean of sentences
W = T.matrix('W')
eos_vector = T.vector('eos_vector')
eos_vector = T.fill(T.zeros_like(input[0,local_context_size:]), io_vocab.VocabConstants.EOS_INDEX)
def weighted_sentence(sentence, W, eos_vector):
sent_len = T.sum(T.neq(sentence, eos_vector))
return T.mean(W[sentence[:sent_len]], axis=0)
output_global, updates = theano.scan(fn=weighted_sentence,
outputs_info=None,
sequences=input[:, local_context_size:],
non_sequences=[self.W_emb, eos_vector])
# concatenate local output and global output to form the output matrix
self.output = T.concatenate([output_local, output_global], axis=1)
# params is the word embedding matrix
self.params = [self.W_emb]
示例13: test_1msigmoid
def test_1msigmoid(self):
if not register_local_1msigmoid:
return
m = theano.config.mode
if m == 'FAST_COMPILE':
m = 'FAST_RUN'
x = T.fmatrix()
# tests exp_over_1_plus_exp
f = theano.function([x], 1 - T.exp(x)/(1+T.exp(x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg, sigmoid_inplace]
# tests inv_1_plus_exp
f = theano.function([x], 1 - T.fill(x,1.0) / (1+T.exp(-x)), mode=m)
theano.printing.debugprint(f)
assert [node.op for node in f.maker.env.toposort()] == [tensor.neg,
sigmoid_inplace]
示例14: output_probabilistic
def output_probabilistic(self, m_w_previous, v_w_previous):
# We add an additional deterministic input with mean 1 and variance 0
m_w_previous_with_bias = \
T.concatenate([ m_w_previous, T.alloc(1, 1) ], 0)
v_w_previous_with_bias = \
T.concatenate([ v_w_previous, T.alloc(0, 1) ], 0)
# We compute the mean and variance after the linear operation
m_linear = T.dot(self.m_w, m_w_previous_with_bias) / T.sqrt(self.n_inputs)
v_linear = (T.dot(self.v_w, v_w_previous_with_bias) + \
T.dot(self.m_w**2, v_w_previous_with_bias) + \
T.dot(self.v_w, m_w_previous_with_bias**2)) / self.n_inputs
if (self.non_linear):
# We compute the mean and variance after the ReLU activation
alpha = m_linear / T.sqrt(v_linear)
gamma = Network_layer.gamma(-alpha)
gamma_robust = -alpha - 1.0 / alpha + 2.0 / alpha**3
gamma_final = T.switch(T.lt(-alpha, T.fill(alpha, 30)), gamma, gamma_robust)
v_aux = m_linear + T.sqrt(v_linear) * gamma_final
m_a = Network_layer.n_cdf(alpha) * v_aux
v_a = m_a * v_aux * Network_layer.n_cdf(-alpha) + \
Network_layer.n_cdf(alpha) * v_linear * \
(1 - gamma_final * (gamma_final + alpha))
return (m_a, v_a)
else:
return (m_linear, v_linear)
开发者ID:Riashat,项目名称:Active-Learning-Bayesian-Convolutional-Neural-Networks,代码行数:37,代码来源:network_layer.py
示例15: chain_crf_accuracy
def chain_crf_accuracy(energies, targets):
"""
decode crf and compute accuracy
:param energies: Theano 4D tensor
energies of each step. the shape is [batch_size, n_time_steps, num_labels, num_labels],
where the pad label index is at last.
:param targets: Theano 2D tensor
targets in the shape [batch_size, n_time_steps]
:return: Theano 1D tensor
an expression for minus log likelihood loss.
"""
assert energies.ndim == 4
assert targets.ndim == 2
def inner_function(energies_one_step, prior_pi, prior_pointer):
"""
:param energies_one_step: [batch_size, t, t]
:param prior_pi: [batch_size, t]
:param prior_pointer: [batch_size, t]
:return:
"""
prior_pi_shuffled = prior_pi.dimshuffle(0, 1, 'x')
pi_t = T.max(prior_pi_shuffled + energies_one_step, axis=1)
pointer_t = T.argmax(prior_pi_shuffled + energies_one_step, axis=1)
return [pi_t, pointer_t]
def back_pointer(pointer, pointer_tp1):
"""
:param pointer: [batch, t]
:param point_tp1: [batch,]
:return:
"""
return pointer[T.arange(pointer.shape[0]), pointer_tp1]
# Input should be provided as (n_batch, n_time_steps, num_labels, num_labels)
# but scan requires the iterable dimension to be first
# So, we need to dimshuffle to (n_time_steps, n_batch, num_labels, num_labels)
energies_shuffled = energies.dimshuffle(1, 0, 2, 3)
# pi at time 0 is the last rwo at time 0. but we need to remove the last column which is the pad symbol.
pi_time0 = energies_shuffled[0, :, -1, :-1]
# the last row and column is the tag for pad symbol. reduce these two dimensions by 1 to remove that.
# now the shape of energies_shuffled is [n_time_steps, b_batch, t, t] where t = num_labels - 1.
energies_shuffled = energies_shuffled[:, :, :-1, :-1]
initials = [pi_time0, T.cast(T.fill(pi_time0, -1), 'int64')]
[pis, pointers], _ = theano.scan(fn=inner_function, outputs_info=initials, sequences=[energies_shuffled[1:]])
pi_n = pis[-1]
pointer_n = T.argmax(pi_n, axis=1)
back_pointers, _ = theano.scan(fn=back_pointer, outputs_info=pointer_n, sequences=[pointers], go_backwards=True)
# prediction shape [batch_size, length]
prediction_revered = T.concatenate([pointer_n.dimshuffle(0, 'x'), back_pointers.dimshuffle(1, 0)], axis=1)
prediction = prediction_revered[:, T.arange(prediction_revered.shape[1] - 1, -1, -1)]
return prediction, T.eq(prediction, targets)