本文整理汇总了Python中theano.tensor.shared_randomstreams.RandomStreams类的典型用法代码示例。如果您正苦于以下问题:Python RandomStreams类的具体用法?Python RandomStreams怎么用?Python RandomStreams使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RandomStreams类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: common_init
def common_init(self, mr, vr, sr, di, ce, node_id):
"""
Initialization function used by the base class and subclasses.
"""
self.MEANRATE = mr
self.VARRATE = vr
self.STARVRATE = sr
self.DIMS = di
self.CENTS = ce
self.ID = node_id
srng = RandomStreams(seed=100)
rv_u = srng.uniform((self.CENTS, self.DIMS))
f = function([], rv_u)
self.mean = 2*f()
#print self.mean
var1 = T.dscalar('var1')
var2 = T.dmatrix('var2')
var3 = T.mul
self.var = theanoScaMatMul(0.001,np.ones((self.CENTS, self.DIMS)))
self.starv = np.ones((self.CENTS, 1))
self.belief = np.zeros((1, self.CENTS))
self.children = []
self.last = np.zeros((1, self.DIMS))
self.whitening = False
示例2: theano_sentence_prediction
def theano_sentence_prediction(self, Vs):
#Make pairwise features. This is really just "tensor product with concatenation instead of multiplication". Is there a command for that?
pairwise_vs, _ = theano.scan(fn=self.__pairwise_features,
outputs_info=None,
sequences=Vs,
non_sequences=[Vs, Vs.shape[0]])
if self.input_lstm_layer.training:
srng = RandomStreams(seed=12345)
full_matrix = self.input_lstm_layer.function(pairwise_vs)
for layer in self.lstm_layers:
if self.input_lstm_layer.training:
print("hah-train")
full_matrix = T.switch(srng.binomial(size=(Vs.shape[0], Vs.shape[0]+1, self.hidden_dimension*4), p=0.5), full_matrix, 0)
else:
print("heh-notrain")
full_matrix = 0.5 * full_matrix
full_matrix = layer.function(full_matrix)
if self.input_lstm_layer.training:
print("hah-train")
full_matrix = T.switch(srng.binomial(size=(Vs.shape[0], Vs.shape[0]+1, self.hidden_dimension*4), p=0.5), full_matrix, 0)
else:
print("heh-notrain")
full_matrix = 0.5 * full_matrix
final_matrix = self.output_convolution.function(full_matrix)
return T.nnet.softmax(final_matrix)
示例3: __init__
def __init__(self, neurons, dimensions, count = 1, max_rate = (200, 300),
intercept = (-1.0, 1.0), t_ref = 0.002, t_rc = 0.02, seed = None,
type = 'lif', dt = 0.001, encoders = None, name = None, address = "localhost"):
self.seed = seed
self.neurons = neurons
self.dimensions = dimensions
self.count = count
self.name = name
self.address = address
self.ticker_conn = None
# create the neurons
# TODO: handle different neuron types, which may have different parameters to pass in
self.neuron = neuron.names[type]((count, self.neurons), t_rc = t_rc, t_ref = t_ref, dt = dt)
# compute alpha and bias
srng = RandomStreams(seed=seed)
max_rates = srng.uniform([neurons], low=max_rate[0], high=max_rate[1])
threshold = srng.uniform([neurons], low=intercept[0], high=intercept[1])
alpha, self.bias = theano.function([], self.neuron.make_alpha_bias(max_rates,threshold))()
self.bias = self.bias.astype('float32')
# compute encoders
self.encoders = make_encoders(neurons, dimensions, srng, encoders=encoders)
self.encoders = (self.encoders.T * alpha).T
# make default origin
self.origin = dict(X=origin.Origin(self))
self.accumulator = {}
示例4: _dropout_from_layer
def _dropout_from_layer(self, layer):
stream = RandomStreams(self.numpy_range.randint(999999))
mask = stream.binomial(size=layer.shape, n=1, p=(1-self._p), dtype=theano.config.floatX)
return layer * Tensor.cast(mask, theano.config.floatX)
示例5: test_binomial_vector
def test_binomial_vector(self):
random = RandomStreams(utt.fetch_seed())
n = tensor.lvector()
prob = tensor.vector()
out = random.binomial(n=n, p=prob)
assert out.ndim == 1
f = function([n, prob], out)
n_val = [1, 2, 3]
prob_val = numpy.asarray([.1, .2, .3], dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(n_val, prob_val)
numpy_val0 = numpy_rng.binomial(n=n_val, p=prob_val)
assert numpy.all(val0 == numpy_val0)
# arguments of size (2,)
val1 = f(n_val[:-1], prob_val[:-1])
numpy_val1 = numpy_rng.binomial(n=n_val[:-1], p=prob_val[:-1])
assert numpy.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([n, prob], random.binomial(n=n, p=prob, size=(3,)))
val2 = g(n_val, prob_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.binomial(n=n_val, p=prob_val, size=(3,))
assert numpy.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], prob_val[:-1])
示例6: test_uniform_vector
def test_uniform_vector(self):
random = RandomStreams(utt.fetch_seed())
low = tensor.dvector()
high = tensor.dvector()
out = random.uniform(low=low, high=high)
assert out.ndim == 1
f = function([low, high], out)
low_val = [.1, .2, .3]
high_val = [1.1, 2.2, 3.3]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(low_val, high_val)
numpy_val0 = numpy_rng.uniform(low=low_val, high=high_val)
print('THEANO', val0)
print('NUMPY', numpy_val0)
assert numpy.all(val0 == numpy_val0)
# arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy_rng.uniform(low=low_val[:-1], high=high_val[:-1])
print('THEANO', val1)
print('NUMPY', numpy_val1)
assert numpy.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([low, high], random.uniform(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.uniform(low=low_val, high=high_val, size=(3,))
assert numpy.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
示例7: _negative_sampling
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
示例8: test_multinomial_vector
def test_multinomial_vector(self):
random = RandomStreams(utt.fetch_seed())
n = tensor.lvector()
pvals = tensor.matrix()
out = random.multinomial(n=n, pvals=pvals)
assert out.ndim == 2
f = function([n, pvals], out)
n_val = [1, 2, 3]
pvals_val = [[.1, .9], [.2, .8], [.3, .7]]
pvals_val = numpy.asarray(pvals_val, dtype=config.floatX)
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(n_val, pvals_val)
numpy_val0 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val0 == numpy_val0)
# arguments of size (2,)
val1 = f(n_val[:-1], pvals_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val[:-1], pvals_val[:-1])])
assert numpy.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([n, pvals], random.multinomial(n=n, pvals=pvals, size=(3,)))
val2 = g(n_val, pvals_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.multinomial(n=nv, pvals=pv)
for nv, pv in zip(n_val, pvals_val)])
assert numpy.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, n_val[:-1], pvals_val[:-1])
示例9: test_random_integers_vector
def test_random_integers_vector(self):
random = RandomStreams(utt.fetch_seed())
low = tensor.lvector()
high = tensor.lvector()
out = random.random_integers(low=low, high=high)
assert out.ndim == 1
f = function([low, high], out)
low_val = [100, 200, 300]
high_val = [110, 220, 330]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(low_val, high_val)
numpy_val0 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)])
assert numpy.all(val0 == numpy_val0)
# arguments of size (2,)
val1 = f(low_val[:-1], high_val[:-1])
numpy_val1 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val[:-1], high_val[:-1])])
assert numpy.all(val1 == numpy_val1)
# Specifying the size explicitly
g = function([low, high], random.random_integers(low=low, high=high, size=(3,)))
val2 = g(low_val, high_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy.asarray([numpy_rng.randint(low=lv, high=hv+1)
for lv, hv in zip(low_val, high_val)])
assert numpy.all(val2 == numpy_val2)
self.assertRaises(ValueError, g, low_val[:-1], high_val[:-1])
示例10: prediction
def prediction(self, h, bias):
srng = RandomStreams(seed=42)
prop, mean_x, mean_y, std_x, std_y, rho, bernoulli = \
self.compute_parameters(h, bias)
mode = T.argmax(srng.multinomial(pvals=prop, dtype=prop.dtype), axis=1)
v = T.arange(0, mean_x.shape[0])
m_x = mean_x[v, mode]
m_y = mean_y[v, mode]
s_x = std_x[v, mode]
s_y = std_y[v, mode]
r = rho[v, mode]
# cov = r * (s_x * s_y)
normal = srng.normal((h.shape[0], 2))
x = normal[:, 0]
y = normal[:, 1]
# x_n = T.shape_padright(s_x * x + cov * y + m_x)
# y_n = T.shape_padright(s_y * y + cov * x + m_y)
x_n = T.shape_padright(m_x + s_x * x)
y_n = T.shape_padright(m_y + s_y * (x * r + y * T.sqrt(1.-r**2)))
uniform = srng.uniform((h.shape[0],))
pin = T.shape_padright(T.cast(bernoulli > uniform, floatX))
return T.concatenate([x_n, y_n, pin], axis=1)
示例11: __init__
def __init__(self, n_visible, n_hidden, weights=None, hidden_bias=None, visible_bias=None, random_on_gpu=False,
seed=69, activation=T.nnet.sigmoid):
self.n_visible = n_visible
self.n_hidden = n_hidden
if random_on_gpu:
self.t_rng = GPU_RandomStreams(seed)
else:
self.t_rng = RandomStreams(seed)
if not weights:
weights = np.asarray(
np.random.normal(
scale=0.01,
size=(self.n_visible, self.n_hidden)),
dtype=theano.config.floatX)
self.ts_weights = theano.shared(value=weights, name='W', borrow=True)
if not hidden_bias:
hidden_bias = np.zeros(n_hidden, dtype=theano.config.floatX)
self.ts_hidden_bias = theano.shared(value=hidden_bias, name='hb', borrow=True)
if not visible_bias:
visible_bias = np.zeros(n_visible, dtype=theano.config.floatX)
self.ts_visible_bias = theano.shared(value=visible_bias, name='vb', borrow=True)
self.x = T.matrix(name='x')
self.activation = activation
self.params = [self.ts_weights, self.ts_hidden_bias, self.ts_visible_bias]
示例12: theano_sentence_prediction
def theano_sentence_prediction(self, Sentence, Chars, WordLengths):
input_lstm_res_f = self.input_lstm_forward_layer.function(Sentence, Chars, WordLengths)
input_lstm_res_b = self.input_lstm_backward_layer.function(Sentence, Chars, WordLengths)
input_combined = T.concatenate((input_lstm_res_f, input_lstm_res_b), axis=1)
#Make pairwise features. This is really just "tensor product with concatenation instead of multiplication". Is there a command for that?
full_matrix, _ = theano.scan(fn=self.__pairwise_features,
outputs_info=None,
sequences=input_combined,
non_sequences=[input_combined, Sentence.shape[0]])
if len(self.lstm_layers) > 0 and self.lstm_layers[0].training:
srng = RandomStreams(seed=12345)
full_matrix = T.switch(srng.binomial(size=(Sentence.shape[0], Sentence.shape[0]+1, self.hidden_dimension*4), p=0.5), full_matrix, 0)
else:
full_matrix = 0.5 * full_matrix
full_matrix = self.transition_layer.function(full_matrix)
for layer in self.lstm_layers:
if layer.training:
print("hah-train")
full_matrix = T.switch(srng.binomial(size=(Sentence.shape[0], Sentence.shape[0]+1, self.hidden_dimension*4), p=0.5), full_matrix, 0)
else:
print("heh-notrain")
full_matrix = 0.5 * full_matrix
full_matrix = layer.function(full_matrix)
final_matrix = self.output_convolution.function(full_matrix)
return T.nnet.softmax(final_matrix)
示例13: test_default_dtype
def test_default_dtype(self):
random = RandomStreams(utt.fetch_seed())
low = tensor.dscalar()
high = tensor.dscalar()
# Should not silently downcast from low and high
out0 = random.uniform(low=low, high=high, size=(42,))
assert out0.dtype == 'float64'
f0 = function([low, high], out0)
val0 = f0(-2.1, 3.1)
assert val0.dtype == 'float64'
# Should downcast, since asked explicitly
out1 = random.uniform(low=low, high=high, size=(42,), dtype='float32')
assert out1.dtype == 'float32'
f1 = function([low, high], out1)
val1 = f1(-1.1, 1.1)
assert val1.dtype == 'float32'
# Should use floatX
lowf = tensor.fscalar()
highf = tensor.fscalar()
outf = random.uniform(low=lowf, high=highf, size=(42,))
assert outf.dtype == config.floatX
ff = function([lowf, highf], outf)
valf = ff(numpy.float32(-0.1), numpy.float32(0.3))
assert valf.dtype == config.floatX
示例14: __init__
def __init__(self, rng, train_input, test_input, n_in, n_out):
# self.input = input.flatten(2)
self.W = theano.shared(
value=numpy.asarray(
rng.uniform(
low=-numpy.sqrt(6. / (n_in + n_out)),
high=numpy.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)
),
dtype=theano.config.floatX
),
name='W',
borrow=True
)
self.b = theano.shared(
value=numpy.zeros((n_out,), dtype=theano.config.floatX),
name='b',
borrow=True
)
p = 0.5
tmp_output = T.nnet.relu(T.dot(train_input.flatten(2), self.W) + self.b)
srng = RandomStreams(rng.randint(1234))
mask = (srng.uniform(size=tmp_output.shape) < p)/p
self.train_output = tmp_output * mask
self.test_output = T.nnet.relu(T.dot(test_input.flatten(2), self.W) + self.b)
self.params = [self.W, self.b]
示例15: test_normal_vector
def test_normal_vector(self):
random = RandomStreams(utt.fetch_seed())
avg = tensor.dvector()
std = tensor.dvector()
out = random.normal(avg=avg, std=std)
assert out.ndim == 1
f = function([avg, std], out)
avg_val = [1, 2, 3]
std_val = [.1, .2, .3]
seed_gen = numpy.random.RandomState(utt.fetch_seed())
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
# Arguments of size (3,)
val0 = f(avg_val, std_val)
numpy_val0 = numpy_rng.normal(loc=avg_val, scale=std_val)
assert numpy.allclose(val0, numpy_val0)
# arguments of size (2,)
val1 = f(avg_val[:-1], std_val[:-1])
numpy_val1 = numpy_rng.normal(loc=avg_val[:-1], scale=std_val[:-1])
assert numpy.allclose(val1, numpy_val1)
# Specifying the size explicitly
g = function([avg, std], random.normal(avg=avg, std=std, size=(3,)))
val2 = g(avg_val, std_val)
numpy_rng = numpy.random.RandomState(int(seed_gen.randint(2**30)))
numpy_val2 = numpy_rng.normal(loc=avg_val, scale=std_val, size=(3,))
assert numpy.allclose(val2, numpy_val2)
self.assertRaises(ValueError, g, avg_val[:-1], std_val[:-1])