本文整理匯總了Python中theano.scan方法的典型用法代碼示例。如果您正苦於以下問題:Python theano.scan方法的具體用法?Python theano.scan怎麽用?Python theano.scan使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類theano
的用法示例。
在下文中一共展示了theano.scan方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: link
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def link(self,attended,state,source):
step_function=self.step;
attended_=T.tanh(T.dot(attended,self.W_A_X))+self.b_A_X;
#attended_=attended;
[energy,glimpsed],_=theano.scan(fn=step_function,
sequences=[attended_],
outputs_info=None,
non_sequences=[attended_,source]);
self.energy=energy;
#combine
#combine=T.concatenate([glimpsed,attended],axis=-1);
combine=T.concatenate([glimpsed,source],axis=-1);
combined=T.tanh(T.dot(combine,self.W_A_combine))+self.b_A_combine;
#no source
#combined=T.tanh(T.dot(glimpsed,self.W_A_combine))+self.b_A_combine;
return combined;
示例2: ctc_path_probs
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def ctc_path_probs(predict, Y, alpha=1e-4):
smoothed_predict = (1 - alpha) * predict[:, Y] + alpha * np.float32(1.) / Y.shape[0]
L = T.log(smoothed_predict)
zeros = T.zeros_like(L[0])
log_first = zeros
f_skip_idxs = ctc_create_skip_idxs(Y)
b_skip_idxs = ctc_create_skip_idxs(Y[::-1]) # there should be a shortcut to calculating this
def step(log_f_curr, log_b_curr, f_active, log_f_prev, b_active, log_b_prev):
f_active_next, log_f_next = ctc_update_log_p(f_skip_idxs, zeros, f_active, log_f_curr, log_f_prev)
b_active_next, log_b_next = ctc_update_log_p(b_skip_idxs, zeros, b_active, log_b_curr, log_b_prev)
return f_active_next, log_f_next, b_active_next, log_b_next
[f_active, log_f_probs, b_active, log_b_probs], _ = theano.scan(
step, sequences=[L, L[::-1, ::-1]], outputs_info=[np.int32(1), log_first, np.int32(1), log_first])
idxs = T.arange(L.shape[1]).dimshuffle('x', 0)
mask = (idxs < f_active.dimshuffle(0, 'x')) & (idxs < b_active.dimshuffle(0, 'x'))[::-1, ::-1]
log_probs = log_f_probs + log_b_probs[::-1, ::-1] - L
return log_probs, mask
示例3: process
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def process(self, input_vector, seq_len):
"""
Convert an input vector into a sequence of categorical distributions
Params:
input_vector: Vector of shape (n_batch, input_width)
seq_len: How many outputs to produce
Returns: Sequence distribution of shape (n_batch, seq_len, num_words)
"""
n_batch = input_vector.shape[0]
outputs_info = [self._seq_gru.initial_state(n_batch)]
scan_step = lambda state, ipt: self._seq_gru.step(ipt, state)
all_out, _ = theano.scan(scan_step, non_sequences=[input_vector], n_steps=seq_len, outputs_info=outputs_info)
# all_out is of shape (seq_len, n_batch, state_size). Squash and apply layer
flat_out = all_out.reshape([-1, self._state_size])
flat_final = self._transform_stack.process(flat_out)
final = flat_final.reshape([seq_len, n_batch, self._num_words]).dimshuffle([1,0,2])
return final
示例4: _make_scan
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def _make_scan(self):
"""Build the sequential composition / scan graph."""
batch_size, seq_length = self.X.shape
# Look up all of the embeddings that will be used.
raw_embeddings = self.embeddings[self.X] # batch_size * seq_length * emb_dim
raw_embeddings = raw_embeddings.dimshuffle(1, 0, 2)
# Initialize the hidden state.
hidden_init = T.zeros((batch_size, self.model_dim))
self.states = theano.scan(
self._step,
sequences=[raw_embeddings],
outputs_info=[hidden_init])[0]
self.final_representations = self.states[-1]
self.transitions_pred = T.zeros((batch_size, 0))
self.predict_transitions = False
self.tracking_state_final = None
示例5: get_output
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def get_output(self, train=False):
X = self.get_input(train) # shape: (nb_samples, time (padded with zeros), input_dim)
# new shape: (time, nb_samples, input_dim) -> because theano.scan iterates over main dimension
padded_mask = self.get_padded_shuffled_mask(train, X, pad=1)
X = X.dimshuffle((1, 0, 2))
x = T.dot(X, self.W) + self.b
# scan = theano symbolic loop.
# See: http://deeplearning.net/software/theano/library/scan.html
# Iterate over the first dimension of the x array (=time).
outputs, updates = theano.scan(
self._step, # this will be called with arguments (sequences[i], outputs[i-1], non_sequences[i])
sequences=[x, dict(input=padded_mask, taps=[-1])], # tensors to iterate over, inputs to _step
# initialization of the output. Input to _step with default tap=-1.
outputs_info=T.unbroadcast(alloc_zeros_matrix(X.shape[1], self.output_dim), 1),
non_sequences=self.U, # static inputs to _step
truncate_gradient=self.truncate_gradient)
if self.return_sequences:
return outputs.dimshuffle((1, 0, 2))
return outputs[-1]
示例6: get_cost_updates
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def get_cost_updates(self, lr=0.1, persistent=None, k=1):
pre_sigmoid_ph, ph_mean, ph_sample = self.sample_h_given_v(self.input)
if persistent is None:
chain_start = ph_sample
else:
chain_start = persistent
([pre_sigmoid_nvs,nv_means,nv_samples,pre_sigmoid_nhs,nh_means,nh_samples],updates) = \
theano.scan(self.gibbs_step, outputs_info=[None, None, None, None, None, chain_start],n_steps=k,name="gibbs_step")
chain_end = nv_samples[-1]
cost = T.mean(self.free_energy(self.input)) - T.mean(self.free_energy(chain_end))
gparams = T.grad(cost, self.params, consider_constant=[chain_end])
for gparam, param in zip(gparams, self.params):
updates[param] = param - gparam * T.cast(lr,dtype=theano.config.floatX)
if persistent:
updates[persistent] = nh_samples[-1]
monitoring_cost = self.get_pseudo_likelihood_cost(updates)
else:
monitoring_cost = self.get_reconstruction_cost(updates,pre_sigmoid_nvs[-1])
return monitoring_cost, updates
示例7: inner_sitsot_only_last_step_used
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def inner_sitsot_only_last_step_used(self, var, scan_args):
"""
Given a inner nit_sot output of scan, return True iff the outer
nit_sot output has only one client and that client is a Subtensor
instance that takes only the last step (last element along the first
axis).
"""
idx = scan_args.inner_out_sit_sot.index(var)
outer_var = scan_args.outer_out_sit_sot[idx]
if len(outer_var.clients) == 1:
client = outer_var.clients[0][0]
if (client != 'output' and isinstance(client.op,
theano.tensor.Subtensor)):
lst = theano.tensor.subtensor.get_idx_list(
client.inputs, client.op.idx_list)
if (len(lst) == 1 and
theano.tensor.extract_constant(lst[0]) == -1):
return True
return False
示例8: test_scan_with_shared_update
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def test_scan_with_shared_update(self):
x = tensor.vector('x')
# counts how many times its value is used
counter = theano.shared(0, name="shared")
counter.update = counter + 1
def step(x, a):
r = a + x
# introducing a shared variable with an update into the
# inner graph is unsupported and the code must crash rather
# than silently produce the wrong result.
r.tag.replacement = counter * (a - x)
return r
s, _ = theano.scan(step, sequences=x,
outputs_info=[numpy.array(0.)])
self.assertRaises(NotImplementedError,
map_variables, self.replacer, [s])
示例9: test_scan_with_shared_update2
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def test_scan_with_shared_update2(self):
x = tensor.vector('x')
# counts how many times its value is used
counter = theano.shared(0, name="shared")
counter.update = counter + 1
def step(x, a):
r = a + x
# introducing a shared variable with an update into the
# inner graph is unsupported and the code must crash rather
# than silently produce the wrong result.
r.tag.replacement = counter * (a - x)
# the shared variable was already present, but the
# replacement changes the number of times it is used,
# which would have to change the updates, which is
# unsupported.
return r + counter
s, _ = theano.scan(step, sequences=x,
outputs_info=[numpy.array(0.)])
self.assertRaises(NotImplementedError,
map_variables, self.replacer, [s])
示例10: test_printing_scan
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def test_printing_scan():
# Skip test if pydot is not available.
if not theano.printing.pydot_imported:
raise SkipTest('pydot not available')
def f_pow2(x_tm1):
return 2 * x_tm1
state = theano.tensor.scalar('state')
n_steps = theano.tensor.iscalar('nsteps')
output, updates = theano.scan(f_pow2,
[],
state,
[],
n_steps=n_steps,
truncate_gradient=-1,
go_backwards=False)
f = theano.function([state, n_steps],
output,
updates=updates,
allow_input_downcast=True)
theano.printing.pydotprint(output, scan_graphs=True)
theano.printing.pydotprint(f, scan_graphs=True)
示例11: calculate
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def calculate(self, input):
P = self.P
Q = self.Q
R = self.R
decay = self.decay
f_0 = T.zeros((input.shape[1], self.n_out), dtype=theano.config.floatX)
([f1, s1, f2, s2, f3], updates) = theano.scan( fn = StrConvLayer.loop_one_step,
sequences = input,
outputs_info = [ f_0, f_0, f_0, f_0, f_0 ],
non_sequences = [ P, Q, R, decay ]
)
return f1, s1, f2, s2, f3
# ###
# Dynamic programming to calculate aggregated unigram to trigram representation vectors
# ###
示例12: compute_emb
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def compute_emb(x, W):
def _step(xi, emb, W):
if prm.att_doc:
new_shape = (xi.shape[0], xi.shape[1], xi.shape[2], prm.dim_emb)
else:
new_shape = (xi.shape[0], xi.shape[1], prm.dim_emb)
out = W[xi.flatten()].reshape(new_shape).sum(-2)
return out / tensor.maximum(1., tensor.neq(xi,-1).astype('float32').sum(-1, keepdims=True))
if prm.att_doc:
emb_init = tensor.alloc(0., x.shape[1], x.shape[2], prm.dim_emb)
else:
emb_init = tensor.alloc(0., x.shape[1], prm.dim_emb)
(embs), scan_updates = theano.scan(_step,
sequences=[x],
outputs_info=[emb_init],
non_sequences=[W],
name='emb_scan',
n_steps=x.shape[0])
return embs
示例13: generative_sampling
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def generative_sampling(self, seed, emb_data, sample_length):
fruit = theano.shared(value=seed)
def step(h_tm, y_tm):
h_t = self.activation(T.dot(emb_data[y_tm], self.W) +
T.dot(h_tm, self.U) + self.bh)
y_t = T.nnet.softmax(T.dot(h_t, self.V) + self.by)
y = T.argmax(y_t, axis=1)
return h_t, y[0]
[_, samples], _ = theano.scan(fn=step,
outputs_info=[self.h0, fruit],
n_steps=sample_length)
get_samples = theano.function(inputs=[],
outputs=samples)
return get_samples()
示例14: get_output_for
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [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)
示例15: ctc_batch_cost
# 需要導入模塊: import theano [as 別名]
# 或者: from theano import scan [as 別名]
def ctc_batch_cost(y_true, y_pred, input_length, label_length):
'''Runs CTC loss algorithm on each batch element.
# Arguments
y_true: tensor (samples, max_string_length) containing the truth labels
y_pred: tensor (samples, time_steps, num_categories) containing the prediction,
or output of the softmax
input_length: tensor (samples,1) containing the sequence length for
each batch item in y_pred
label_length: tensor (samples,1) containing the sequence length for
each batch item in y_true
# Returns
Tensor with shape (samples,1) containing the
CTC loss of each element
'''
def ctc_step(y_true_step, y_pred_step, input_length_step, label_length_step):
y_pred_step = y_pred_step[0: input_length_step[0]]
y_true_step = y_true_step[0:label_length_step[0]]
return ctc_cost(y_pred_step, y_true_step)
ret, _ = theano.scan(
fn=ctc_step,
outputs_info=None,
sequences=[y_true, y_pred, input_length, label_length]
)
ret = ret.dimshuffle('x', 0)
return ret
# HIGH ORDER FUNCTIONS