def set_output(self):
self._output = tensor.ones_like(self._prev_layer.output) - self._prev_layer.output
python类ones_like()的实例源码
def ones_like(self, x):
'''Instantiates an all-ones tensor of the same shape as another tensor.
'''
return T.ones_like(x)
def ones_like(x, dtype=None, name=None):
return T.ones_like(x, dtype=dtype)
def oneslike(self, t, dtype):
return T.ones_like(t, dtype)
def _create_components(self, deterministic=False):
# load network input
X = self.inputs[0]
x = X.flatten(2)
# load networks
l_p_mu, l_q_mu, l_q_sample, _, _, _ = self.network
# load network output
z, q_mu = lasagne.layers.get_output([l_q_sample, l_q_mu], deterministic=deterministic)
p_mu = lasagne.layers.get_output(l_p_mu, z, deterministic=deterministic)
# entropy term
log_qz_given_x = log_bernoulli(dg(z), q_mu).sum(axis=1)
# expected p(x,z) term
z_prior = T.ones_like(z)*np.float32(0.5)
log_pz = log_bernoulli(z, z_prior).sum(axis=1)
log_px_given_z = log_bernoulli(x, p_mu).sum(axis=1)
log_pxz = log_pz + log_px_given_z
# save them for later
self.log_pxz = log_pxz
self.log_qz_given_x = log_qz_given_x
return log_pxz.flatten(), log_qz_given_x.flatten()
def _forward_step(self, x_t, s_t):
"""Input vector/matrix x(t) and state matrix s(t)."""
# Gradient clipping
E, a, U, W, b, V, c = [ th.gradient.grad_clip(p, -5.0, 5.0) for p in self.params.values() ]
# Initialize state to return
s_next = T.zeros_like(s_t)
# Vocab-to-state encoding layer
inout = T.tanh(T.dot(x_t, E) + a)
# Loop over GRU layers
for layer in range(self.hyper.layers):
# 3 matrices per layer
L = layer * 3
# Get previous state for this layer
s_prev = s_t[layer]
# Update gate
z = T.nnet.hard_sigmoid(T.dot(inout, U[L]) + T.dot(s_prev, W[L]) + b[L])
# Reset gate
r = T.nnet.hard_sigmoid(T.dot(inout, U[L+1]) + T.dot(s_prev, W[L+1]) + b[L+1])
# Candidate state
h = T.tanh(T.dot(inout, U[L+2]) + T.dot(r * s_prev, W[L+2]) + b[L+2])
# New state
s_new = (T.ones_like(z) - z) * h + z * s_prev
s_next = T.set_subtensor(s_next[layer], s_new)
# Update for next layer or final output (might add dropout here later)
inout = s_new
# Final output
o_t = T.dot(inout, V) + c
return o_t, s_next
# Regularization cost
def ones_like(x):
return T.ones_like(x, dtype=theano.config.floatX)
def get_padded_shuffled_mask(self, train, X, pad=0):
mask = self.get_input_mask(train)
if mask is None:
mask = T.ones_like(X.sum(axis=-1)) # is there a better way to do this without a sum?
# mask is (nb_samples, time)
mask = T.shape_padright(mask) # (nb_samples, time, 1)
mask = T.addbroadcast(mask, -1) # (time, nb_samples, 1) matrix.
mask = mask.dimshuffle(1, 0, 2) # (time, nb_samples, 1)
if pad > 0:
# left-pad in time with 0
padding = alloc_zeros_matrix(pad, mask.shape[1], 1)
mask = T.concatenate([padding, mask], axis=0)
return mask.astype('int8')
def get_output_mask(self, train=None):
X = self.get_input(train)
if not self.mask_zero:
return None
else:
return T.ones_like(X) * (1 - T.eq(X,0))
def ones_like(x, name=None):
return T.ones_like(x)
def ones_like(x):
return T.ones_like(x)
def apply(self, x):
if self.mode is 'normal' or x.ndim == 2:
shape = x.shape
dropout_mask = self.rng_theano.binomial(x.shape, p=self.p, dtype='float32') / self.p
elif x.ndim == 4 or x.ndim == 5:
# spatial dropout, meaning drop a whole feature map
shape = (x.shape[x.ndim-3],)
pattern = ('x',) * (x.ndim-3) + (0,) + ('x','x',)
dropout_feature_mask = self.rng_theano.binomial(shape, p=self.p, dtype='float32') / self.p
dropout_mask = T.ones_like(x) * dropout_feature_mask.dimshuffle(*pattern) / self.p
return x
def best_path_decoding(self, probs, probs_mask=None):
# probs is T x B x C+1
T = probs.shape[0]
B = probs.shape[1]
C = probs.shape[2]-1
maxprob = probs.argmax(axis=2)
is_double = tensor.eq(maxprob[:-1], maxprob[1:])
maxprob = tensor.switch(tensor.concatenate([tensor.zeros((1,B)), is_double]),
C*tensor.ones_like(maxprob), maxprob)
# maxprob = theano.printing.Print('maxprob')(maxprob.T).T
# returns two values :
# label : (T x) T x B
# label_length : (T x) B
def recursion(maxp, p_mask, label_length, label):
nonzero = p_mask * tensor.neq(maxp, C)
nonzero_id = nonzero.nonzero()[0]
new_label = tensor.set_subtensor(label[label_length[nonzero_id], nonzero_id], maxp[nonzero_id])
new_label_length = tensor.switch(nonzero, label_length + numpy.int32(1), label_length)
return new_label_length, new_label
[label_length, label], _ = scan(fn=recursion,
sequences=[maxprob, probs_mask],
outputs_info=[tensor.zeros((B,),dtype='int32'),-tensor.ones((T,B))])
return label[-1], label_length[-1]
def build(self):
state_pre=T.zeros((self.x.shape[-1],self.n_hidden),dtype=theano.config.floatX)
def _recurrence(x_t,m,h_tm1):
x_e=self.E[x_t,:]
concated=T.concatenate([x_e,h_tm1],axis=1)
# Update gate
z_t=self.f(T.dot(concated,self.Wz) + self.bz )
# Input fate
r_t=self.f(T.dot(concated,self.Wr) + self.br )
# Cell update
c_t=T.tanh(T.dot(x_e,self.Wxc)+T.dot(r_t*h_tm1,self.Whc)+self.bc)
# Hidden state
h_t=(T.ones_like(z_t)-z_t) * c_t + z_t * h_tm1
# masking
h_t=h_t*m[:,None]
return h_t
h,_=theano.scan(fn=_recurrence,
sequences=[self.x,self.mask],
outputs_info=state_pre,
truncate_gradient=self.bptt)
# Dropout
if self.p>0:
drop_mask=self.rng.binomial(n=1,p=1-self.p,size=h.shape,dtype=theano.config.floatX)
self.activation=T.switch(self.is_train,h*drop_mask,h*(1-self.p))
else:
self.activation=T.switch(self.is_train,h,h)
def build(self):
state_pre = T.zeros((self.x.shape[-1], self.n_hidden), dtype=theano.config.floatX)
state_below = T.dot(self.E[self.x,:], self.W) + self.b
state_belowx = T.dot(self.E[self.x,:], self.Wx) + self.bx
def split(x, n, dim):
if x.ndim == 3:
return x[:, :, n * dim: (n + 1) * dim]
return x[:, n * dim:(n + 1) * dim]
def _recurrence(x_t, xx_t, m, h_tm1):
preact = x_t + T.dot(h_tm1, self.U)
# reset fate
r_t = self.f(split(preact, 0, self.n_hidden))
# Update gate
z_t = self.f(split(preact, 1, self.n_hidden))
# Cell update
c_t = T.tanh(T.dot(h_tm1, self.Ux) * r_t + xx_t)
# Hidden state
h_t = (T.ones_like(z_t) - z_t) * c_t + z_t * h_tm1
# masking
h_t = h_t * m[:, None]
return h_t
h, _ = theano.scan(fn=_recurrence,
sequences=[state_below, state_belowx, self.mask],
outputs_info=state_pre,
truncate_gradient=self.bptt)
# Dropout
if self.p > 0:
drop_mask = self.rng.binomial(n=1, p=1 - self.p, size=h.shape, dtype=theano.config.floatX)
self.activation = T.switch(self.is_train, h * drop_mask, h * (1 - self.p))
else:
self.activation = T.switch(self.is_train, h, h)
def build(self):
state_pre=T.zeros((self.n_batch,self.n_hidden),dtype=theano.config.floatX)
def _recurrence(x_t,m,h_tm1):
x_e=self.E[x_t,:]
concated=T.concatenate([x_e,h_tm1],axis=-1)
# Update gate
z_t=T.nnet.sigmoid(T.dot(concated,self.Wz) + self.bz )
# Input fate
r_t=T.nnet.sigmoid(T.dot(concated,self.Wr) + self.br )
# Cell update
c_t=T.tanh(T.dot(x_e,self.Wxc)+T.dot(r_t*h_tm1,self.Whc)+self.bc)
# Hidden state
h_t=(T.ones_like(z_t)-z_t) * c_t + z_t * h_tm1
# masking
h_t=h_t*m[:,None]#+(1.-m)[:,None]*h_t
return h_t
h,_=theano.scan(fn=_recurrence,
sequences=[self.x,self.mask],
outputs_info=[dict(initial=T.zeros((self.n_batch,self.n_hidden) )),])
# Dropout
if self.p>0:
drop_mask=self.rng.binomial(n=1,p=1-self.p,size=h.shape,dtype=theano.config.floatX)
self.activation=T.switch(T.eq(self.is_train,1),h*drop_mask,h*(1-self.p))
else:
self.activation=T.switch(T.eq(self.is_train,1),h,h)
def ones_like(x, dtype=None, name=None):
return T.ones_like(x, dtype=dtype)
def ones_like(x, dtype=None, name=None):
return T.ones_like(x, dtype=dtype)
def test_scan(self):
"""
Test the compute_test_value mechanism Scan.
"""
orig_compute_test_value = theano.config.compute_test_value
try:
theano.config.compute_test_value = 'raise'
# theano.config.compute_test_value = 'warn'
k = T.iscalar("k")
A = T.vector("A")
k.tag.test_value = 3
A.tag.test_value = numpy.random.rand(5).astype(config.floatX)
def fx(prior_result, A):
return prior_result * A
# Symbolic description of the result
result, updates = theano.scan(fn=fx,
outputs_info=T.ones_like(A),
non_sequences=A,
n_steps=k)
# We only care about A**k, but scan has provided us with A**1 through A**k.
# Discard the values that we don't care about. Scan is smart enough to
# notice this and not waste memory saving them.
final_result = result[-1]
assert hasattr(final_result.tag, 'test_value')
finally:
theano.config.compute_test_value = orig_compute_test_value
def test_scan_err1(self):
# This test should fail when building fx for the first time
orig_compute_test_value = theano.config.compute_test_value
try:
theano.config.compute_test_value = 'raise'
k = T.iscalar("k")
A = T.matrix("A")
k.tag.test_value = 3
A.tag.test_value = numpy.random.rand(5, 3).astype(config.floatX)
def fx(prior_result, A):
return T.dot(prior_result, A)
# Since we have to inspect the traceback,
# we cannot simply use self.assertRaises()
try:
theano.scan(
fn=fx,
outputs_info=T.ones_like(A),
non_sequences=A,
n_steps=k)
assert False
except ValueError:
# Get traceback
tb = sys.exc_info()[2]
# Get frame info 4 layers up
frame_info = traceback.extract_tb(tb)[-5]
# We should be in the "fx" function defined above
expected = 'test_compute_test_value.py'
assert os.path.split(frame_info[0])[1] == expected, frame_info
assert frame_info[2] == 'fx'
finally:
theano.config.compute_test_value = orig_compute_test_value