本文整理汇总了Python中theano.tensor.shared_randomstreams.RandomStreams.choice方法的典型用法代码示例。如果您正苦于以下问题:Python RandomStreams.choice方法的具体用法?Python RandomStreams.choice怎么用?Python RandomStreams.choice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor.shared_randomstreams.RandomStreams
的用法示例。
在下文中一共展示了RandomStreams.choice方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _negative_sampling
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
def _negative_sampling(self, num_negative_samples, target_indices):
assert num_negative_samples > 0
logging.debug('Stochastically sampling %d negative instances '
'out of %d classes (%.2f%%).',
num_negative_samples, self.num_entities,
100.0 *
float(num_negative_samples) / self.num_entities)
from theano.tensor.shared_randomstreams import RandomStreams
srng = RandomStreams(
seed=np.random.randint(low=0, high=(1 << 30)))
rng_sample_size = (self.batch_size, num_negative_samples,)
logging.debug(
'Using %s for random sample generation of %s tensors.',
RandomStreams, rng_sample_size)
logging.debug('For every batch %d random integers are sampled.',
np.prod(rng_sample_size))
random_negative_indices = srng.choice(
rng_sample_size,
a=self.num_entities,
p=self.clazz_distribution)
if self.__DEBUG__:
random_negative_indices = theano.printing.Print(
'random_negative_indices')(random_negative_indices)
return random_negative_indices
示例2: tied_losses
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
def tied_losses(preds, n_sample_preds, n_classes, n_pairs):
preds_per_trial_row = preds.reshape((-1, n_sample_preds, n_classes))
_srng = RandomStreams(get_rng().randint(1, 2147462579))
rand_inds = _srng.choice([n_pairs * 2], n_sample_preds, replace=False)
part_1 = preds_per_trial_row[:,rand_inds[:n_pairs]]
part_2 = preds_per_trial_row[:,rand_inds[n_pairs:]]
# Have to now ensure first values are larger zero
# for numerical stability :/
eps = 1e-4
part_1 = T.maximum(part_1, eps)
loss = categorical_crossentropy(part_1, part_2)
return loss
示例3: GibbsRegressor
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
class GibbsRegressor(ISymbolicPredictor):
def __init__(self, n_dim_in, n_dim_out, sample_y = False, n_alpha = 1, possible_ws = [0, 1],
alpha_update_policy = 'sequential', seed = None):
self._w = theano.shared(np.zeros((n_dim_in, n_dim_out), dtype = theano.config.floatX), name = 'w')
self._rng = RandomStreams(seed)
if n_alpha == 'all':
n_alpha = n_dim_in
self._n_alpha = n_alpha
self._alpha = theano.shared(np.arange(n_alpha)) # scalar
self._sample_y = sample_y
self._possible_ws = theano.shared(np.array(possible_ws), name = 'possible_ws')
assert alpha_update_policy in ('sequential', 'random')
self._alpha_update_policy = alpha_update_policy
def _add_alpha_update(self):
new_alpha = (self._alpha+self._n_alpha) % self._w.shape[0] \
if self._alpha_update_policy == 'sequential' else \
self._rng.choice(a=self._w.shape[0], size = (self._n_alpha, ), replace = False).reshape([-1]) # Reshape is for some reason necessary when n_alpha=1
add_update(self._alpha, new_alpha)
@staticmethod
def compute_p_wa(w, x, y, alpha, possible_ws = np.array([0, 1])):
"""
Compute the probability the weights at index alpha taking on each of the values in possible_ws
"""
assert x.tag.test_value.ndim == y.tag.test_value.ndim == 2
assert x.tag.test_value.shape[0] == y.tag.test_value.shape[0]
assert w.get_value().shape[1] == y.tag.test_value.shape[1]
v_current = x.dot(w) # (n_samples, n_dim_out)
v_0 = v_current[None, :, :] - w[alpha, None, :]*x.T[alpha, :, None] # (n_alpha, n_samples, n_dim_out)
possible_vs = v_0[:, :, :, None] + possible_ws[None, None, None, :]*x.T[alpha, :, None, None] # (n_alpha, n_samples, n_dim_out, n_possible_ws)
all_zs = tt.nnet.sigmoid(possible_vs) # (n_alpha, n_samples, n_dim_out, n_possible_ws)
log_likelihoods = tt.sum(tt.log(bernoulli(y[None, :, :, None], all_zs[:, :, :, :])), axis = 1) # (n_alpha, n_dim_out, n_possible_ws)
# Question: Need to shift for stability here or will Theano take care of that?
# Stupid theano didn't implement softmax very nicely so we have to do some reshaping.
return tt.nnet.softmax(log_likelihoods.reshape([alpha.shape[0]*w.shape[1], possible_ws.shape[0]]))\
.reshape([alpha.shape[0], w.shape[1], possible_ws.shape[0]]) # (n_alpha, n_dim_out, n_possible_ws)
@symbolic_updater
def train(self, x, y):
p_wa = self.compute_p_wa(self._w, x, y, self._alpha, self._possible_ws) # (n_alpha, n_dim_out, n_possible_ws)
w_sample = sample_categorical(self._rng, p_wa, values = self._possible_ws)
w_new = tt.set_subtensor(self._w[self._alpha], w_sample) # (n_dim_in, n_dim_out)
add_update(self._w, w_new)
self._add_alpha_update()
@symbolic_simple
def predict(self, x):
p_y = tt.nnet.sigmoid(x.dot(self._w))
return self._rng.binomial(p = p_y) if self._sample_y else p_y
示例4: KernelDensityEstimateDistribution
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
class KernelDensityEstimateDistribution(Distribution):
"""Randomly samples from a kernel density estimate yielded by a set
of training points.
Simple sampling procedure [1]:
1. With training points $x_1, ... x_n$, sample a point $x_i$
uniformly
2. From original KDE, we have a kernel defined at point $x_i$;
sample randomly from this kernel
[1]: http://www.stat.cmu.edu/~cshalizi/350/lectures/28/lecture-28.pdf
"""
def __init__(self, X, bandwidth=1, space=None, rng=None):
"""
Parameters
----------
X : ndarray of shape (num_examples, num_features)
Training examples from which to generate a kernel density
estimate
bandwidth : float
Bandwidth (or h, or sigma) of the generated kernels
"""
assert X.ndim == 2
if space is None:
space = VectorSpace(dim=X.shape[1], dtype=X.dtype)
# super(KernelDensityEstimateDistribution, self).__init__(space)
self.X = sharedX(X, name='KDE_X')
self.bandwidth = sharedX(bandwidth, name='bandwidth')
self.rng = RandomStreams() if rng is None else rng
def sample(self, n):
# Sample $n$ training examples
training_samples = self.X[self.rng.choice(size=(n,), a=self.X.shape[0], replace=True)]
# Sample individually from each selected associated kernel
#
# (not well documented within NumPy / Theano, but rng.normal
# call samples from a multivariate normal with diagonal
# covariance matrix)
ret = self.rng.normal(size=(n, self.X.shape[1]),
avg=training_samples, std=self.bandwidth,
dtype=theano.config.floatX)
return ret
示例5: test_choice
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
def test_choice(self):
"""Test that RandomStreams.choice generates the same results as numpy"""
# Check over two calls to see if the random state is correctly updated.
random = RandomStreams(utt.fetch_seed())
fn = function([], random.choice((11, 8), 10, 1, 0))
fn_val0 = fn()
fn_val1 = fn()
rng_seed = np.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = np.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.choice(10, (11, 8), True, None)
numpy_val1 = rng.choice(10, (11, 8), True, None)
assert np.all(fn_val0 == numpy_val0)
assert np.all(fn_val1 == numpy_val1)
示例6: test_choice
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
def test_choice(self):
"""Test that RandomStreams.choice generates the same results as numpy"""
# numpy.random.choice is only available for numpy versions >= 1.7
major, minor, _ = numpy.version.short_version.split('.')
if (int(major), int(minor)) < (1, 7):
raise utt.SkipTest('choice requires at NumPy version >= 1.7 '
'(%s)' % numpy.__version__)
# Check over two calls to see if the random state is correctly updated.
random = RandomStreams(utt.fetch_seed())
fn = function([], random.choice((11, 8), 10, 1, 0))
fn_val0 = fn()
fn_val1 = fn()
rng_seed = numpy.random.RandomState(utt.fetch_seed()).randint(2**30)
rng = numpy.random.RandomState(int(rng_seed)) # int() is for 32bit
numpy_val0 = rng.choice(10, (11, 8), True, None)
numpy_val1 = rng.choice(10, (11, 8), True, None)
assert numpy.all(fn_val0 == numpy_val0)
assert numpy.all(fn_val1 == numpy_val1)
示例7: SDGMSSL
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
#.........这里部分代码省略.........
axis=(1, 2))
y_u += 1e-8 # Ensure that we get no NANs when calculating the entropy
y_u /= T.sum(y_u, axis=1, keepdims=True)
lb_u = (y_u * (lb_u - T.log(y_u))).sum(axis=1)
if self.batchnorm:
# TODO: implement the BN layer correctly.
inputs = {self.l_x_in: self.sym_x_u, self.l_y_in: y_u, self.l_a_in: a_x_u}
get_output(out_layers, inputs, weighting=None, batch_norm_update_averages=True,
batch_norm_use_averages=False)
# Regularizing with weight priors p(theta|N(0,1)), collecting and clipping gradients
weight_priors = 0.0
for p in self.trainable_model_params:
if 'W' not in str(p):
continue
weight_priors += log_normal(p, 0, 1).sum()
# Collect the lower bound and scale it with the weight priors.
elbo = ((lb_l.mean() + lb_u.mean()) * n + weight_priors) / -n
lb_labeled = -lb_l.mean()
lb_unlabeled = -lb_u.mean()
grads_collect = T.grad(elbo, self.trainable_model_params)
params_collect = self.trainable_model_params
sym_beta1 = T.scalar('beta1')
sym_beta2 = T.scalar('beta2')
clip_grad, max_norm = 1, 5
mgrads = total_norm_constraint(grads_collect, max_norm=max_norm)
mgrads = [T.clip(g, -clip_grad, clip_grad) for g in mgrads]
updates = adam(mgrads, params_collect, self.sym_lr, sym_beta1, sym_beta2)
# Training function
indices = self._srng.choice(size=[self.sym_bs_l], a=sh_train_x_l.shape[0], replace=False)
x_batch_l = sh_train_x_l[indices]
t_batch_l = sh_train_t_l[indices]
x_batch_u = self.sh_train_x[self.batch_slice]
if self.x_dist == 'bernoulli': # Sample bernoulli input.
x_batch_u = self._srng.binomial(size=x_batch_u.shape, n=1, p=x_batch_u, dtype=theano.config.floatX)
x_batch_l = self._srng.binomial(size=x_batch_l.shape, n=1, p=x_batch_l, dtype=theano.config.floatX)
givens = {self.sym_x_l: x_batch_l,
self.sym_x_u: x_batch_u,
self.sym_t_l: t_batch_l}
inputs = [self.sym_index, self.sym_batchsize, self.sym_bs_l, self.sym_beta,
self.sym_lr, sym_beta1, sym_beta2, self.sym_samples]
outputs = [elbo, lb_labeled, lb_unlabeled]
f_train = theano.function(inputs=inputs, outputs=outputs, givens=givens, updates=updates)
# Default training args. Note that these can be changed during or prior to training.
self.train_args['inputs']['batchsize_unlabeled'] = 100
self.train_args['inputs']['batchsize_labeled'] = 100
self.train_args['inputs']['beta'] = 0.1
self.train_args['inputs']['learningrate'] = 3e-4
self.train_args['inputs']['beta1'] = 0.9
self.train_args['inputs']['beta2'] = 0.999
self.train_args['inputs']['samples'] = 1
self.train_args['outputs']['lb'] = '%0.4f'
self.train_args['outputs']['lb-labeled'] = '%0.4f'
self.train_args['outputs']['lb-unlabeled'] = '%0.4f'
# Validation and test function
y = get_output(self.l_qy, self.sym_x_l, deterministic=True).mean(axis=(1, 2))
class_err = (1. - categorical_accuracy(y, self.sym_t_l).mean()) * 100
givens = {self.sym_x_l: self.sh_test_x,
self.sym_t_l: self.sh_test_t}
示例8: RandomStreams
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import choice [as 别名]
import theano as th
data = np.random.rand(10,3)
it = th.shared(0)
y = th.shared(data)
srng = RandomStreams(seed=234)
expectRvs = srng.normal(size=(3,1))
expectRvs.name='expectRvs'
epochStream = srng.permutation(n=10)
currentBatch = epochStream.reshape((5,2))[:,it]
y_mini = y[ currentBatch, :]
L = th.tensor.sum(th.tensor.dot( y_mini, expectRvs ))
L_func = function([], L, no_default_updates=True)
padding = srng.choice(size=(3,), a=10, replace=False, p=None, ndim=None, dtype='int64')
f1 = function([], expectRvs, no_default_updates=True)
f2 = function([], expectRvs)