本文整理汇总了Python中theano.tensor.shared_randomstreams.RandomStreams.normal方法的典型用法代码示例。如果您正苦于以下问题:Python RandomStreams.normal方法的具体用法?Python RandomStreams.normal怎么用?Python RandomStreams.normal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor.shared_randomstreams.RandomStreams
的用法示例。
在下文中一共展示了RandomStreams.normal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_normal_vector
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
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])
示例2: prediction
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
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)
示例3: kmeans
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
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
示例4: KmeansMiniBatch
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class KmeansMiniBatch(object):
def __init__(self, batch_size, data=None, K=300, epsilon_whitening=0.015):
if data is None:
self.X = T.matrix('X_train')
else:
self.X = data
########################
# Normalize the inputs #
########################
# A constant added to the variance to avoid division by zero
self.epsilon_norm = 10
self.epsilon_whitening = epsilon_whitening
# We subtract from each training sample (each column in X_train) its mean
self.X = self.X - T.mean(self.X, axis=0) / T.sqrt(T.var(self.X, axis=0) + self.epsilon_norm)
#####################
# Whiten the inputs #
#####################
sigma = T.dot(self.X, T.transpose(self.X)) / self.X.shape[1]
U, s, V = linalg.svd(sigma, full_matrices=False)
tmp = T.dot(U, T.diag(1/T.sqrt(s + self.epsilon_whitening)))
tmp = T.dot(tmp, T.transpose(U))
self.X = T.dot(tmp, self.X)
##################
# Initialization #
##################
self.K = K # The number of clusters
self.dimensions = self.X.shape[0]
self.samples = batch_size
self.srng = RandomStreams(seed=234)
# We initialize the centroids by sampling them from a normal
# distribution, and then normalizing them to unit length
# D \in R^{n \times k}
self.D = self.srng.normal(size=(self.dimensions, self.K))
self.D = self.D / T.sqrt(T.sum(T.sqr(self.D), axis=0))
def fit_once(self):
# Initialize new point representations
# for every pass of the algorithm
S = T.zeros((self.K, self.samples))
tmp = T.dot(self.D.T, self.X)
res = T.argmax(tmp, axis=0)
max_values = tmp[res, T.arange(self.samples)]
S = T.set_subtensor(S[res, T.arange(self.samples)], max_values)
self.D = T.dot(self.X, T.transpose(S))
self.D = self.D / T.sqrt(T.sum(T.sqr(self.D), axis=0))
return self.D
示例5: Kmeans
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def Kmeans(X_train=None, K=300, epsilon_whitening=0.015):
if X_train is None:
X_train = T.matrix("X_train")
########################
# Normalize the inputs #
########################
# A constant added to the variance to avoid division by zero
epsilon_norm = 10
# We subtract from each training sample (each column in X_train) its mean
X_train = X_train - T.mean(X_train, axis=0) / T.sqrt(T.var(X_train, axis=0) + epsilon_norm)
#####################
# Whiten the inputs #
#####################
sigma = T.dot(X_train, T.transpose(X_train)) / X_train.shape[1]
U, s, V = linalg.svd(sigma, full_matrices=False)
tmp = T.dot(U, T.diag(1 / T.sqrt(s + epsilon_whitening)))
tmp = T.dot(tmp, T.transpose(U))
X_Whitened = T.dot(tmp, X_train)
######################
# Training the Model #
######################
# Initialization
dimensions = X_Whitened.shape[0]
samples = X_Whitened.shape[1]
srng = RandomStreams(seed=234)
# We initialize the centroids by sampling them from a normal
# distribution, and then normalizing them to unit length
# D \in R^{n \times k}
D = srng.normal(size=(dimensions, K))
D = D / T.sqrt(T.sum(T.sqr(D), axis=0))
iterations = 30
for i in xrange(iterations):
# Initialize new point representations
# for every pass of the algorithm
S = T.zeros((K, samples))
tmp = T.dot(D.T, X_Whitened)
res = T.argmax(tmp, axis=0)
max_values = tmp[res, T.arange(samples)]
S = T.set_subtensor(S[res, T.arange(samples)], max_values)
D = T.dot(X_Whitened, T.transpose(S))
D = D / T.sqrt(T.sum(T.sqr(D), axis=0))
return D
示例6: GaussianNoise
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class GaussianNoise(Layer):
def __init__(self,std):
self.std = numpy.array(std).astype(theano.config.floatX)
self.rng = RandomStreams(numpy.random.randint(1234))
def forward(self,x):
print "Layer/GaussianNoise"
noise = self.rng.normal(std=self.std,size=x.shape)
return x + noise
示例7: __init__
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class Noise:
def __init__(self, dim_out, std=1e-2):
self.dim_out = dim_out
self.std = theano.shared(std)
self.rng = RandomStreams()
self.inputs = T.matrix()
self.cmp = theano.function([self.inputs], self.apply(self.inputs))
def apply(self, inputs):
return inputs + self.rng.normal(std=self.std, size=(inputs.shape[0], self.dim_out), dtype=config.floatX)
示例8: random_stuff
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def random_stuff():
rng = RandomStreams(seed = None)
a = rng.uniform((10, 10))
b = rng.normal((10, 1))
tdbplot(a, 'a', ImagePlot(cmap = 'jet'))
tdbplot(b, 'b', HistogramPlot(edges = np.linspace(-5, 5, 20)))
c = a+b
return c
示例9: __theano__noise
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def __theano__noise(self, inp, noisetype, p=None, n=None, sigma=None, thicken=True, mode=None, srng=None):
# Local imports
from theano.tensor.shared_randomstreams import RandomStreams
# Parse noise type and check arguments
if noisetype in ['binomial', 'dropout']:
noisetype = 'binomial'
assert None not in [n, p], "n and p must be provided for binomial noise."
mode = 'mul' if mode is None else mode
elif noisetype in ['gaussian', 'normal']:
noisetype = 'normal'
assert sigma is not None, "sigma must be provided for normal noise."
mode = 'add' if mode is None else mode
else:
raise NotImplementedError("Unknown noisetype: {}".format(noisetype))
# Parse mode
if mode in ['add', 'additive', 'addition']:
mode = 'add'
elif mode in ['mul', 'multiplicative', 'multiplication', 'multiply']:
mode = 'mul'
else:
raise NotImplementedError("Mode {} is not implemented.".format(mode))
# Default rng
if srng is None:
srng = RandomStreams(seed=42)
elif isinstance(srng, int):
srng = RandomStreams(seed=srng)
# Make noise kernel
if noisetype == 'normal':
noisekernel = T.cast(srng.normal(size=inp.shape, std=sigma), dtype='floatX')
elif noisetype == 'binomial':
noisekernel = T.cast(srng.binomial(size=inp.shape, n=n, p=p), dtype='floatX')
else:
raise NotImplementedError
# Couple with input
if mode == 'add':
y = inp + noisekernel
elif mode == 'mul':
y = inp * noisekernel
else:
raise NotImplementedError
if thicken and noisetype is 'binomial':
y = y / getattr(np, th.config.floatX)(p)
# Return
return y
示例10: test_tutorial
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def test_tutorial(self):
srng = RandomStreams(seed=234)
rv_u = srng.uniform((2, 2))
rv_n = srng.normal((2, 2))
f = function([], rv_u)
g = function([], rv_n, no_default_updates=True) # Not updating rv_n.rng
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
assert numpy.all(f() != f())
assert numpy.all(g() == g())
assert numpy.all(abs(nearly_zeros()) < 1e-5)
assert isinstance(rv_u.rng.get_value(borrow=True), numpy.random.RandomState)
示例11: FastDropoutLayer
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class FastDropoutLayer(Layer):
""" Basic linear transformation layer (W.X + b) """
def __init__(self, rng):
super(FastDropoutLayer, self).__init__()
seed = rng.randint(2 ** 30)
self.srng = RandomStreams(seed)
def output_func(self, input):
mask = self.srng.normal(size=input.shape, avg=1., dtype=theano.config.floatX)
return input * mask
def __repr__(self):
return "{}".format(self.__class__.__name__)
示例12: RectifiedNoisyVar1
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class RectifiedNoisyVar1(ActivationFunction):
def __init__(self):
self.theanoGenerator = RandomStreams(seed=np.random.randint(1, 1000))
def nonDeterminstic(self, x):
x += self.theanoGenerator.normal(avg=0.0, std=1.0)
return x * (x > 0.0)
def deterministic(self, x):
return expectedValueRectified(x, 1.0)
def activationProbablity(self, x):
return 1.0 - cdf(0, miu=x, variance=1.0)
示例13: ApproximatedRectifiedNoisy
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
class ApproximatedRectifiedNoisy(ActivationFunction):
def __init__(self):
self.theanoGenerator = RandomStreams(seed=np.random.randint(1, 1000))
def nonDeterminstic(self, x):
x += self.theanoGenerator.normal(avg=0.0, std=(T.sqrt(T.nnet.sigmoid(x)) + 1e-8))
return x * (x > 0.0)
def deterministic(self, x):
return expectedValueRectified(x, T.nnet.sigmoid(x) + 1e-08)
def activationProbablity(self, x):
return 1.0 - cdf(0, miu=x, variance=T.nnet.sigmoid(x))
示例14: LDS_finite_diff
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def LDS_finite_diff(x, forward_func, main_obj_type, epsilon, norm_constraint="L2", num_power_iter=1, xi=1e-6):
rng = RandomStreams(seed=numpy.random.randint(1234))
y = forward_func(x)
d = rng.normal(size=x.shape, dtype=theano.config.floatX)
# power_iteration
for power_iter in xrange(num_power_iter):
d = xi * get_normalized_vector(d)
y_d = forward_func(x + d)
Hd = T.grad(get_kl(y_d, y, main_obj_type).mean(), wrt=d) / xi
Hd = theano.gradient.disconnected_grad(Hd)
d = Hd
r_vadv = get_perturbation(d, epsilon, norm_constraint)
return -get_kl(forward_func(x + r_vadv), y, main_obj_type, include_ent_term=True)
示例15: load_data
# 需要导入模块: from theano.tensor.shared_randomstreams import RandomStreams [as 别名]
# 或者: from theano.tensor.shared_randomstreams.RandomStreams import normal [as 别名]
def load_data(self):
srng = RandomStreams(seed=234)
rv_q = srng.uniform((10 * 10, 15000)).eval()
rv_t1 = srng.normal((10 * 100, 15000)).eval()
rv_t2 = srng.normal((10 * 40, 15000)).eval()
shared_q = theano.shared(
np.asarray(rv_q,
dtype=theano.config.floatX),
name='q', borrow=True)
shared_t1 = theano.shared(
np.asarray(rv_t1,
dtype=theano.config.floatX),
name='t1', borrow=True)
shared_t2 = theano.shared(
np.asarray(rv_t2,
dtype=theano.config.floatX),
name='t2', borrow=True)
return shared_q, shared_t1, shared_t2