def get_output_for(self, inputs, **kwargs):
unary, ref = inputs
N, _, H, W = ref.shape
yx = tt.cast(tt.stack(tt.mgrid[0:H, 0:W]), "float32")
grid = tt.alloc(yx[np.newaxis, :, :, :], N, 2, H, W)
stacked = tt.concatenate([grid, ref], axis=1)
def _bilateral(V, R):
o = tt.ones((1, V.shape[1], V.shape[2]), "float32")
norm = tt.sqrt(gaussian_filter(R, o, self.kstd_bf,
self.ref_dim)) + 1e-8
return gaussian_filter(R, V/norm, self.kstd_bf, self.ref_dim,
self.val_dim) / norm
def _step(prev_q, U, ref, normalize=True):
qbf = _bilateral(prev_q, ref,)
qsf = tt.nnet.conv2d(prev_q[np.newaxis, :, :, :],
self.W_spatial, border_mode="half")[0]
q_hat = -self.compat_bf * qbf + -self.compat_spatial * qsf
q_hat = U - q_hat
return softmax(q_hat, axis=0) if normalize else q_hat
def _inference(unary_i, ref_i):
U = tt.log(tt.clip(unary_i, 1e-5, 1))
prev_q = softmax(U, axis=0)
# This is faster than using scan.
for i in range(self.num_iter):
normalize = self.normalize_final_iter or i < self.num_iter-1
prev_q = _step(prev_q, U, ref_i, normalize)
return prev_q
return theano.scan(fn=_inference, sequences=[unary, stacked],
outputs_info=None)[0]
python类alloc()的实例源码
def sample(self, x0=None, h0=None, c0=None, n_samples=10, n_steps=10,
condition_on=None, debug=False):
if x0 is None:
x0, _ = self.output_net.sample(
p=T.constant(0.5).astype(floatX),
size=(n_samples, self.output_net.dim_out)).astype(floatX)
if h0 is None:
h0 = T.alloc(0., x0.shape[0], self.dim_h).astype(floatX)
if c0 is None:
c0 = T.alloc(0., x0.shape[0], self.dim_h).astype(floatX)
z0 = self.output_net.preact(h0)
seqs = []
outputs_info = [h0, c0, x0, None]
non_seqs = []
step = self.step_sample
p0 = self.output_net.distribution(z0)
non_seqs += self.get_sample_params()
if debug:
return self.step_sample(h0, x0, *self.get_sample_params())
outs = scan(step, seqs, outputs_info, non_seqs, n_steps,
name=self.name+'_sampling', strict=False)
(h, c, x, p), updates = outs
x = concatenate([x0[None, :, :], x])
h = concatenate([h0[None, :, :], h])
p = concatenate([p0[None, :, :], p])
return OrderedDict(x=x, p=p, h=h, x0=x0, p0=p0, h0=h0), updates
def __call__(self, x, h0=None, c0=None, condition_on=None):
if h0 is None:
h0 = T.alloc(0., x.shape[1], self.dim_h).astype(floatX)
if c0 is None:
c0 = T.alloc(0., x.shape[1], self.dim_h).astype(floatX)
params = self.get_sample_params()
return self.step_call(x, h0, c0, condition_on, *params)
def test_recurrent(dim_in=13, dim_h=17, n_samples=107, window=7):
rnn = test_build(dim_in, dim_h)
data_iter = Euclidean(n_samples=n_samples, dims=dim_in, batch_size=window)
x = data_iter.next()[data_iter.name]
test_dict = OrderedDict()
X = T.matrix('x', dtype=floatX)
Y = rnn.call_seqs(X, None, 0, *rnn.get_sample_params())[0]
y = np.dot(x, rnn.input_net.params['W0']) + rnn.input_net.params['b0']
test_dict['RNN preact from data'] = (X, Y, x, y, theano.OrderedUpdates())
H0 = T.alloc(0., X.shape[0], rnn.dim_hs[0]).astype(floatX)
H = rnn._step(1, Y, H0, rnn.Ur0)
h0 = np.zeros((x.shape[0], rnn.dim_hs[0])).astype(floatX)
h = np.tanh(np.dot(h0, rnn.params['Ur0']) + y)
test_dict['step reccurent'] = (X, H, x, h, theano.OrderedUpdates())
P = rnn.output_net.feed(H)
p = sigmoid(np.dot(h, rnn.output_net.params['W0']) + rnn.output_net.params['b0'])
test_dict['output'] = (X, P, x, p, theano.OrderedUpdates())
for k, v in test_dict.iteritems():
print 'Testing %s' % k
inp, out, inp_np, out_np, updates = v
f = theano.function([inp], out, updates=updates)
out_actual = f(inp_np)
if not np.allclose(out_np, out_actual):
print 'np', out_np
print 'theano', out_actual
assert False
def init_h(h_init, X, batch_size, models, **h_args):
'''Initializes the RNN hidden state.
Args:
h_init (str): type of initialization.
X (T.tensor): input tensor for initialization through MLP.
batch_size (int)
models (list): list of Layer, pulls 'h_net' for initialization
(TODO change this).
**h_args: kwargs for different initializations.
Returns:
T.tensor: Full 3D tensor returned to train `h_net`.
'''
if h_init is None:
h0 = None
elif h_init == 'last':
print 'Initializing h0 from chain'
h0 = theano.shared(np.zeros((batch_size, rnn.dim_h)).astype(floatX))
h0s = h0[None, :, :]
elif h_init == 'noise':
noise_amount = h_args['noise_amount']
print 'Initializing h0 from noise'
h0 = trng.normal(avg=0, std=0.1, size=(batch_size, rnn.dim_h)).astype(floatX)
h0s = h0[None, :, :]
elif h_init == 'average':
print 'Initializing h0 from running average'
averager = models['averager']
h0 = (T.alloc(0., batch_size, rnn.dim_h) + averager.m[None, :]).astype(floatX)
h0s = h0[None, :, :]
elif h_init == 'mlp':
print 'Initializing h0 from MLP'
mlp = models['h_net']
h0s = mlp(X)
return h0s
def __call__(self, x, m=None, h0s=None, condition_on=None):
'''Call function.
For learning RNNs.
Args:
x (T.tensor): input sequence. window x batch x dim
m (T.tensor): mask. window x batch. For masking in recurrent steps.
h0s (Optional[list]): initial h0s.
condition_on (Optional[T.tensor]): conditional for recurrent step.
Returns:
OrderedDict: dictionary of results: hiddens, probabilities, and
preacts.
theano.OrderedUpdates.
'''
if h0s is None:
h0s = [T.alloc(0., x.shape[1], dim_h).astype(floatX) for dim_h in self.dim_hs]
if m is None:
m = T.ones((x.shape[0], x.shape[1])).astype(floatX)
params = self.get_sample_params()
return self.step_call(x, m, h0s, *params)
def ctc_interleave_blanks(Y):
Y_ = T.alloc(-1, Y.shape[0] * 2 + 1)
Y_ = T.set_subtensor(Y_[T.arange(Y.shape[0]) * 2 + 1], Y)
return Y_
def fprop(self, all_states):
if self.ntimes:
stateshape0 = all_states.shape[0]
shape0 = TT.switch(TT.gt(self.n, 0), self.n, all_states.shape[0])
single_frame = TT.shape_padleft(all_states[stateshape0-1])
mask = TT.alloc(numpy.float32(1), shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
single_frame = all_states[all_states.shape[0]-1]
self.out = single_frame
return single_frame
def fprop(self, all_states):
shape0 = all_states.shape[0]
single_frame = all_states.min(0)
if self.ntimes:
single_frame = TT.shape_padleft(all_states.max(0))
mask = TT.alloc(numpy.float32(1),
shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
self.out = single_frame
return single_frame
def fprop(self, all_states):
shape0 = all_states.shape[0]
single_frame = all_states.max(0)
if self.ntimes:
single_frame = TT.shape_padleft(all_states.max(0))
mask = TT.alloc(numpy.float32(1),
shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
self.out = single_frame
return single_frame
def fprop(self, all_states):
if self.ntimes:
stateshape0 = all_states.shape[0]
shape0 = TT.switch(TT.gt(self.n, 0), self.n, all_states.shape[0])
single_frame = TT.shape_padleft(all_states[stateshape0-1])
mask = TT.alloc(numpy.float32(1), shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
single_frame = all_states[all_states.shape[0]-1]
self.out = single_frame
return single_frame
def fprop(self, all_states):
shape0 = all_states.shape[0]
single_frame = all_states.min(0)
if self.ntimes:
single_frame = TT.shape_padleft(all_states.max(0))
mask = TT.alloc(numpy.float32(1),
shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
self.out = single_frame
return single_frame
def fprop(self, all_states):
shape0 = all_states.shape[0]
single_frame = all_states.max(0)
if self.ntimes:
single_frame = TT.shape_padleft(all_states.max(0))
mask = TT.alloc(numpy.float32(1),
shape0, *[1 for k in xrange(all_states.ndim-1)])
rval = single_frame * mask
self.out = rval
return rval
self.out = single_frame
return single_frame
def fprop(self, x):
# This is black magic based on broadcasting,
# that's why variable names don't make any sense.
a = TT.shape_padleft(x)
padding = [1] * x.ndim
b = TT.alloc(numpy.float32(1), self.n_times, *padding)
self.out = a * b
return self.out
def forward(self, inputtensor):
x = inputtensor[0]
input_shape = x.shape
ch = input_shape[1]
bs = input_shape[0]
half_n = self.localSize // 2
input_sqr = T.sqr(x)
extra_channels = T.alloc(0., bs, ch + 2*half_n, *input_shape[2:])
input_sqr = T.set_subtensor(extra_channels[:, half_n:half_n+ch, :, :]
, input_sqr)
scale = 1
for i in range(self.localSize):
scale += self.alpha/self.localSize * input_sqr[:, i:i+ch, ...]
scale = scale ** self.beta
return (x/scale, )
def alloc_zeros_matrix(*dims):
return T.alloc(np.cast[theano.config.floatX](0.), *dims)
def alloc_ones_matrix(*dims):
return T.alloc(np.cast[theano.config.floatX](1.), *dims)
def set_output(self):
if sum(self._padding) > 0:
padded_input = tensor.alloc(0.0, # Value to fill the tensor
self._input_shape[0],
self._input_shape[1],
self._input_shape[2] + 2 * self._padding[2],
self._input_shape[3] + 2 * self._padding[3])
padded_input = tensor.set_subtensor(
padded_input[:, :, self._padding[2]:self._padding[2] + self._input_shape[2],
self._padding[3]:self._padding[3] + self._input_shape[3]],
self._prev_layer.output)
padded_input_shape = [self._input_shape[0], self._input_shape[1],
self._input_shape[2] + 2 * self._padding[2],
self._input_shape[3] + 2 * self._padding[3]]
else:
padded_input = self._prev_layer.output
padded_input_shape = self._input_shape
conv_out = conv.conv2d(
input=padded_input,
filters=self.W.val,
filter_shape=self._filter_shape,
image_shape=np.asarray(
padded_input_shape, dtype=np.int16),
border_mode='valid')
# add the bias term. Since the bias is a vector (1D array), we first
# reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will
# thus be broadcasted across mini-batches and feature map
# width & height
self._output = conv_out + self.b.val.dimshuffle('x', 0, 'x', 'x')
def ctc_interleave_blanks(Y):
Y_ = T.alloc(-1, Y.shape[0] * 2 + 1)
Y_ = T.set_subtensor(Y_[T.arange(Y.shape[0]) * 2 + 1], Y)
return Y_
def alloc(self, value, shape, dtype):
return T.alloc(value, *tuple(shape), dtype=dtype)