本文整理汇总了Python中theano.sandbox.scan.scan函数的典型用法代码示例。如果您正苦于以下问题:Python scan函数的具体用法?Python scan怎么用?Python scan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scan函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_Gv
def compute_Gv(*args):
idx0 = const([0])
ep = [TT.alloc(const(0), 1, *shp)
for shp in model.params_shape]
def Gv_step(*gv_args):
idx = TT.cast(gv_args[0], 'int32')
nw_inps = [x[idx * options['cbs']: \
(idx + 1) * options['cbs']] for x in
loc_inputs]
replace = dict(zip(model.inputs, nw_inps))
nw_cost, nw_preactiv_out = safe_clone([model.train_cost,
model.preactiv_out],
replace)
nw_gvs = TT.Lop(nw_preactiv_out, model.params,
TT.Rop(TT.grad(nw_cost, nw_preactiv_out),
model.params, args))
Gvs = [ogv + ngv
for (ogv, ngv) in zip(gv_args[1:], nw_gvs)]
return [gv_args[0] + const(1)] + Gvs
states = [idx0] + ep
n_steps = options['mbs'] // options['cbs']
rvals, updates = scan(Gv_step,
states=states,
n_steps=n_steps,
mode=theano.Mode(linker='cvm'),
name='Gv_step',
profile=options['profile'])
final_Gvs = [x[0] / const(n_steps) for x in rvals[1:]]
return final_Gvs, updates
示例2: _e_step
def _e_step(psamples, W_list, b_list, n_steps=100, eps=1e-5):
"""
Performs 'n_steps' of mean-field inference (used to compute positive phase
statistics)
Parameters
----------
psamples : array-like object of theano shared variables
State of each layer of the DBM (during the inference process).
psamples[0] points to the input
n_steps : integer
Number of iterations of mean-field to perform
"""
depth = len(psamples)
new_psamples = [T.unbroadcast(T.shape_padleft(psample)) for psample in psamples]
# now alternate mean-field inference for even/odd layers
def mf_iteration(*psamples):
new_psamples = [p for p in psamples]
for i in xrange(1, depth, 2):
new_psamples[i] = hi_given(psamples, i, W_list, b_list)
for i in xrange(2, depth, 2):
new_psamples[i] = hi_given(psamples, i, W_list, b_list)
score = 0.0
for i in xrange(1, depth):
score = T.maximum(T.mean(abs(new_psamples[i] - psamples[i])), score)
return new_psamples, theano.scan_module.until(score < eps)
new_psamples, updates = scan(mf_iteration, states=new_psamples, n_steps=n_steps)
return [x[0] for x in new_psamples]
示例3: compute_Gv
def compute_Gv(*args):
idx0 = const([0])
ep = [TT.alloc(const(0), 1, *shp)
for shp in model.params_shape]
def Gv_step(*gv_args):
idx = TT.cast(gv_args[0], 'int32')
nw_inps = [x[idx * options['cbs']: \
(idx + 1) * options['cbs']] for x in
loc_inputs]
replace = dict(zip(model.inputs, nw_inps))
nw_outs = safe_clone(model.outs, replace)
final_results = dict(zip(model.params, [None] * len(model.params)))
for nw_out, out_operator in zip(nw_outs, model.outs_operator):
loc_params = [x for x in model.params
if x in theano.gof.graph.inputs([nw_out])]
loc_args = [x for x, y in zip(args, model.params)
if y in theano.gof.graph.inputs([nw_out])]
if out_operator == 'softmax':
factor = const(options['cbs']) * nw_out
elif out_operator == 'sigmoid':
factor = const(options['cbs']) * nw_out * (1 - nw_out)
else:
factor = const(options['cbs'])
loc_Gvs = TT.Lop(nw_out, loc_params,
TT.Rop(nw_out, loc_params, loc_args) /\
factor)
for lp, lgv in zip(loc_params, loc_Gvs):
if final_results[lp] is None:
final_results[lp] = lgv
else:
final_results[lp] += lgv
Gvs = [ogv + final_results[param]
for (ogv, param) in zip(gv_args[1:], model.params)]
return [gv_args[0] + const(1)] + Gvs
nw_cost, nw_preactiv_out = safe_clone([model.train_cost,
model.preactiv_out],
replace)
nw_gvs = TT.Lop(nw_preactiv_out, model.params,
TT.Rop(TT.grad(nw_cost, nw_preactiv_out),
model.params, args))
Gvs = [ogv + ngv
for (ogv, ngv) in zip(gv_args[1:], nw_gvs)]
return [gv_args[0] + const(1)] + Gvs
states = [idx0] + ep
n_steps = options['mbs'] // options['cbs']
rvals, updates = scan(Gv_step,
states=states,
n_steps=n_steps,
mode=theano.Mode(linker='cvm'),
name='Gv_step',
profile=options['profile'])
final_Gvs = [x[0] / const(n_steps) for x in rvals[1:]]
return final_Gvs, updates
示例4: e_step
def e_step(self, n_steps=100, eps=1e-5):
"""
Performs `n_steps` of mean-field inference (used to compute positive phase statistics).
:param psamples: list of tensor-like objects, representing the state of each layer of
the DBM (during the inference process). psamples[0] points to self.input.
:param n_steps: number of iterations of mean-field to perform.
"""
new_psamples = [T.unbroadcast(T.shape_padleft(psample)) for psample in self.psamples]
# now alternate mean-field inference for even/odd layers
def mf_iteration(*psamples):
new_psamples = [p for p in psamples]
for i in xrange(1,self.depth,2):
new_psamples[i] = self.hi_given(psamples, i)
for i in xrange(2,self.depth,2):
new_psamples[i] = self.hi_given(psamples, i)
score = 0.
for i in xrange(1, self.depth):
score = T.maximum(T.mean(abs(new_psamples[i] - psamples[i])), score)
return new_psamples, theano.scan_module.until(score < eps)
new_psamples, updates = scan(
mf_iteration,
states = new_psamples,
n_steps=n_steps)
return [x[0] for x in new_psamples]
示例5: pos_phase
def pos_phase(self, v, init_state, n_steps=1, eps=1e-3):
"""
Mixed mean-field + sampling inference in positive phase.
:param v: input being conditioned on
:param init: dictionary of initial values
:param n_steps: number of Gibbs updates to perform afterwards.
"""
def pos_mf_iteration(g1, h1, v, pos_counter):
h2 = self.h_hat(g1, v)
s2_1 = self.s1_hat(g1, v)
s2_0 = self.s0_hat(g1, v)
g2 = self.g_hat(h2, s2_1, s2_0)
# stopping criterion
dl_dghat = T.max(abs(self.dlbound_dg(g2, h2, s2_1, s2_0, v)))
dl_dhhat = T.max(abs(self.dlbound_dh(g2, h2, s2_1, s2_0, v)))
stop = T.maximum(dl_dghat, dl_dhhat)
return [g2, h2, s2_1, s2_0, v, pos_counter + 1], theano.scan_module.until(stop < eps)
states = [T.unbroadcast(T.shape_padleft(init_state['g'])),
T.unbroadcast(T.shape_padleft(init_state['h'])),
{'steps': 1},
{'steps': 1},
T.unbroadcast(T.shape_padleft(v)),
T.unbroadcast(T.shape_padleft(0.))]
rvals, updates = scan(
pos_mf_iteration,
states = states,
n_steps=n_steps)
return [rval[0] for rval in rvals]
示例6: scalar_armijo_search
def scalar_armijo_search(phi, phi0, derphi0, c1=constant(1e-4),
n_iters=10, profile=0):
alpha0 = one
phi_a0 = phi(alpha0)
alpha1 = -(derphi0) * alpha0 ** 2 / 2.0 /\
(phi_a0 - phi0 - derphi0 * alpha0)
phi_a1 = phi(alpha1)
csol1 = phi_a0 <= phi0 + c1 * derphi0
csol2 = phi_a1 <= phi0 + c1 * alpha1 * derphi0
def armijo(alpha0, alpha1, phi_a0, phi_a1):
factor = alpha0 ** 2 * alpha1 ** 2 * (alpha1 - alpha0)
a = alpha0 ** 2 * (phi_a1 - phi0 - derphi0 * alpha1) - \
alpha1 ** 2 * (phi_a0 - phi0 - derphi0 * alpha0)
a = a / factor
b = -alpha0 ** 3 * (phi_a1 - phi0 - derphi0 * alpha1) + \
alpha1 ** 3 * (phi_a0 - phi0 - derphi0 * alpha0)
b = b / factor
alpha2 = (-b + TT.sqrt(abs(b ** 2 - 3 * a * derphi0))) / (3.0 * a)
phi_a2 = phi(alpha2)
end_condition = phi_a2 <= phi0 + c1 * alpha2 * derphi0
end_condition = TT.bitwise_or(
TT.isnan(alpha2), end_condition)
end_condition = TT.bitwise_or(
TT.isinf(alpha2), end_condition)
alpha2 = TT.switch(
TT.bitwise_or(alpha1 - alpha2 > alpha1 / constant(2.),
one - alpha2 / alpha1 < 0.96),
alpha1 / constant(2.),
alpha2)
return [alpha1, alpha2, phi_a1, phi_a2], \
theano.scan_module.until(end_condition)
states = []
states += [TT.unbroadcast(TT.shape_padleft(alpha0), 0)]
states += [TT.unbroadcast(TT.shape_padleft(alpha1), 0)]
states += [TT.unbroadcast(TT.shape_padleft(phi_a0), 0)]
states += [TT.unbroadcast(TT.shape_padleft(phi_a1), 0)]
# print 'armijo'
rvals, _ = scan(
armijo,
states=states,
n_steps=n_iters,
name='armijo',
mode=theano.Mode(linker='cvm'),
profile=profile)
sol_scan = rvals[1][0]
a_opt = ifelse(csol1, one,
ifelse(csol2, alpha1,
sol_scan))
score = ifelse(csol1, phi_a0,
ifelse(csol2, phi_a1,
rvals[2][0]))
return a_opt, score
示例7: test_005
def test_005():
sq = theano.tensor.fvector('sq')
nst = theano.tensor.iscalar('nst')
out, _ = scan.scan(lambda s: s+numpy.float32(1),
sequences=sq,
states=[None],
n_steps=nst)
fn = theano.function([sq, nst], out)
val_sq = numpy.float32([1, 2, 3, 4, 5])
assert numpy.all(fn(val_sq, 5) == val_sq + 1)
示例8: test_001
def test_001():
x0 = theano.tensor.fvector('x0')
state = theano.tensor.unbroadcast(
theano.tensor.shape_padleft(x0), 0)
out, _ = scan.scan(lambda x: x+numpy.float32(1),
states=state,
n_steps=5)
fn = theano.function([x0], out[0])
val_x0 = numpy.float32([1, 2, 3])
assert numpy.all(fn(val_x0) == val_x0 + 5)
示例9: test_002
def test_002():
x0 = theano.tensor.fvector('x0')
state = theano.tensor.alloc(
theano.tensor.constant(numpy.float32(0)),
6,
x0.shape[0])
state = theano.tensor.set_subtensor(state[0], x0)
out, _ = scan.scan(lambda x: x+numpy.float32(1),
states=state,
n_steps=5)
fn = theano.function([x0], out)
val_x0 = numpy.float32([1, 2, 3])
assert numpy.all(fn(val_x0)[-1] == val_x0 + 5)
assert numpy.all(fn(val_x0)[0] == val_x0)
示例10: linear_cg_fletcher_reeves
def linear_cg_fletcher_reeves(compute_Ax, bs, xinit = None,
rtol = 1e-6, maxiter = 1000, damp=0,
floatX = None, profile=0):
"""
assume all are lists all the time
Reference:
http://en.wikipedia.org/wiki/Conjugate_gradient_method
"""
n_params = len(bs)
def loop(rz_old, *args):
ps = args[:n_params]
rs = args[n_params:2*n_params]
xs = args[2*n_params:]
_Aps = compute_Ax(*ps)
Aps = [x + damp*y for x,y in zip(_Aps, ps)]
alpha = rz_old/sum( (x*y).sum() for x,y in zip(Aps, ps))
xs = [x + alpha * p for x,p in zip(xs,ps)]
rs = [r - alpha * Ap for r, Ap, in zip(rs, Aps)]
rz_new = sum( (r*r).sum() for r in rs)
ps = [ r + rz_new/rz_old*p for r,p in zip(rs,ps)]
return [rz_new]+ps+rs+xs, \
theano.scan_module.until(abs(rz_new) < rtol)
if xinit is None:
r0s = bs
_x0s = [tensor.unbroadcast(tensor.shape_padleft(tensor.zeros_like(x))) for x in bs]
else:
init_Ax = compute_Ax(*xinit)
r0s = [bs[i] - init_Ax[i] for i in xrange(len(bs))]
_x0s = [tensor.unbroadcast(tensor.shape_padleft(xi)) for xi in xinit]
_p0s = [tensor.unbroadcast(tensor.shape_padleft(x),0) for x in r0s]
_r0s = [tensor.unbroadcast(tensor.shape_padleft(x),0) for x in r0s]
rz_old = sum( (r*r).sum() for r in r0s)
_rz_old = tensor.unbroadcast(tensor.shape_padleft(rz_old),0)
outs, updates = scan(loop,
states = [_rz_old] + _p0s + _r0s + _x0s,
n_steps = maxiter,
mode = theano.Mode(linker='cvm'),
name = 'linear_conjugate_gradient',
profile=profile)
fxs = outs[1+2*n_params:]
return [x[0] for x in fxs]
示例11: test_003
def test_003():
x0 = theano.tensor.fvector('x0')
sq = theano.tensor.fvector('sq')
state = theano.tensor.alloc(
theano.tensor.constant(numpy.float32(0)),
6,
x0.shape[0])
state = theano.tensor.set_subtensor(state[0], x0)
out, _ = scan.scan(lambda s, x: x+s,
sequences=sq,
states=state,
n_steps=5)
fn = theano.function([sq, x0], out)
val_x0 = numpy.float32([1, 2, 3])
val_sq = numpy.float32([1, 2, 3, 4, 5])
assert numpy.all(fn(val_sq, val_x0)[-1] == val_x0 + 15)
assert numpy.all(fn(val_sq, val_x0)[0] == val_x0)
示例12: linear_cg_precond
def linear_cg_precond(compute_Gv, bs, Msz, rtol = 1e-16, maxit = 100000, floatX = None):
"""
assume all are lists all the time
Reference:
http://en.wikipedia.org/wiki/Conjugate_gradient_method
"""
n_params = len(bs)
def loop(rsold, *args):
ps = args[:n_params]
rs = args[n_params:2*n_params]
xs = args[2*n_params:]
Aps = compute_Gv(*ps)
alpha = rsold/sum( (x*y).sum() for x,y in zip(Aps, ps))
xs = [x + alpha * p for x,p in zip(xs,ps)]
rs = [r - alpha * Ap for r, Ap, in zip(rs, Aps)]
zs = [ r/z for r,z in zip(rs, Msz)]
rsnew = sum( (r*z).sum() for r,z in zip(rs,zs))
ps = [ z + rsnew/rsold*p for z,p in zip(zs,ps)]
return [rsnew]+ps+rs+xs,
theano.scan_module.until(abs(rsnew) < rtol)
r0s = bs
_p0s = [tensor.unbroadcast(tensor.shape_padleft(x/z),0) for x,z in zip(r0s, Msz)]
_r0s = [tensor.unbroadcast(tensor.shape_padleft(x),0) for x in r0s]
_x0s = [tensor.unbroadcast(tensor.shape_padleft(
tensor.zeros_like(x)),0) for x in bs]
rsold = sum( (r*r/z).sum() for r,z in zip(r0s, Msz))
_rsold = tensor.unbroadcast(tensor.shape_padleft(rsold),0)
outs, updates = scan(loop,
states = [_rsold] + _p0s + _r0s + _x0s,
n_steps = maxit,
mode = theano.Mode(linker='c|py'),
name = 'linear_conjugate_gradient',
profile=0)
fxs = outs[1+2*n_params:]
return [x[0] for x in fxs]
示例13: pos_sampling
def pos_sampling(self, n_steps=50):
"""
Performs `n_steps` of mean-field inference (used to compute positive phase statistics).
:param psamples: list of tensor-like objects, representing the state of each layer of
the DBM (during the inference process). psamples[0] points to self.input.
:param n_steps: number of iterations of mean-field to perform.
"""
new_psamples = [T.unbroadcast(T.shape_padleft(psample)) for psample in self.psamples]
# now alternate mean-field inference for even/odd layers
def sample_iteration(*psamples):
new_psamples = [p for p in psamples]
for i in xrange(1,self.depth,2):
new_psamples[i] = self.sample_hi_given(psamples, i)
for i in xrange(2,self.depth,2):
new_psamples[i] = self.sample_hi_given(psamples, i)
return new_psamples
new_psamples, updates = scan(
sample_iteration,
states = new_psamples,
n_steps=n_steps)
return [x[0] for x in new_psamples]
示例14: _zoom
#.........这里部分代码省略.........
phi_hi,
TT.switch(cond2, phi_hi, phi_lo),
name='phi_rec')
a_rec = ifelse(cond1,
a_hi,
TT.switch(cond2, a_hi, a_lo),
name='a_rec')
a_hi = ifelse(cond1, a_j,
TT.switch(cond2, a_lo, a_hi),
name='a_hi')
phi_hi = ifelse(cond1, phi_aj,
TT.switch(cond2, phi_lo, phi_hi),
name='phi_hi')
a_lo = TT.switch(cond1, a_lo, a_j)
phi_lo = TT.switch(cond1, phi_lo, phi_aj)
derphi_lo = ifelse(cond1, derphi_lo, derphi_aj, name='derphi_lo')
a_star = a_j
val_star = phi_aj
valprime = ifelse(cond1, nan,
TT.switch(cond2, derphi_aj, nan), name='valprime')
return ([phi_rec,
a_rec,
a_lo,
a_hi,
phi_hi,
phi_lo,
derphi_lo,
a_star,
val_star,
valprime],
theano.scan_module.scan_utils.until(stop))
maxiter = n_iters
# cubic interpolant check
delta1 = TT.constant(numpy.asarray(0.2,
dtype=theano.config.floatX))
# quadratic interpolant check
delta2 = TT.constant(numpy.asarray(0.1,
dtype=theano.config.floatX))
phi_rec = phi0
a_rec = zero
# Initial iteration
dalpha = a_hi - a_lo
a = TT.switch(dalpha < zero, a_hi, a_lo)
b = TT.switch(dalpha < zero, a_lo, a_hi)
#a = ifelse(dalpha < 0, a_hi, a_lo)
#b = ifelse(dalpha < 0, a_lo, a_hi)
# minimizer of cubic interpolant
# (uses phi_lo, derphi_lo, phi_hi, and the most recent value of phi)
#
# if the result is too close to the end points (or out of the
# interval) then use quadratic interpolation with phi_lo,
# derphi_lo and phi_hi if the result is stil too close to the
# end points (or out of the interval) then use bisection
# quadric interpolation
qchk = delta2 * dalpha
a_j = _quadmin(a_lo, phi_lo, derphi_lo, a_hi, phi_hi)
cond_q = lazy_or('mcond_q',
TT.isnan(a_j),
示例15: __init__
#.........这里部分代码省略.........
self.W_hhb = theano.shared(self.W_hhb, name='W_hhb')
self.W_hyf = theano.shared(self.W_hyf, name='W_hyf')
self.W_hyb = theano.shared(self.W_hyb, name='W_hyb')
self.b_hhf = theano.shared(self.b_hhf, name='b_hhf')
self.b_hhb = theano.shared(self.b_hhb, name='b_hhb')
self.b_hy = theano.shared(self.b_hy, name='b_hy')
self.params = [self.W_uhf, self.W_uhb, self.W_hhf, self.W_hhb,
self.W_hyf, self.W_hyb, self.b_hhf, self.b_hhb,
self.b_hy]
self.best_params = [(x.name, x.get_value()) for x in self.params]
self.params_shape = [x.get_value(borrow=True).shape for x in
self.params]
# 2. Constructing Theano graph
# Note: new interface of scan asks the user to provide a memory
# buffer that contains the initial state but which is also used
# internally by scan to store the intermediate values of its
# computations - hence the initial state is a 3D tensor
h0_f = TT.alloc(numpy.array(0,dtype=floatX), self.seqlen+1, self.bs,
self.nhids)
h0_b = TT.alloc(numpy.array(0, dtype=floatX), self.seqlen+1, self.bs,
self.nhids)
# Do we use to much memory!?
p_hf = TT.dot(self.x.reshape((self.seqlen*self.bs, self.nins)), self.W_uhf) + self.b_hhf
p_hb = TT.dot(self.x[::-1].reshape((self.seqlen*self.bs, self.nins)), self.W_uhb) + self.b_hhb
def recurrent_fn(pf_t, pb_t, hf_tm1, hb_tm1):
hf_t = activ(TT.dot(hf_tm1, self.W_hhf) + pf_t)
hb_t = activ(TT.dot(hb_tm1, self.W_hhb) + pb_t)
return hf_t, hb_t
# provide sequence length !? is better on GPU
[h_f, h_b], _ = scan(
recurrent_fn,
sequences = [
p_hf.reshape((self.seqlen, self.bs, self.nhids)),
p_hb.reshape((self.seqlen, self.bs, self.nhids))],
states = [h0_f, h0_b],
n_steps = self.seqlen,
name = 'bi-RNN',
profile = 0)
h_b = h_b[::-1]
# Optionally do the max over hidden layer !?
# I'm afraid the semantics for RNN are somewhat different than MLP
y = TT.nnet.softmax(
TT.dot(h_f.reshape((self.seqlen * self.bs+self.bs, self.nhids)), self.W_hyf) + # Check doc flatten
TT.dot(h_b.reshape((self.seqlen * self.bs+self.bs, self.nhids)), self.W_hyb) +
self.b_hy)
my = y.reshape((self.seqlen+1, self.bs, self.nouts)).max(axis=0)
nll = -TT.log(
my[TT.constant(numpy.arange(self.bs)), self.t])
self.train_cost = nll.mean()
self.error = TT.mean(TT.neq(my.argmax(axis=1), self.t) * 100.)
## |-----------------------------
# - Computing metric times a vector efficiently for p(y|x)
# Assume softmax .. we might want sigmoids though
self.Gyvs = lambda *args:\
TT.Lop(y, self.params,
TT.Rop(y, self.params, args) /\
(y*numpy.array(self.bs, dtype=floatX)))
# Computing metric times a vector effciently for p(h|x)
if activ == TT.nnet.sigmoid:
fn = lambda x : (1-x)*x*numpy.array(self.bs, dtype=floatX)
elif activ == TT.tanh:
# Please check formula !!!! It is probably wrong