本文整理汇总了Python中theano.sandbox.cuda.rng_curand.CURAND_RandomStreams.uniform方法的典型用法代码示例。如果您正苦于以下问题:Python CURAND_RandomStreams.uniform方法的具体用法?Python CURAND_RandomStreams.uniform怎么用?Python CURAND_RandomStreams.uniform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.sandbox.cuda.rng_curand.CURAND_RandomStreams
的用法示例。
在下文中一共展示了CURAND_RandomStreams.uniform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compare_speed
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
def compare_speed():
# To run this speed comparison
# cd <directory of this file>
# THEANO_FLAGS=device=gpu \
# python -c 'import test_rng_curand; test_rng_curand.compare_speed()'
mrg = MRG_RandomStreams()
crn = CURAND_RandomStreams(234)
N = 1000 * 100
dest = theano.shared(numpy.zeros(N, dtype=theano.config.floatX))
mrg_u = theano.function([], [], updates={dest: mrg.uniform((N,))},
profile='mrg uniform')
crn_u = theano.function([], [], updates={dest: crn.uniform((N,))},
profile='crn uniform')
mrg_n = theano.function([], [], updates={dest: mrg.normal((N,))},
profile='mrg normal')
crn_n = theano.function([], [], updates={dest: crn.normal((N,))},
profile='crn normal')
for f in mrg_u, crn_u, mrg_n, crn_n:
# don't time the first call, it has some startup cost
print('DEBUGPRINT')
print('----------')
theano.printing.debugprint(f)
for i in range(100):
for f in mrg_u, crn_u, mrg_n, crn_n:
# don't time the first call, it has some startup cost
f.fn.time_thunks = (i > 0)
f()
示例2: check_uniform_basic
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
def check_uniform_basic(shape_as_symbolic, dim_as_symbolic=False):
"""
check_uniform_basic(shape_as_symbolic, dim_as_symbolic=False)
Runs a basic sanity check on the `uniform` method of a
`CURAND_RandomStreams` object.
Checks that variates
* are in the range [0, 1]
* have a mean in the right neighbourhood (near 0.5)
* are of the specified shape
* successive calls produce different arrays of variates
Parameters
----------
shape_as_symbolic : boolean
If `True`, est the case that the shape tuple is a symbolic
variable rather than known at compile-time.
dim_as_symbolic : boolean
If `True`, test the case that an element of the shape
tuple is a Theano symbolic. Irrelevant if `shape_as_symbolic`
is `True`.
"""
rng = CURAND_RandomStreams(234)
if shape_as_symbolic:
# instantiate a TensorConstant with the value (10, 10)
shape = constant((10, 10))
else:
# Only one dimension is symbolic, with the others known
if dim_as_symbolic:
shape = (10, constant(10))
else:
shape = (10, 10)
u0 = rng.uniform(shape)
u1 = rng.uniform(shape)
f0 = theano.function([], u0, mode=mode_with_gpu)
f1 = theano.function([], u1, mode=mode_with_gpu)
v0list = [f0() for i in range(3)]
v1list = [f1() for i in range(3)]
# print v0list
# print v1list
# assert that elements are different in a few ways
assert numpy.all(v0list[0] != v0list[1])
assert numpy.all(v1list[0] != v1list[1])
assert numpy.all(v0list[0] != v1list[0])
for v in v0list:
assert v.shape == (10, 10)
assert v.min() >= 0
assert v.max() <= 1
assert v.min() < v.max()
assert .25 <= v.mean() <= .75
示例3: Training
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class Training(Layer):
def __init__(self,rng, W=None,m=1.0, n_samples=50,shape=None,batch_size=1000):
if W is None:
W = numpy.asarray(rng.uniform(
low=-numpy.sqrt(6. / (shape[0] + shape[1])),
high=numpy.sqrt(6. / (shape[0] + shape[1])),
size=(shape[0], shape[1])), dtype=theano.config.floatX)
self.W = theano.shared(value=W, name='Hashtag_emb', borrow=True)
self.batch_size = batch_size
self.n_ht = W.shape[0]
self.m = m
self.n_samples = n_samples
self.csrng = CURAND_RandomStreams(123)
mask = self.csrng.uniform(size=(self.n_samples,1),low=0.0,high=1.0,dtype=theano.config.floatX)
self.rfun = theano.function([],mask.argsort(axis=0))
self.alpha = T.constant(1.0/numpy.arange(start=1,stop=self.n_ht + 1,step=1))
self.weights = [self.W]
self.biases = []
def __repr__(self):
return "{}: W_shape: {}, m={}, n_samples={}, n_ht={}".format(self.__class__.__name__, self.W.shape.eval(),self.m,self.n_samples,self.n_ht)
def output_func(self, input):
self.f = T.tensordot(input.dimshuffle(0,'x',1),self.W.dimshuffle('x',0,1),axes=[[1,2],[0,2]]) # cosine sim
self.y_pred = T.argmax(self.f,axis=0)
return self.y_pred
def get_tag_neg(self,f,f_y):
cand = f[(f > f_y - self.m).nonzero()]
rnk =cand.shape[0] - 1# due to i != y
if rnk == 0:
return 0
l = T.sum(self.alpha[T.arange(rnk)])
return l/rnk
def _warp_loss_cost(self, y,i):
f_y = self.f[T.arange(y.shape[0]), y]
s = self.m - f_y + self.f[T.arange(i.shape[0]),i]
return T.maximum(0.0,s)
def warp_loss_cost(self, y, idx):
f_y = self.f[T.arange(y.shape[0]), y]
f_yy = T.repeat(f_y.dimshuffle(0,'x'),self.f.shape[1],axis=1)
f_idx = T.maximum(0.0,f_yy - self.f + self.m)
idx = f_idx.argsort(axis=1)[:,0]
s = self.m - f_y + self.f[T.arange(idx.shape[0]),idx]
return T.maximum(0.0,s)
def training_cost(self, y,i):
return T.mean(self.warp_loss_cost(y,i))
示例4: HiddenLayer
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
W_shape = (self.in_chans, self.out_chans)
if self.W_scale == 'xg':
W_np = glorot_matrix(W_shape)
else:
#W_np = (self.W_scale * (1.0 / np.sqrt(self.in_chans))) * \
# npr.normal(0.0, 1.0, W_shape)
W_np = ortho_matrix(shape=W_shape, gain=self.W_scale)
W_np = W_np.astype(theano.config.floatX)
W = theano.shared(value=W_np, name="{0:s}_W".format(self.name))
if b is None:
b_np = np.zeros((self.out_chans,), dtype=theano.config.floatX)
b = theano.shared(value=b_np, name="{0:s}_b".format(self.name))
# setup scale and bias params for after batch normalization
if b_in is None:
# batch normalization reshifts are initialized to zero
ary = np.zeros((self.out_chans,), dtype=theano.config.floatX)
b_in = theano.shared(value=ary, name="{0:s}_b_in".format(self.name))
if s_in is None:
# batch normalization rescales are initialized to zero
ary = np.zeros((self.out_chans,), dtype=theano.config.floatX)
s_in = theano.shared(value=ary, name="{0:s}_s_in".format(self.name))
return W, b, b_in, s_in
def _init_conv_params(self, W=None, b=None, b_in=None, s_in=None):
"""
Initialize all parameters that may be required for feedforward through
a convolutional hidden layer.
"""
if W is None:
W_shape = (self.out_chans, self.in_chans, self.filt_dim, self.filt_dim)
ary = npr.normal(0.0, self.W_scale*0.02, W_shape).astype(theano.config.floatX)
W = theano.shared(value=ary, name="{0:s}_W".format(self.name))
if b is None:
b_shape = (self.out_chans,)
ary = npr.normal(0.0, 0.01, b_shape).astype(theano.config.floatX)
b = theano.shared(value=ary, name="{0:s}_b".format(self.name))
# setup scale and bias params for after batch normalization
if b_in is None:
# batch normalization reshifts are initialized to zero
ary = np.zeros((self.out_chans,), dtype=theano.config.floatX)
b_in = theano.shared(value=ary, name="{0:s}_b_in".format(self.name))
if s_in is None:
# batch normalization rescales are initialized to zero
ary = np.zeros((self.out_chans,), dtype=theano.config.floatX)
s_in = theano.shared(value=ary, name="{0:s}_s_in".format(self.name))
return W, b, b_in, s_in
def apply(self, input, use_drop=False):
"""
Apply feedforward to this input, returning several partial results.
"""
# Reshape input if a reshape command was provided
if not (self.shape_func_in is None):
input = self.shape_func_in(input)
# Apply masking noise to the input (if desired)
if use_drop:
input = self._drop_from_input(input, self.drop_rate)
if self.layer_type == 'fc':
# Feedforward through fully-connected layer
linear_output = T.dot(input, self.W) + self.b
elif self.layer_type == 'conv':
# Feedforward through convolutional layer, with adjustable stride
bm = int((self.filt_dim - 1) / 2) # use "same" mode convolutions
if self.conv_stride == 'double':
linear_output = dnn_conv(input, self.W, subsample=(2, 2),
border_mode=(bm, bm))
elif self.conv_stride == 'single':
linear_output = dnn_conv(input, self.W, subsample=(1, 1),
border_mode=(bm, bm))
elif self.conv_stride == 'half':
linear_output = deconv(input, self.W, subsample=(2, 2),
border_mode=(bm, bm))
else:
assert False, "Unknown stride type!"
linear_output = linear_output + self.b.dimshuffle('x',0,'x','x')
else:
assert False, "Unknown layer type!"
# Apply batch normalization if desired
if self.apply_bn:
linear_output = batchnorm(linear_output, rescale=self.s_in,
reshift=self.b_in, u=None, s=None)
# Apply activation function
final_output = self.activation(linear_output)
# Reshape output if a reshape command was provided
if not (self.shape_func_out is None):
linear_output = self.shape_func_out(linear_output)
final_output = self.shape_func_out(final_output)
return final_output, linear_output
def _drop_from_input(self, input, p):
"""p is the probability of dropping elements of input."""
# get a drop mask that drops things with probability p
drop_rnd = self.rng.uniform(size=input.shape, low=0.0, high=1.0, \
dtype=theano.config.floatX)
drop_mask = drop_rnd > p
# get a scaling factor to keep expectations fixed after droppage
drop_scale = 1. / (1. - p)
# apply dropout mask and rescaling factor to the input
droppy_input = drop_scale * input * drop_mask
return droppy_input
示例5: MultiStageModel
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
# this weight balances l1 vs. l2 penalty on posterior KLds
self.lam_kld_l1l2 = theano.shared(value=zero_ary, name='msm_lam_kld_l1l2')
self.set_lam_kld_l1l2(1.0)
if self.shared_param_dicts is None:
# initialize "optimizable" parameters specific to this MSM
init_vec = to_fX( np.zeros((self.z_dim,)) )
self.p_z_mean = theano.shared(value=init_vec, name='msm_p_z_mean')
self.p_z_logvar = theano.shared(value=init_vec, name='msm_p_z_logvar')
init_vec = to_fX( np.zeros((self.obs_dim,)) )
self.obs_logvar = theano.shared(value=zero_ary, name='msm_obs_logvar')
self.bounded_logvar = 8.0 * T.tanh((1.0/8.0) * self.obs_logvar)
self.shared_param_dicts = {}
self.shared_param_dicts['p_z_mean'] = self.p_z_mean
self.shared_param_dicts['p_z_logvar'] = self.p_z_logvar
self.shared_param_dicts['obs_logvar'] = self.obs_logvar
else:
self.p_z_mean = self.shared_param_dicts['p_z_mean']
self.p_z_logvar = self.shared_param_dicts['p_z_logvar']
self.obs_logvar = self.shared_param_dicts['obs_logvar']
self.bounded_logvar = 8.0 * T.tanh((1.0/8.0) * self.obs_logvar)
# setup a function for computing reconstruction log likelihood
if self.x_type == 'bernoulli':
self.log_prob_func = lambda xo, xh: \
(-1.0 * log_prob_bernoulli(xo, xh))
else:
self.log_prob_func = lambda xo, xh: \
(-1.0 * log_prob_gaussian2(xo, xh, \
log_vars=self.bounded_logvar))
# get a drop mask that drops things with probability p
drop_scale = 1. / (1. - self.drop_rate[0])
drop_rnd = self.rng.uniform(size=self.x_out.shape, \
low=0.0, high=1.0, dtype=theano.config.floatX)
drop_mask = drop_scale * (drop_rnd > self.drop_rate[0])
#############################
# Setup self.z and self.s0. #
#############################
print("Building MSM step 0...")
drop_x = drop_mask * self.x_in
self.q_z_mean, self.q_z_logvar, self.z = \
self.q_z_given_x.apply(drop_x, do_samples=True)
# get initial observation state
self.s0, _ = self.p_s0_given_z.apply(self.z, do_samples=False)
# gather KLd and NLL for the initialization step
self.init_klds = gaussian_kld(self.q_z_mean, self.q_z_logvar, \
self.p_z_mean, self.p_z_logvar)
self.init_nlls = -1.0 * \
self.log_prob_func(self.x_out, self.obs_transform(self.s0))
##################################################
# Setup the iterative generation loop using scan #
##################################################
def ir_step_func(hi_zmuv, sim1):
# get variables used throughout this refinement step
sim1_obs = self.obs_transform(sim1) # transform state -> obs
grad_ll = self.x_out - sim1_obs
# get samples of next hi, conditioned on current si
hi_p_mean, hi_p_logvar = self.p_hi_given_si.apply( \
sim1_obs, do_samples=False)
# now we build the model for variational hi given si
hi_q_mean, hi_q_logvar = self.q_hi_given_x_si.apply( \
示例6: WalkoutModel
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
switch_val = 1.0
zero_ary = np.zeros((1,))
new_val = zero_ary + switch_val
self.train_switch.set_value(to_fX(new_val))
return
def _construct_zi_zmuv(self, xo):
"""
Construct the necessary ZMUV gaussian samples for generating
trajectories from this WalkoutModel, for input matrix xo.
"""
zi_zmuv = self.rng.normal( \
size=(self.total_steps, xo.shape[0], self.z_dim), \
avg=0.0, std=1.0, dtype=theano.config.floatX)
return zi_zmuv
def _construct_rev_masks(self, xo):
"""
Compute the sequential revelation masks for the input batch in xo.
-- We need to construct mask sequences for both p and q.
"""
if self.use_rev_masks:
# make batch copies of self.rev_masks_p and self.rev_masks_q
pmasks = self.rev_masks_p.dimshuffle(0,'x',1).repeat(xo.shape[0], axis=1)
qmasks = self.rev_masks_q.dimshuffle(0,'x',1).repeat(xo.shape[0], axis=1)
else:
pm_list = []
qm_list = []
# make a zero mask that does nothing
zero_mask = T.alloc(0.0, 1, xo.shape[0], xo.shape[1])
# generate independently sampled masks for each revelation block
for rb in self.rev_sched:
# make a random binary mask with ones at rate rb[1]
rand_vals = self.rng.uniform( \
size=(1, xo.shape[0], xo.shape[1]), \
low=0.0, high=1.0, dtype=theano.config.floatX)
rand_mask = rand_vals < rb[1]
# append the masks for this revleation block to the mask lists
#
# the guide policy (in q) gets to peek at the values that will be
# revealed to the primary policy (in p) for the entire block. The
# primary policy only gets to see these values at end of the final
# step of the block. Within a given step, values are revealed to q
# at the beginning of the step, and to p at the end.
#
# e.g. in a revelation block with only a single step, the guide
# policy sees the values at the beginning of the step, which allows
# it to guide the step. the primary policy only gets to see the
# values at the end of the step.
#
# i.e. a standard variational auto-encoder is equivalent to a
# sequential revelation and refinement model with only one
# revelation block, which has one step and a reveal rate of 1.0.
#
for refine_step in range(rb[0]-1):
pm_list.append(zero_mask)
qm_list.append(rand_mask)
pm_list.append(rand_mask)
qm_list.append(rand_mask)
# concatenate each mask list into a 3-tensor
pmasks = T.cast(T.concatenate(pm_list, axis=0), 'floatX')
qmasks = T.cast(T.concatenate(qm_list, axis=0), 'floatX')
return [pmasks, qmasks]
def _construct_nll_costs(self, si, xo, nll_mask):
"""
示例7: HiddenLayer
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
else:
self.noisy_input = self.fuzzy_input
# Set some basic layer properties
self.pool_size = pool_size
self.in_dim = in_dim
self.out_dim = out_dim
if self.pool_size <= 1:
self.filt_count = self.out_dim
else:
self.filt_count = self.out_dim * self.pool_size
self.pool_count = self.filt_count / max(self.pool_size, 1)
if activation:
self.activation = activation
else:
if self.pool_size <= 1:
self.activation = lambda x: relu_actfun(x)
else:
self.activation = lambda x: \
maxout_actfun(x, self.pool_size, self.filt_count)
# Get some random initial weights and biases, if not given
if W is None:
if self.pool_size <= 1:
# Generate random initial filters in a typical way
W_init = np.asarray(0.04 * rng.standard_normal( \
size=(self.in_dim, self.filt_count)), \
dtype=theano.config.floatX)
else:
# Generate groups of random filters to pool over such that
# intra-group correlations are stronger than inter-group
# correlations, to encourage pooling over similar filters...
filters = []
for g_num in range(self.pool_count):
g_filt = 0.01 * rng.standard_normal(size=(self.in_dim,1))
for f_num in range(self.pool_size):
f_filt = g_filt + (0.005 * rng.standard_normal( \
size=(self.in_dim,1)))
filters.append(f_filt)
W_init = np.hstack(filters).astype(theano.config.floatX)
W = theano.shared(value=W_init, name="{0:s}_W".format(name))
if b is None:
b_init = np.zeros((self.filt_count,), dtype=theano.config.floatX)
b = theano.shared(value=b_init, name="{0:s}_b".format(name))
# Set layer weights and biases
self.W = W
self.b = b
# Compute linear "pre-activation" for this layer
if use_bias:
self.linear_output = T.dot(self.noisy_input, self.W) + self.b
else:
self.linear_output = T.dot(self.noisy_input, self.W)
# Add noise to the pre-activation features (if desired)
self.noisy_linear = self.linear_output + \
(bias_noise * self.srng.normal(size=self.linear_output.shape, \
dtype=theano.config.floatX))
# Apply activation function
self.output = self.activation(self.noisy_linear)
# Compute some properties of the activations, probably to regularize
self.act_l2_sum = T.sum(self.output**2.) / self.output.size
self.row_l1_sum = T.sum(abs(row_normalize(self.output))) / \
self.output.shape[0]
self.col_l1_sum = T.sum(abs(col_normalize(self.output))) / \
self.output.shape[1]
# Conveniently package layer parameters
if use_bias:
self.params = [self.W, self.b]
else:
self.params = [self.W]
# Layer construction complete...
return
def _drop_from_input(self, input, p):
"""p is the probability of dropping elements of input."""
# get a drop mask that drops things with probability p
#drop_mask = self.srng.binomial(n=1, p=1-p, size=input.shape, \
# dtype=theano.config.floatX)
noise_rnd = self.srng.uniform(input.shape, low=0.0, high=1.0, \
dtype=theano.config.floatX)
drop_mask = noise_rnd > p
# get a scaling factor to keep expectations fixed after droppage
drop_scale = 1. / (1. - p)
# apply dropout mask and rescaling factor to the input
droppy_input = drop_scale * input * drop_mask
return droppy_input
def _noisy_params(self, P, noise_lvl=0.):
"""Noisy weights, like convolving energy surface with a gaussian."""
#P_nz = P + self.srng.normal(size=P.shape, avg=0., std=noise_lvl, \
# dtype=theano.config.floatX)
P_nz = P + self.srng.normal(size=P.shape, avg=0.0, std=noise_lvl, \
dtype=theano.config.floatX)
return P_nz
示例8: HiddenLayer
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
self.pool_count = self.filt_count / max(self.pool_size, 1)
if activation is None:
activation = relu_actfun
if self.pool_size <= 1:
self.activation = activation
else:
self.activation = lambda x: \
maxout_actfun(x, self.pool_size, self.filt_count)
# Get some random initial weights and biases, if not given
if W is None:
# Generate initial filters using orthogonal random trick
W_shape = (self.in_dim, self.filt_count)
#W_scale = W_scale * (1.0 / np.sqrt(self.in_dim))
#W_init = W_scale * npr.normal(0.0, 1.0, W_shape)
W_init = ortho_matrix(shape=(self.in_dim, self.filt_count), \
gain=W_scale)
#W_init = 0.01 * npr.normal(0.0, 1.0, W_shape)
W_init = W_init.astype(theano.config.floatX)
W = theano.shared(value=W_init, name="{0:s}_W".format(name))
if b is None:
b_init = np.zeros((self.filt_count,), dtype=theano.config.floatX)
b = theano.shared(value=b_init, name="{0:s}_b".format(name))
# Set layer weights and biases
self.W = W
self.b = b
# Feedforward through the layer
use_in = input_noise > 0.001
use_bn = bias_noise > 0.001
use_drop = drop_rate > 0.001
self.linear_output, self.noisy_linear, self.output = \
self.apply(input, use_in=use_in, use_bn=use_bn, \
use_drop=use_drop)
# Compute some properties of the activations, probably to regularize
self.act_l2_sum = T.sum(self.noisy_linear**2.) / self.output.size
# Conveniently package layer parameters
self.params = [self.W, self.b, self.b_in, self.s_in]
self.shared_param_dicts = { \
'W': self.W, \
'b': self.b, \
'b_in': self.b_in, \
's_in': self.s_in }
# Layer construction complete...
return
def apply(self, input, use_in=False, use_bn=False, use_drop=False):
"""
Apply feedforward to this input, returning several partial results.
"""
# Add gaussian noise to the input (if desired)
#fancy_input = T.nnet.softplus(self.s_in) * (input + self.b_in)
fancy_input = input
if use_in:
fuzzy_input = fancy_input + self.input_noise[0] * \
self.rng.normal(size=fancy_input.shape, avg=0.0, std=1.0, \
dtype=theano.config.floatX)
else:
fuzzy_input = fancy_input
# Apply masking noise to the input (if desired)
if use_drop:
noisy_input = self._drop_from_input(fuzzy_input, self.drop_rate[0])
else:
noisy_input = fuzzy_input
self.noisy_input = noisy_input
# Compute linear "pre-activation" for this layer
linear_output = T.dot(noisy_input, self.W) + self.b
# Add noise to the pre-activation features (if desired)
if use_bn:
noisy_linear = linear_output + self.bias_noise[0] * \
self.rng.normal(size=linear_output.shape, avg=0.0, \
std=1.0, dtype=theano.config.floatX)
else:
noisy_linear = linear_output
# Apply activation function
final_output = self.activation(noisy_linear)
# package partial results for easy return
results = [linear_output, noisy_linear, final_output]
return results
def _drop_from_input(self, input, p):
"""p is the probability of dropping elements of input."""
# get a drop mask that drops things with probability p
drop_rnd = self.rng.uniform(size=input.shape, low=0.0, high=1.0, \
dtype=theano.config.floatX)
drop_mask = drop_rnd > p
# get a scaling factor to keep expectations fixed after droppage
drop_scale = 1. / (1. - p)
# apply dropout mask and rescaling factor to the input
droppy_input = drop_scale * input * drop_mask
return droppy_input
def _noisy_params(self, P, noise_lvl=0.):
"""Noisy weights, like convolving energy surface with a gaussian."""
P_nz = P + self.rng.normal(size=P.shape, avg=0.0, std=noise_lvl, \
dtype=theano.config.floatX)
return P_nz
示例9: DAELayer
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class DAELayer(object):
def __init__(self, rng, clean_input=None, fuzzy_input=None, \
in_dim=0, out_dim=0, activation=None, input_noise=0., \
W=None, b_h=None, b_v=None, W_scale=1.0):
# Setup a shared random generator for this layer
self.rng = RandStream(rng.randint(1000000))
# Grab the layer input and perturb it with some sort of noise. This
# is, afterall, a _denoising_ autoencoder...
self.clean_input = clean_input
self.noisy_input = self._get_noisy_input(fuzzy_input, input_noise)
# Set some basic layer properties
self.activation = activation
self.in_dim = in_dim
self.out_dim = out_dim
# Get some random initial weights and biases, if not given
if W is None:
W_init = np.asarray(1.0 * DCG(rng.standard_normal( \
size=(in_dim, out_dim)), dtype=theano.config.floatX))
W = theano.shared(value=(W_scale*W_init), name='W')
if b_h is None:
b_init = np.zeros((out_dim,), dtype=theano.config.floatX)
b_h = theano.shared(value=b_init, name='b_h')
if b_v is None:
b_init = np.zeros((in_dim,), dtype=theano.config.floatX)
b_v = theano.shared(value=b_init, name='b_v')
# Grab pointers to the now-initialized weights and biases
self.W = W
self.b_h = b_h
self.b_v = b_v
# Put the learnable/optimizable parameters into a list
self.params = [self.W, self.b_h, self.b_v]
# Beep boop... layer construction complete...
return
def compute_costs(self, lam_l1=None):
"""Compute reconstruction and activation sparsity costs."""
# Get noise-perturbed encoder/decoder parameters
W_nz = self._noisy_params(self.W, 0.01)
b_nz = self.b_h #self._noisy_params(self.b_h, 0.05)
# Compute hidden and visible activations
A_v, A_h = self._compute_activations(self.noisy_input, \
W_nz, b_nz, self.b_v)
# Compute reconstruction error cost
recon_cost = T.sum((self.clean_input - A_v)**2.0) / \
self.clean_input.shape[0]
# Compute sparsity penalty (over both population and lifetime)
row_l1_sum = T.sum(abs(row_normalize(A_h))) / A_h.shape[0]
col_l1_sum = T.sum(abs(col_normalize(A_h))) / A_h.shape[1]
sparse_cost = lam_l1[0] * (row_l1_sum + col_l1_sum)
return [recon_cost, sparse_cost]
def _compute_hidden_acts(self, X, W, b_h):
"""Compute activations of encoder (at hidden layer)."""
A_h = self.activation(T.dot(X, W) + b_h)
return A_h
def _compute_activations(self, X, W, b_h, b_v):
"""Compute activations of decoder (at visible layer)."""
A_h = self._compute_hidden_acts(X, W, b_h)
A_v = T.dot(A_h, W.T) + b_v
return [A_v, A_h]
def _noisy_params(self, P, noise_lvl=0.):
"""Noisy weights, like convolving energy surface with a gaussian."""
if noise_lvl > 1e-3:
P_nz = P + DCG(self.rng.normal(size=P.shape, avg=0.0, std=noise_lvl, \
dtype=theano.config.floatX))
else:
P_nz = P
return P_nz
def _get_noisy_input(self, input, p):
"""p is the probability of dropping elements of input."""
drop_rnd = self.rng.uniform(input.shape, low=0.0, high=1.0, \
dtype=theano.config.floatX)
drop_mask = drop_rnd > p
# Cast mask from int to float32, to keep things on GPU
noisy_input = input * DCG(drop_mask)
return noisy_input
示例10: ConvPoolLayer
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class ConvPoolLayer(object):
"""
A simple convolution --> max-pooling layer.
The (symbolic) input to this layer must be a theano.tensor.dtensor4 shaped
like (batch_size, chan_count, im_dim_1, im_dim_2).
filt_def should be a 4-tuple like (filt_count, in_chans, filt_def_1, filt_def_2)
pool_def should be a 3-tuple like (pool_dim, pool_stride)
"""
def __init__(self, rng, input=None, filt_def=None, pool_def=(2, 2), \
activation=None, drop_rate=0., input_noise=0., bias_noise=0., \
W=None, b=None, name="", W_scale=1.0):
# Setup a shared random generator for this layer
#self.rng = theano.tensor.shared_randomstreams.RandomStreams( \
# rng.randint(100000))
self.rng = CURAND_RandomStreams(rng.randint(1000000))
self.clean_input = input
# Add gaussian noise to the input (if desired)
if (input_noise > 1e-4):
self.fuzzy_input = input + self.rng.normal(size=input.shape, \
avg=0.0, std=input_noise, dtype=theano.config.floatX)
else:
self.fuzzy_input = input
# Apply masking noise to the input (if desired)
if (drop_rate > 1e-4):
self.noisy_input = self._drop_from_input(self.fuzzy_input, drop_rate)
else:
self.noisy_input = self.fuzzy_input
# Set the activation function for the conv filters
if activation:
self.activation = activation
else:
self.activation = lambda x: relu_actfun(x)
# initialize weights with random weights
W_init = 0.01 * np.asarray(rng.normal( \
size=filt_def), dtype=theano.config.floatX)
self.W = theano.shared(value=(W_scale*W_init), \
name="{0:s}_W".format(name))
# the bias is a 1D tensor -- one bias per output feature map
b_init = np.zeros((filt_def[0],), dtype=theano.config.floatX) + 0.1
self.b = theano.shared(value=b_init, name="{0:s}_b".format(name))
# convolve input feature maps with filters
input_c01b = self.noisy_input.dimshuffle(1, 2, 3, 0) # bc01 to c01b
filters_c01b = self.W.dimshuffle(1, 2, 3, 0) # bc01 to c01b
conv_op = FilterActs(stride=1, partial_sum=1)
contig_input = gpu_contiguous(input_c01b)
contig_filters = gpu_contiguous(filters_c01b)
conv_out_c01b = conv_op(contig_input, contig_filters)
if (bias_noise > 1e-4):
noisy_conv_out_c01b = conv_out_c01b + self.rng.normal( \
size=conv_out_c01b.shape, avg=0.0, std=bias_noise, \
dtype=theano.config.floatX)
else:
noisy_conv_out_c01b = conv_out_c01b
# downsample each feature map individually, using maxpooling
pool_op = MaxPool(ds=pool_def[0], stride=pool_def[1])
mp_out_c01b = pool_op(noisy_conv_out_c01b)
mp_out_bc01 = mp_out_c01b.dimshuffle(3, 0, 1, 2) # c01b to bc01
# 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.noisy_linear_output = mp_out_bc01 + self.b.dimshuffle('x', 0, 'x', 'x')
self.linear_output = self.noisy_linear_output
self.output = self.activation(self.noisy_linear_output)
# store parameters of this layer
self.params = [self.W, self.b]
return
def _drop_from_input(self, input, p):
"""p is the probability of dropping elements of input."""
# get a drop mask that drops things with probability p
drop_rnd = self.rng.uniform(size=input.shape, low=0.0, high=1.0, \
dtype=theano.config.floatX)
drop_mask = drop_rnd > p
# get a scaling factor to keep expectations fixed after droppage
drop_scale = 1. / (1. - p)
# apply dropout mask and rescaling factor to the input
droppy_input = drop_scale * input * drop_mask
return droppy_input
def _noisy_params(self, P, noise_lvl=0.):
"""Noisy weights, like convolving energy surface with a gaussian."""
P_nz = P + self.rng.normal(size=P.shape, avg=0.0, std=noise_lvl, \
dtype=theano.config.floatX)
#.........这里部分代码省略.........
示例11: TwoStageModel
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
#.........这里部分代码省略.........
self.batch_reps = T.lscalar()
# setup switching variable for changing between sampling/training
zero_ary = to_fX( np.zeros((1,)) )
self.train_switch = theano.shared(value=zero_ary, name='msm_train_switch')
self.set_train_switch(1.0)
# setup a variable for controlling dropout noise
self.drop_rate = theano.shared(value=zero_ary, name='msm_drop_rate')
self.set_drop_rate(0.0)
# this weight balances l1 vs. l2 penalty on posterior KLds
self.lam_kld_l1l2 = theano.shared(value=zero_ary, name='msm_lam_kld_l1l2')
self.set_lam_kld_l1l2(1.0)
if self.shared_param_dicts is None:
# initialize "optimizable" parameters specific to this MSM
init_vec = to_fX( np.zeros((self.z_dim,)) )
self.p_z_mean = theano.shared(value=init_vec, name='msm_p_z_mean')
self.p_z_logvar = theano.shared(value=init_vec, name='msm_p_z_logvar')
init_vec = to_fX( np.zeros((self.x_dim,)) )
self.obs_logvar = theano.shared(value=zero_ary, name='msm_obs_logvar')
self.bounded_logvar = 8.0 * T.tanh((1.0/8.0) * self.obs_logvar)
self.shared_param_dicts = {}
self.shared_param_dicts['p_z_mean'] = self.p_z_mean
self.shared_param_dicts['p_z_logvar'] = self.p_z_logvar
self.shared_param_dicts['obs_logvar'] = self.obs_logvar
else:
self.p_z_mean = self.shared_param_dicts['p_z_mean']
self.p_z_logvar = self.shared_param_dicts['p_z_logvar']
self.obs_logvar = self.shared_param_dicts['obs_logvar']
self.bounded_logvar = 8.0 * T.tanh((1.0/8.0) * self.obs_logvar)
# get a drop mask that drops things with probability p
drop_scale = 1. / (1. - self.drop_rate[0])
drop_rnd = self.rng.uniform(size=self.x_out.shape, \
low=0.0, high=1.0, dtype=theano.config.floatX)
drop_mask = drop_scale * (drop_rnd > self.drop_rate[0])
##############################################
# Setup the TwoStageModels main computation. #
##############################################
print("Building TSM...")
# samples of "first" latent state
drop_x = drop_mask * self.x_in
z_q_mean, z_q_logvar, self.z = \
self.q_z_given_x.apply(drop_x, do_samples=True)
# compute relevant KLds for this step
self.kld_z_q2ps = gaussian_kld(z_q_mean, z_q_logvar, \
self.p_z_mean, self.p_z_logvar)
self.kld_z_p2qs = gaussian_kld(self.p_z_mean, self.p_z_logvar, \
z_q_mean, z_q_logvar)
# transform "first" latent state into "second" latent state
self.s, _ = self.p_s_given_z.apply(self.z, do_samples=False)
# get samples of h, conditioned on current s
h_p_mean, h_p_logvar, h_p = self.p_h_given_s.apply( \
self.s, do_samples=True)
# get variational samples of h, given s and x_out
h_q_mean, h_q_logvar, h_q = self.q_h_given_x_s.apply( \
T.horizontal_stack(self.x_out, self.s), \
do_samples=True)
# make h samples that can be switched between h_p and h_q
self.h = (self.train_switch[0] * h_q) + \
((1.0 - self.train_switch[0]) * h_p)
# compute relevant KLds for this step
示例12: ClassModel
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class ClassModel(object):
"""
Controller for training a fancy pseudo-bayesian classifier.
Parameters:
rng: numpy.random.RandomState (for reproducibility)
x_in: the input data to encode
y_in: int labels >= 1 for x_in when available, otherwise 0.
q_z_given_x: InfNet for z given x
class_count: number of classes to classify into
z_dim: dimension of the "initial" latent space
use_samples: whether to use z samples or just z mean
"""
def __init__(self, rng=None, \
x_in=None, y_in=None, \
q_z_given_x=None, \
class_count=None, \
z_dim=None, \
use_samples=None):
# setup a rng for this GIPair
self.rng = RandStream(rng.randint(100000))
# record the dimensions of various spaces relevant to this model
self.class_count = class_count
self.z_dim = z_dim
self.shared_dim = q_z_given_x.shared_layers[-1].out_dim
self.use_samples = use_samples
# grab handles to the relevant InfNets
self.q_z_given_x = q_z_given_x
# record the symbolic variables that will provide inputs to the
# computation graph created to describe this MultiStageModel
self.x_in = x_in
self.y_in = y_in
# setup switching variable for changing between sampling/training
zero_ary = to_fX( np.zeros((1,)) )
# setup a variable for controlling dropout noise
self.drop_rate = theano.shared(value=zero_ary, name='cm_drop_rate')
self.set_drop_rate(0.0)
# initialize classification layer parameters
init_mat = to_fX(0.01 * npr.randn(self.shared_dim, self.class_count))
init_vec = to_fX( np.zeros((self.class_count,)) )
self.W_class = theano.shared(value=init_mat, name='cm_W_class')
self.b_class = theano.shared(value=init_vec, name='cm_b_class')
# initialize "optimizable" parameters specific to this CM
init_vec = to_fX( np.zeros((self.z_dim,)) )
self.p_z_mean = theano.shared(value=init_vec, name='cm_p_z_mean')
self.p_z_logvar = theano.shared(value=init_vec, name='cm_p_z_logvar')
#################
# Setup self.z. #
#################
self.q_z_mean, self.q_z_logvar, self.q_z_samples = \
self.q_z_given_x.apply(self.x_in, do_samples=True)
self.q_z_samples = self.q_z_given_x.apply_shared(self.x_in)
# get a drop mask that drops things with probability p
drop_scale = 1. / (1. - self.drop_rate[0])
drop_rnd = self.rng.uniform(size=self.q_z_samples.shape, \
low=0.0, high=1.0, dtype=theano.config.floatX)
drop_mask = drop_scale * (drop_rnd > self.drop_rate[0])
# get a droppy version of either z mean or z samples
# if self.use_samples:
# self.z = self.q_z_samples * drop_mask
# else:
# self.z = self.q_z_mean * drop_mask
self.z = self.q_z_samples * drop_mask
# compute class predictions
self.y_out = T.dot(self.z, self.W_class) + self.b_class
# compute KLds for training via variational free-energy
self.kld_z_q2ps = gaussian_kld(self.q_z_mean, self.q_z_logvar, \
self.p_z_mean, self.p_z_logvar)
self.kld_z_p2qs = gaussian_kld(self.p_z_mean, self.p_z_logvar, \
self.q_z_mean, self.q_z_logvar)
######################################################################
# ALL SYMBOLIC VARS NEEDED FOR THE OBJECTIVE SHOULD NOW BE AVAILABLE #
######################################################################
# shared var learning rate for generator and inferencer
zero_ary = to_fX( np.zeros((1,)) )
self.lr_1 = theano.shared(value=zero_ary, name='cm_lr_1')
self.lr_2 = theano.shared(value=zero_ary, name='cm_lr_2')
# shared var momentum parameters for generator and inferencer
self.mom_1 = theano.shared(value=zero_ary, name='cm_mom_1')
self.mom_2 = theano.shared(value=zero_ary, name='cm_mom_2')
# init parameters for controlling learning dynamics
self.set_sgd_params()
# init shared var for weighting nll of data given posterior sample
self.lam_nll = theano.shared(value=zero_ary, name='cm_lam_nll')
self.set_lam_nll(lam_nll=1.0)
# init shared var for weighting prior kld against reconstruction
self.lam_kld_q2p = theano.shared(value=zero_ary, name='cm_lam_kld_q2p')
self.lam_kld_p2q = theano.shared(value=zero_ary, name='cm_lam_kld_p2q')
#.........这里部分代码省略.........
示例13: GenUniModule
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class GenUniModule(object):
"""
Module that applies a linear transform followed by an non-linearity.
"""
def __init__(self, rand_dim, out_dim,
apply_bn=True, init_func=None,
rand_type='normal', final_relu=True,
mod_name='dm_uni'):
self.rand_dim = rand_dim
self.out_dim = out_dim
self.apply_bn = apply_bn
self.mod_name = mod_name
self.rand_type = rand_type
self.final_relu = final_relu
self.rng = RandStream(123)
if init_func is None:
self.init_func = inits.Normal(scale=0.02)
else:
self.init_func = init_func
self._init_params() # initialize parameters
return
def _init_params(self):
"""
Initialize parameters for the layers in this generator module.
"""
self.w1 = self.init_func((self.rand_dim, self.out_dim),
"{}_w1".format(self.mod_name))
self.params = [ self.w1 ]
# make gains and biases for transforms that will get batch normed
if self.apply_bn:
gain_ifn = inits.Normal(loc=1., scale=0.02)
bias_ifn = inits.Constant(c=0.)
self.g1 = gain_ifn((self.out_dim), "{}_g1".format(self.mod_name))
self.b1 = bias_ifn((self.out_dim), "{}_b1".format(self.mod_name))
self.params.extend([self.g1, self.b1])
return
def apply(self, batch_size=None, rand_vals=None):
"""
Apply this generator module. Pass _either_ batch_size or rand_vals.
"""
assert not ((batch_size is None) and (rand_vals is None)), "need either batch_size or rand_vals"
if rand_vals is None:
rand_shape = (batch_size, self.rand_dim)
if self.rand_type == 'normal':
rand_vals = self.rng.normal(size=rand_shape, avg=0.0, std=1.0, \
dtype=theano.config.floatX)
else:
rand_vals = self.rng.uniform(size=rand_shape, low=-1.0, high=1.0, \
dtype=theano.config.floatX)
else:
rand_shape = (rand_vals.shape[0], self.rand_dim)
rand_vals = rand_vals.reshape(rand_shape)
# transform random values linearly
h1 = T.dot(rand_vals, self.w1)
if self.apply_bn:
h1 = batchnorm(h1, g=self.g1, b=self.b1)
if self.final_relu:
h1 = relu(h1)
return h1
##############
# EYE BUFFER #
##############
示例14: GenFCModule
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class GenFCModule(object):
"""
Module that transforms random values through a single fully connected
layer, and then a linear transform (with another relu, optionally).
"""
def __init__(self, rand_dim, out_dim, fc_dim,
apply_bn_1=True, apply_bn_2=True,
init_func=None, rand_type='normal',
final_relu=True, mod_name='dm_fc'):
self.rand_dim = rand_dim
self.out_dim = out_dim
self.fc_dim = fc_dim
self.apply_bn_1 = apply_bn_1
self.apply_bn_2 = apply_bn_2
self.mod_name = mod_name
self.rand_type = rand_type
self.final_relu = final_relu
self.rng = RandStream(123)
if init_func is None:
self.init_func = inits.Normal(scale=0.02)
else:
self.init_func = init_func
self._init_params() # initialize parameters
return
def _init_params(self):
"""
Initialize parameters for the layers in this generator module.
"""
self.w1 = self.init_func((self.rand_dim, self.fc_dim),
"{}_w1".format(self.mod_name))
self.w2 = self.init_func((self.fc_dim, self.out_dim),
"{}_w2".format(self.mod_name))
self.params = [self.w1, self.w2]
# make gains and biases for transforms that will get batch normed
if self.apply_bn_1:
gain_ifn = inits.Normal(loc=1., scale=0.02)
bias_ifn = inits.Constant(c=0.)
self.g1 = gain_ifn((self.fc_dim), "{}_g1".format(self.mod_name))
self.b1 = bias_ifn((self.fc_dim), "{}_b1".format(self.mod_name))
self.params.extend([self.g1, self.b1])
if self.apply_bn_2:
gain_ifn = inits.Normal(loc=1., scale=0.02)
bias_ifn = inits.Constant(c=0.)
self.g2 = gain_ifn((self.out_dim), "{}_g2".format(self.mod_name))
self.b2 = bias_ifn((self.out_dim), "{}_b2".format(self.mod_name))
self.params.extend([self.g2, self.b2])
return
def apply(self, batch_size=None, rand_vals=None):
"""
Apply this generator module. Pass _either_ batch_size or rand_vals.
"""
assert not ((batch_size is None) and (rand_vals is None)), "need either batch_size or rand_vals"
if rand_vals is None:
rand_shape = (batch_size, self.rand_dim)
if self.rand_type == 'normal':
rand_vals = self.rng.normal(size=rand_shape, avg=0.0, std=1.0, \
dtype=theano.config.floatX)
else:
rand_vals = self.rng.uniform(size=rand_shape, low=-1.0, high=1.0, \
dtype=theano.config.floatX)
else:
rand_shape = (rand_vals.shape[0], self.rand_dim)
rand_vals = rand_vals.reshape(rand_shape)
# transform random values into fc layer
h1 = T.dot(rand_vals, self.w1)
if self.apply_bn_1:
h1 = batchnorm(h1, g=self.g1, b=self.b1)
h1 = relu(h1)
# transform from fc layer to output
h2 = T.dot(h1, self.w2)
if self.apply_bn_2:
h2 = batchnorm(h2, g=self.g2, b=self.b2)
if self.final_relu:
h2 = relu(h2)
return h2
示例15: GenConvModule
# 需要导入模块: from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams [as 别名]
# 或者: from theano.sandbox.cuda.rng_curand.CURAND_RandomStreams import uniform [as 别名]
class GenConvModule(object):
"""
Module of one "fractionally strided" convolution layer followed by one
regular convolution layer. Inputs to the fractionally strided convolution
can optionally be augmented with some random values.
Params:
filt_shape: shape for convolution filters -- should be square and odd
in_chans: number of channels in the inputs to module
out_chans: number of channels in the outputs from module
rand_chans: number of random channels to augment input
use_rand: flag for whether or not to augment inputs
apply_bn_1: flag for whether to batch normalize following first conv
apply_bn_2: flag for whether to batch normalize following second conv
us_stride: upsampling ratio in the fractionally strided convolution
use_pooling: whether to use unpooling or fractional striding
init_func: function for initializing module parameters
mod_name: text name for identifying module in theano graph
rand_type: whether to use Gaussian or uniform randomness
"""
def __init__(self, filt_shape, in_chans, out_chans, rand_chans,
use_rand=True, apply_bn_1=True, apply_bn_2=True,
us_stride=2, use_pooling=True,
init_func=None, mod_name='gm_conv',
rand_type='normal'):
assert ((filt_shape[0] % 2) > 0), "filter dim should be odd (not even)"
self.filt_dim = filt_shape[0]
self.in_chans = in_chans
self.out_chans = out_chans
self.rand_chans = rand_chans
self.use_rand = use_rand
self.apply_bn_1 = apply_bn_1
self.apply_bn_2 = apply_bn_2
self.us_stride = us_stride
self.use_pooling = use_pooling
self.mod_name = mod_name
self.rand_type = rand_type
self.rng = RandStream(123)
if init_func is None:
self.init_func = inits.Normal(scale=0.02)
else:
self.init_func = init_func
self._init_params() # initialize parameters
return
def _init_params(self):
"""
Initialize parameters for the layers in this generator module.
"""
if self.use_rand:
# random values will be stacked on exogenous input
self.w1 = self.init_func((self.out_chans, (self.in_chans+self.rand_chans), self.filt_dim, self.filt_dim),
"{}_w1".format(self.mod_name))
else:
# random values won't be stacked on exogenous input
self.w1 = self.init_func((self.out_chans, self.in_chans, self.filt_dim, self.filt_dim),
"{}_w1".format(self.mod_name))
self.w2 = self.init_func((self.out_chans, self.out_chans, self.filt_dim, self.filt_dim),
"{}_w2".format(self.mod_name))
self.params = [self.w1, self.w2]
# make gains and biases for transforms that will get batch normed
if self.apply_bn_1:
gain_ifn = inits.Normal(loc=1., scale=0.02)
bias_ifn = inits.Constant(c=0.)
self.g1 = gain_ifn((self.out_chans), "{}_g1".format(self.mod_name))
self.b1 = bias_ifn((self.out_chans), "{}_b1".format(self.mod_name))
self.params.extend([self.g1, self.b1])
if self.apply_bn_2:
gain_ifn = inits.Normal(loc=1., scale=0.02)
bias_ifn = inits.Constant(c=0.)
self.g2 = gain_ifn((self.out_chans), "{}_g2".format(self.mod_name))
self.b2 = bias_ifn((self.out_chans), "{}_b2".format(self.mod_name))
self.params.extend([self.g2, self.b2])
return
def apply(self, input, rand_vals=None):
"""
Apply this generator module to some input.
"""
batch_size = input.shape[0]
bm = int((self.filt_dim - 1) / 2) # use "same" mode convolutions
ss = self.us_stride # stride for "learned upsampling"
if self.use_pooling:
# "unpool" the input if desired
input = input.repeat(ss, axis=2).repeat(ss, axis=3)
# get shape for random values that will augment input
rand_shape = (batch_size, self.rand_chans, input.shape[2], input.shape[3])
if self.use_rand:
# augment input with random channels
if rand_vals is None:
if self.rand_type == 'normal':
rand_vals = self.rng.normal(size=rand_shape, avg=0.0, std=1.0, \
dtype=theano.config.floatX)
else:
rand_vals = self.rng.uniform(size=rand_shape, low=-1.0, high=1.0, \
dtype=theano.config.floatX)
rand_vals = rand_vals.reshape(rand_shape)
# stack random values on top of input
full_input = T.concatenate([rand_vals, input], axis=1)
else:
#.........这里部分代码省略.........