本文整理汇总了Python中theano.tensor.shared_randomstreams.RandomStreams类的典型用法代码示例。如果您正苦于以下问题:Python RandomStreams类的具体用法?Python RandomStreams怎么用?Python RandomStreams使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RandomStreams类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:cosmoharrigan,项目名称:python-destin,代码行数:25,代码来源:Clustering.py
示例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)
开发者ID:MichSchli,项目名称:Speciale,代码行数:32,代码来源:corner_lstm.py
示例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 = {}
开发者ID:LouisCastricato,项目名称:spike,代码行数:29,代码来源:ensemble.py
示例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)
开发者ID:aviveise,项目名称:double_encoder,代码行数:7,代码来源:symmetric_dropout_hidden_layer.py
示例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])
开发者ID:ChinaQuants,项目名称:Theano,代码行数:30,代码来源:test_shared_randomstreams.py
示例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])
开发者ID:ChinaQuants,项目名称:Theano,代码行数:34,代码来源:test_shared_randomstreams.py
示例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
开发者ID:cvangysel,项目名称:SERT,代码行数:33,代码来源:models.py
示例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])
开发者ID:ChinaQuants,项目名称:Theano,代码行数:34,代码来源:test_shared_randomstreams.py
示例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])
开发者ID:ChinaQuants,项目名称:Theano,代码行数:33,代码来源:test_shared_randomstreams.py
示例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)
开发者ID:adbrebs,项目名称:handwriting,代码行数:30,代码来源:model.py
示例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]
开发者ID:Craftinity,项目名称:DA-AGH,代码行数:33,代码来源:autoencoders.py
示例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)
开发者ID:MichSchli,项目名称:Speciale,代码行数:34,代码来源:fourway_lstm_both.py
示例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
开发者ID:ChinaQuants,项目名称:Theano,代码行数:27,代码来源:test_shared_randomstreams.py
示例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]
开发者ID:yangli625,项目名称:ReId_theano,代码行数:32,代码来源:Layer.py
示例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])
开发者ID:ChinaQuants,项目名称:Theano,代码行数:30,代码来源:test_shared_randomstreams.py
示例16: CTC_train
def CTC_train(self):
CTC_LOSSs = T.cast(T.mean(self.CTC_LOSS(), axis=0), "float32")
train_data_d = []
train_data_m = []
train_data_m_s = []
learning_rate = T.scalar()
decay = T.scalar()
seed = np.random.randint(10e6)
rng = RandomStreams(seed=seed)
grad_rate = 0.8
for data in self.train_data:
data_d = rng.binomial((1,), p=grad_rate, dtype="float32")[0]*T.grad(CTC_LOSSs, data)
train_data_d.append(data_d)
data_m_s = theano.shared(np.zeros(data.get_value().shape).astype(np.float32))
train_data_m_s.append(data_m_s)
data_m = data_m_s*decay + (1-decay)*data_d**2
train_data_m.append(data_m)
#self.grad_test = theano.function([self.X, self.Y], train_data_d[-4])
#self.data_d_print = theano.function([self.X,self.Y],train_data_d[0][0])
#upd = [(d,d-learning_rate*d_d)for d,d_d in zip(self.train_data,train_data_d)]
upd = [(d, d-learning_rate*d_d/T.sqrt(d_m+1e-4))for d,d_d,d_m in zip(self.train_data,train_data_d,train_data_m)]
upd1 = [(d_m_s, decay*d_m_s+(1-decay)*d_d**2) for d_m_s,d_d in zip(train_data_m_s,train_data_d)]
upd +=upd1
#self.test = theano.function([self.X,self.Y],train_data_d[0])
self.sgd_train = theano.function([self.X, self.Y, learning_rate, decay],
[],
updates = upd
)
开发者ID:nightinwhite,项目名称:Theano-NN_Starter,代码行数:28,代码来源:Layer.py
示例17: __init__
def __init__(self, input, filter_shape, corruption_level = 0.1,
shared_W = None, shared_b = None, image_shape = None,
poolsize = (2,2)):
theano_rng = RandomStreams()
fan_in = numpy.prod(filter_shape[1:])
fan_out = filter_shape[0] * numpy.prod(filter_shape[2:])
center = theano.shared(value = 1, name="center")
scale = theano.shared(value = 2, name="scale")
if shared_W != None and shared_b != None :
self.W = shared_W
self.b = shared_b
else:
initial_W = numpy.asarray( numpy.random.uniform(
low = -numpy.sqrt(6./(fan_in+fan_out)),
high = numpy.sqrt(6./(fan_in+fan_out)),
size = filter_shape), dtype = theano.config.floatX)
initial_b = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.W = theano.shared(value = initial_W, name = "W")
self.b = theano.shared(value = initial_b, name = "b")
initial_b_prime= numpy.zeros((filter_shape[1],),dtype=theano.config.floatX)
self.b_prime = theano.shared(value = initial_b_prime, name = "b_prime")
self.x = input
self.tilde_x = theano_rng.binomial( self.x.shape, 1, 1 - corruption_level,dtype=theano.config.floatX) * self.x
conv1_out = conv.conv2d(self.tilde_x, self.W, filter_shape=filter_shape,
image_shape=image_shape, border_mode='valid')
self.y = T.tanh(conv1_out + self.b.dimshuffle('x', 0, 'x', 'x'))
da_filter_shape = [ filter_shape[1], filter_shape[0],
filter_shape[2], filter_shape[3] ]
initial_W_prime = numpy.asarray( numpy.random.uniform( \
low = -numpy.sqrt(6./(fan_in+fan_out)), \
high = numpy.sqrt(6./(fan_in+fan_out)), \
size = da_filter_shape), dtype = theano.config.floatX)
self.W_prime = theano.shared(value = initial_W_prime, name = "W_prime")
conv2_out = conv.conv2d(self.y, self.W_prime,
filter_shape = da_filter_shape,
border_mode='full')
self.z = (T.tanh(conv2_out + self.b_prime.dimshuffle('x', 0, 'x', 'x'))+center) / scale
scaled_x = (self.x + center) / scale
self.L = - T.sum( scaled_x*T.log(self.z) + (1-scaled_x)*T.log(1-self.z), axis=1 )
self.cost = T.mean(self.L)
self.params = [ self.W, self.b, self.b_prime ]
开发者ID:sauravbiswasiupr,项目名称:image_transformations,代码行数:59,代码来源:stacked_convolutional_dae.py
示例18: __init__
def __init__(self,X,mask,shape,is_train=1,p=0.5,state_pre=None):
prefix="GRU"
self.in_size,self.hidden_size=shape
self.W_xr=theano.shared(value=np.asarray((np.random.randn(self.in_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_xr')
self.W_hr=theano.shared(value=np.asarray((np.random.randn(self.hidden_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_hr')
self.b_r=theano.shared(value=np.asarray(np.zeros(self.hidden_size),dtype=theano.config.floatX),
name=prefix+'_b_r')
self.W_xz=theano.shared(value=np.asarray((np.random.randn(self.in_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_xz')
self.W_hz=theano.shared(value=np.asarray((np.random.randn(self.hidden_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_hz')
self.b_z=theano.shared(value=np.asarray(np.zeros(self.hidden_size),dtype=theano.config.floatX),
name=prefix+'_b_z')
self.W_xh=theano.shared(value=np.asarray((np.random.randn(self.in_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_xh')
self.W_hh=theano.shared(value=np.asarray((np.random.randn(self.hidden_size,self.hidden_size) * 0.1),dtype=theano.config.floatX),
name=prefix+'_W_hh')
self.b_h=theano.shared(value=np.asarray(np.zeros(self.hidden_size),dtype=theano.config.floatX),
name=prefix+'_b_h')
self.X=X
self.mask=mask
batch_size=self.X.shape[1]
if state_pre==None:
state_pre=T.zeros((batch_size,self.hidden_size),dtype=theano.config.floatX)
def _step(x,m,h_tm1):
r=T.nnet.sigmoid(T.dot(x,self.W_xr) + T.dot(h_tm1,self.W_hr) +self.b_r)
z=T.nnet.sigmoid(T.dot(x,self.W_xz) + T.dot(h_tm1,self.W_hz) +self.b_z)
gh=T.tanh(T.dot(x , self.W_xh) + T.dot(r * h_tm1 , self.W_hh) + self.b_h)
h_t=z * h_tm1 + (T.ones_like(z) - z) * gh
h_t = h_t * m[:,None]
return h_t
h,_=theano.scan(fn=_step,
sequences=[self.X,self.mask],
outputs_info=state_pre)
self.h=h
if p>0:
trng=RandomStreams(12345)
drop_mask=trng.binomial(n=1,p=1-p,size=h.shape,dtype=theano.config.floatX)
self.activation=T.switch(T.eq(is_train,1),h*drop_mask,h*(1-p))
else:
self.activation=T.switch(T.eq(is_train,1),h,h)
self.params=[self.W_xr,self.W_hr,self.b_r,
self.W_xz,self.W_hz,self.b_z,
self.W_xh,self.W_hh,self.b_h]
开发者ID:jiangnanHugo,项目名称:seq2seq,代码行数:59,代码来源:gru.py
示例19: build_model
def build_model(tparams, options):
trng = RandomStreams(1234)
use_noise = theano.shared(numpy.float32(0.))
# description string: #words x #samples
if options['use_target_as_input']:
x = tensor.tensor3('x', dtype='float32')
else:
x = tensor.matrix('x', dtype='int64')
mask = tensor.matrix('mask', dtype='float32')
# context: #samples x dim
ctx = tensor.matrix('ctx', dtype='float32')
n_timesteps = x.shape[0]
n_samples = x.shape[1]
# word embedding
if options['use_target_as_input']:
emb = x
else:
emb = tparams['Wemb'][x.flatten()].reshape([n_timesteps, n_samples, options['dim_word']])
# decoder
if options.setdefault('feedforward', False):
proj_h = tensor.dot(emb, tparams['Wff'])
proj_h = (proj_h * mask[:,:,None]).sum(axis=0)
proj_h = proj_h / mask.sum(axis=0)[:,None]
elif options.setdefault('regress', False):
proj_h = (emb * mask[:,:,None]).sum(axis=0)
proj_h = tensor.dot(proj_h, tparams['Wff'])
proj_h = proj_h / mask.sum(axis=0)[:,None]
else:
proj = get_layer('lstm')[1](tparams, emb, options,
prefix='encoder',
mask=mask)
proj_h = proj[0]
if options['use_mean']:
proj_h = (proj_h * mask[:,:,None]).sum(axis=0)
proj_h = proj_h / mask.sum(axis=0)[:,None]
else:
proj_h = proj_h[-1]
if 'n_layers' in options:
for lidx in xrange(1, options['n_layers']):
proj_h = get_layer('ff')[1](tparams, proj_h, options, prefix='ff_out_%d'%lidx, activ='tanh')
out = get_layer('ff')[1](tparams, proj_h, options, prefix='ff_out', activ='linear')
# cost
if options['loss_type'] == 'cosine':
out = out / tensor.sqrt((out ** 2).sum(1))[:,None]
cost = 1. - (out * ctx).sum(1)
elif options['loss_type'] == 'ranking':
out = out / tensor.sqrt((out ** 2).sum(1))[:,None]
rndidx = trng.permutation(n=ctx.shape[0])
ctx_rnd = ctx[rndidx]
cost = tensor.maximum(0., 1 - (out * ctx).sum(1) + (out * ctx_rnd).sum(1))
else:
raise Exception('Unknown loss function')
return trng, use_noise, x, mask, ctx, cost
开发者ID:JHnlp,项目名称:DefGen2,代码行数:59,代码来源:defgen_rev.py
示例20: kmeans
def kmeans(train_set_x):
if train_set_x is None:
train_set_x = T.matrix('train_set_x')
########################
# Normalize the inputs #
########################
epsilon_norm = 10
epsilon_zca = 0.015
K = 500
train_set_x = train_set_x - T.mean(train_set_x, axis=0) / T.sqrt(T.var(train_set_x, axis=0) + epsilon_norm)
#####################
# Whiten the inputs #
#####################
# a simple choice of whitening transform is the ZCA whitening transform
# epsilon_zca is small constant
# for contrast-normalizaed data, setting epsilon_zca to 0.01 for 16-by-16 pixel patches,
# or to 0.1 for 8-by-8 pixel patches
# is good starting point
cov = T.dot(train_set_x, T.transpose(train_set_x)) / train_set_x.shape[1]
U, S, V = linalg.svd(cov)
tmp = T.dot(U, T.diag(1/T.sqrt(S + epsilon_zca)))
tmp = T.dot(tmp, T.transpose(U))
whitened_x = T.dot(tmp, train_set_x)
######################
# Training the Model #
######################
# Initialization
dimension_size = whitened_x.shape[0]
num_samples = whitened_x.shape[1]
srng = RandomStreams(seed=234)
D = srng.normal(size=(dimension_size, K))
D = D / T.sqrt(T.sum(T.sqr(D), axis=0))
# typically 10 iterations is enough
num_iteration = 15
# compute new centroids, D_new
for i in xrange(num_iteration):
dx = T.dot(D.T, whitened_x)
arg_max_dx = T.argmax(dx, axis=0)
s = dx[arg_max_dx, T.arange(num_samples)]
S = T.zeros((K, num_samples))
S = T.set_subtensor(S[arg_max_dx, T.arange(num_samples)], s)
D = T.dot(whitened_x, T.transpose(S)) + D
D = D / T.sqrt(T.sum(T.sqr(D), axis=0))
return D
开发者ID:jsungjei,项目名称:Deep-Learning-for-the-Real-World,代码行数:59,代码来源:problem_30.py
注:本文中的theano.tensor.shared_randomstreams.RandomStreams类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论