本文整理汇总了Python中theano.sandbox.scan.scan函数的典型用法代码示例。如果您正苦于以下问题:Python scan函数的具体用法?Python scan怎么用?Python scan使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了scan函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:cc13ny,项目名称:galatea,代码行数:32,代码来源:krylov_lbfgs.py
示例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]
开发者ID:yosinski,项目名称:pylearn2,代码行数:34,代码来源:dbm_metrics.py
示例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
开发者ID:cc13ny,项目名称:galatea,代码行数:60,代码来源:natSGD.py
示例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]
开发者ID:gdesjardins,项目名称:DBM,代码行数:29,代码来源:dbm.py
示例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]
开发者ID:gdesjardins,项目名称:hossrbm,代码行数:31,代码来源:implicit_hossrbm_v05_2.py
示例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
开发者ID:SuperElectric,项目名称:pylearn2,代码行数:58,代码来源:linesearch.py
示例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)
开发者ID:johnarevalo,项目名称:Theano,代码行数:10,代码来源:test_scan.py
示例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)
开发者ID:johnarevalo,项目名称:Theano,代码行数:10,代码来源:test_scan.py
示例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)
开发者ID:johnarevalo,项目名称:Theano,代码行数:15,代码来源:test_scan.py
示例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]
开发者ID:gdesjardins,项目名称:MFNG,代码行数:45,代码来源:lincg.py
示例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)
开发者ID:johnarevalo,项目名称:Theano,代码行数:18,代码来源:test_scan.py
示例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]
开发者ID:LeeEdel,项目名称:theano_optimize,代码行数:36,代码来源:lincg.py
示例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]
开发者ID:gdesjardins,项目名称:DBM,代码行数:24,代码来源:dbm.py
示例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),
开发者ID:EderSantana,项目名称:pylearn2,代码行数:67,代码来源:linesearch.py
示例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
开发者ID:LeonBai,项目名称:lisa_emotiw-1,代码行数:67,代码来源:birnn.py
示例16: scalar_search_wolfe2
#.........这里部分代码省略.........
alpha1.name = 'alpha1'
# This shouldn't happen. Perhaps the increment has slipped below
# machine precision? For now, set the return variables skip the
# useless while loop, and raise warnflag=2 due to possible imprecision.
phi0 = TT.switch(TT.eq(alpha1, zero), old_phi0, phi0)
# I need a lazyif for alpha1 == 0 !!!
phi_a1 = ifelse(TT.eq(alpha1, zero), phi0,
phi(alpha1), name='phi_a1')
phi_a1.name = 'phi_a1'
phi_a0 = phi0
phi_a0.name = 'phi_a0'
derphi_a0 = derphi0
derphi_a0.name = 'derphi_a0'
# Make sure variables are tensors otherwise strange things happen
c1 = TT.as_tensor_variable(c1)
c2 = TT.as_tensor_variable(c2)
maxiter = n_iters
def while_search(alpha0, alpha1, phi_a0, phi_a1, derphi_a0, i_t,
alpha_star, phi_star, derphi_star):
derphi_a1 = derphi(alpha1)
cond1 = TT.bitwise_or(phi_a1 > phi0 + c1 * alpha1 * derphi0,
TT.bitwise_and(phi_a1 >= phi_a0, i_t > zero))
cond2 = abs(derphi_a1) <= -c2 * derphi0
cond3 = derphi_a1 >= zero
alpha_star_c1, phi_star_c1, derphi_star_c1 = \
_zoom(alpha0, alpha1, phi_a0, phi_a1, derphi_a0,
phi, derphi, phi0, derphi0, c1, c2,
profile=profile)
alpha_star_c3, phi_star_c3, derphi_star_c3 = \
_zoom(alpha1, alpha0, phi_a1, phi_a0, derphi_a1, phi,
derphi, phi0, derphi0, c1, c2,
profile=profile)
nw_alpha1 = alpha1 * numpy.asarray(2, dtype=theano.config.floatX)
nw_phi = phi(nw_alpha1)
alpha_star, phi_star, derphi_star = \
ifelse(cond1,
(alpha_star_c1, phi_star_c1, derphi_star_c1),
ifelse(cond2,
(alpha1, phi_a1, derphi_a1),
ifelse(cond3,
(alpha_star_c3, phi_star_c3, derphi_star_c3),
(nw_alpha1, nw_phi, nan),
name='alphastar_c3'),
name='alphastar_c2'),
name='alphastar_c1')
return ([alpha1,
nw_alpha1,
phi_a1,
ifelse(lazy_or('allconds',
cond1,
cond2,
cond3),
phi_a1,
nw_phi,
name='nwphi1'),
ifelse(cond1, derphi_a0, derphi_a1, name='derphi'),
i_t + one,
alpha_star,
phi_star,
derphi_star],
theano.scan_module.scan_utils.until(
lazy_or('until_cond_',
TT.eq(nw_alpha1, zero),
cond1,
cond2,
cond3)))
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)]
states += [TT.unbroadcast(TT.shape_padleft(derphi_a0), 0)]
# i_t
states += [TT.unbroadcast(TT.shape_padleft(zero), 0)]
# alpha_star
states += [TT.unbroadcast(TT.shape_padleft(zero), 0)]
# phi_star
states += [TT.unbroadcast(TT.shape_padleft(zero), 0)]
# derphi_star
states += [TT.unbroadcast(TT.shape_padleft(zero), 0)]
# print 'while_search'
outs, updates = scan(while_search,
states=states,
n_steps=maxiter,
name='while_search',
mode=theano.Mode(linker='cvm_nogc'),
profile=profile)
# print 'done_while_search'
out3 = outs[-3][0]
out2 = outs[-2][0]
out1 = outs[-1][0]
alpha_star, phi_star, derphi_star = \
ifelse(TT.eq(alpha1, zero),
(nan, phi0, nan),
(out3, out2, out1), name='main_alphastar')
return alpha_star, phi_star, phi0, derphi_star
开发者ID:EderSantana,项目名称:pylearn2,代码行数:101,代码来源:linesearch.py
示例17: __init__
#.........这里部分代码省略.........
for shp in model.params_shape]
self.permg = self.rng.permutation(self.grad_batches)
self.permr = self.rng.permutation(self.metric_batches)
self.perme = self.rng.permutation(self.eval_batches)
self.k = 0
self.posg = 0
self.posr = 0
self.pose = 0
# Step 1. Compile function for computing eucledian gradients
# inputs
gbdx = TT.iscalar('grad_batch_idx')
print 'Constructing grad function'
srng = RandomStreams(numpy.random.randint(1e5))
loc_inputs = [x.type() for x in model.inputs]
def grad_step(*args):
idx = TT.cast(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 = safe_clone(model.train_cost, replace=replace)
gs = TT.grad(nw_cost, model.params)
nw_gs = [op + np for op, np in zip(args[1: 1 + n_params], gs)]
return [args[0] + const(1)] + \
nw_gs
ig = [TT.unbroadcast(TT.alloc(const(0), 1, *shp),0)
for shp in model.params_shape]
idx0 = TT.unbroadcast(const([0]),0)
n_steps = options['gbs'] // options['cbs']
rvals, updates = scan(grad_step,
states=[idx0] + ig,
n_steps=n_steps,
name='grad_loop',
profile=options['profile'])
nw_gs = [x[0] / const(n_steps) for x in rvals[1: 1 + n_params]]
# updates
updates.update(dict(zip(self.gs, nw_gs)))
# givens
if options['device'] == 'gpu':
grad_inps = [(x, y[gbdx*options['gbs']:(gbdx+1)*options['gbs']])
for x,y in zip(loc_inputs, shared_data)]
else:
grad_inps = zip(loc_inputs, shared_data)
print 'Compiling grad function'
self.compute_eucledian_gradients = theano.function(
[gbdx],
[],
updates=updates,
givens=dict(grad_inps),
name='compute_eucledian_gradients',
mode=gpu_mode,
on_unused_input='warn',
profile=options['profile'])
# Step 2. Compile function for Computing Riemannian gradients
rbdx = TT.iscalar('riemmanian_batch_idx')
rbpos = rbdx * options['mbs']
if options['device'] == 'gpu':
开发者ID:cc13ny,项目名称:galatea,代码行数:67,代码来源:natSGD.py
示例18: minres
#.........这里部分代码省略.........
TT.ge(xnorm, maxxnorm),
constantX(6),
TT.ge(niter, TT.cast(maxit,
theano.config.floatX)),
constantX(8),
flag),
flag)
flag = TT.switch(TT.lt(Axnorm, rtol * Anorm * xnorm),
constantX(11.),
flag)
return [niter + constantX(1.),
beta,
betan,
phi,
Acond,
cs,
dbarn,
eplnn,
rnorm,
sn,
Tnorm,
rnorml,
xnorm,
Dnorm,
gamma,
pnorm,
gammal,
Axnorm,
relrnorm,
relArnorml,
Anorm,
flag] + xs + r1s + r2s + r3s + dls + ds, upds, \
theano.scan_module.scan_utils.until(TT.neq(flag, 0))
states = []
# 0 niter
states.append(constantX([0]))
# 1 beta
states.append(constantX([0]))
# 2 betan
states.append(TT.unbroadcast(TT.shape_padleft(beta1), 0))
# 3 phi
states.append(TT.unbroadcast(TT.shape_padleft(beta1), 0))
# 4 Acond
states.append(constantX([1]))
# 5 cs
states.append(constantX([-1]))
# 6 dbarn
states.append(constantX([0]))
# 7 eplnn
states.append(constantX([0]))
# 8 rnorm
states.append(TT.unbroadcast(TT.shape_padleft(beta1), 0))
# 9 sn
states.append(constantX([0]))
# 10 Tnorm
states.append(constantX([0]))
# 11 rnorml
states.append(TT.unbroadcast(TT.shape_padleft(beta1), 0))
# 12 xnorm
states.append(constantX([0]))
# 13 Dnorm
states.append(constantX([0]))
# 14 gamma
states.append(constantX([0]))
开发者ID:poolio,项目名称:pylearn,代码行数:67,代码来源:minres.py
示例19: __init__
def __init__(self, options, channel, data, model):
"""
Parameters:
options: Dictionary
`options` is expected to contain the following keys:
`cbs` -> int
Number of samples to consider at a time when computing
some property of the model
`gbs` -> int
Number of samples over which to compute the gradients
`ebs` -> int
Number of samples over which to evaluate the training
error
`seed` -> int
Random number generator seed
`profile` -> bool
Flag, if profiling should be on or not
`verbose` -> int
Verbosity level
`lr` -> float
Learning rate
channel: jobman channel or None
data: dictionary-like object return by numpy.load containing the
data
model : model
"""
self.model = model
# push dataset into shared var
n_params = len(model.params)
xdata = theano.shared(data['train_x'].astype('float32'),
name='xdata')
# ! This works for 1 of k classification
ydata = TT.cast(
theano.shared(data['train_y'].astype('float32'),
name='ydata'), 'int32')
shared_data = [xdata, ydata]
self.xdata = xdata
self.ydata = ydata
# all sorts of indices
self.rng = numpy.random.RandomState(options['seed'])
n_samples = data['train_x'].shape[0]
self.grad_batches = n_samples // options['gbs']
self.metric_batches = n_samples // options['mbs']
self.eval_batches = n_samples // options['ebs']
self.verbose = options['verbose']
# vars for gradients
# Store Euclidean gradients
self.gs = [theano.shared(numpy.zeros(shp, dtype=theano.config.floatX))
for shp in model.params_shape]
# Store riemannian gradients (H^-1*g)
self.rs = [theano.shared(numpy.zeros(shp, dtype=theano.config.floatX))
for shp in model.params_shape]
self.permg = self.rng.permutation(self.grad_batches)
self.permr = self.rng.permutation(self.metric_batches)
self.perme = self.rng.permutation(self.eval_batches)
self.k = 0
self.posg = 0
self.posr = 0
self.pose = 0
# Step 1. Compile function for computing eucledian gradients
gbdx = TT.iscalar('grad_batch_idx')
print 'Constructing grad function'
loc_inputs = [x.type() for x in model.inputs]
def grad_step(*args):
idx = TT.cast(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 = safe_clone(model.train_cost, replace=replace)
gs = TT.grad(nw_cost, model.params)
nw_gs = [op + np for op, np in zip(args[1: 1 + n_params], gs)]
return [args[0] + const(1)] + \
nw_gs
ig = [TT.unbroadcast(TT.alloc(const(0), 1, *shp),0)
for shp in model.params_shape]
idx0 = TT.unbroadcast(const([0]),0)
n_steps = options['gbs'] // options['cbs']
rvals, updates = scan(grad_step,
states=[idx0] + ig,
n_steps=n_steps,
name='grad_loop',
profile=options['profile'])
nw_gs = [x[0] / const(n_steps) for x in rvals[1: 1 + n_params]]
updates.update(dict(zip(self.gs, nw_gs)))
grad_inps = [(x, y[gbdx*options['gbs']:(gbdx+1)*options['gbs']])
for x,y in zip(loc_inputs, shared_data)]
print 'Compiling grad function'
self.compute_eucledian_gradients = theano.function(
[gbdx],
[],
updates=updates,
givens=dict(grad_inps),
on_unused_input='warn',
#.........这里部分代码省略.........
开发者ID:cc13ny,项目名称:galatea,代码行数:101,代码来源:SGD.py
示例20: minres
#.........这里部分代码省略.........
npy_floatX(5.),
TT.switch(TT.ge(xnorm, maxxnorm),
npy_floatX(6.),
TT.switch(TT.ge(niter, TT.cast(maxiter,floatX)),
npy_floatX(8.),
flag)))))))),
flag)
flag = TT.switch(TT.lt(Axnorm, rtol*Anor
|
请发表评论