本文整理汇总了Python中keras.backend.random_normal方法的典型用法代码示例。如果您正苦于以下问题:Python backend.random_normal方法的具体用法?Python backend.random_normal怎么用?Python backend.random_normal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.random_normal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_encoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def build_encoder(self):
# Encoder
img = Input(shape=self.img_shape)
h = Flatten()(img)
h = Dense(512)(h)
h = LeakyReLU(alpha=0.2)(h)
h = Dense(512)(h)
h = LeakyReLU(alpha=0.2)(h)
mu = Dense(self.latent_dim)(h)
log_var = Dense(self.latent_dim)(h)
latent_repr = merge([mu, log_var],
mode=lambda p: p[0] + K.random_normal(K.shape(p[0])) * K.exp(p[1] / 2),
output_shape=lambda p: p[0])
return Model(img, latent_repr)
示例2: sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def sampling(args: tuple):
"""
Reparameterization trick by sampling z from unit Gaussian
:param args: (tensor, tensor) mean and log of variance of q(z|x)
:returns tensor: sampled latent vector z
"""
# unpack the input tuple
z_mean, z_log_var = args
# mini-batch size
mb_size = K.shape(z_mean)[0]
# latent space size
dim = K.int_shape(z_mean)[1]
# random normal vector with mean=0 and std=1.0
epsilon = K.random_normal(shape=(mb_size, dim))
return z_mean + K.exp(0.5 * z_log_var) * epsilon
示例3: sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def sampling(self, args):
"""Reparametrisation by sampling from Gaussian, N(0,I)
To sample from epsilon = Norm(0,I) instead of from likelihood Q(z|X)
with latent variables z: z = z_mean + sqrt(var) * epsilon
Parameters
----------
args : tensor
Mean and log of variance of Q(z|X).
Returns
-------
z : tensor
Sampled latent variable.
"""
z_mean, z_log = args
batch = K.shape(z_mean)[0] # batch size
dim = K.int_shape(z_mean)[1] # latent dimension
epsilon = K.random_normal(shape=(batch, dim)) # mean=0, std=1.0
return z_mean + K.exp(0.5 * z_log) * epsilon
示例4: _compute_probabilities
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def _compute_probabilities(self, energy, previous_attention=None):
if self.is_monotonic:
# add presigmoid noise to encourage discreteness
sigmoid_noise = K.in_train_phase(1., 0.)
noise = K.random_normal(K.shape(energy), mean=0.0, stddev=sigmoid_noise)
# encourage discreteness in train
energy = K.in_train_phase(energy + noise, energy)
p = K.in_train_phase(K.sigmoid(energy),
K.cast(energy > 0, energy.dtype))
p = K.squeeze(p, -1)
p_prev = K.squeeze(previous_attention, -1)
# monotonic attention function from tensorflow
at = K.in_train_phase(
tf.contrib.seq2seq.monotonic_attention(p, p_prev, 'parallel'),
tf.contrib.seq2seq.monotonic_attention(p, p_prev, 'hard'))
at = K.expand_dims(at, -1)
else:
# softmax
at = keras.activations.softmax(energy, axis=1)
return at
示例5: _sample_z
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def _sample_z(args):
"""
Samples from standard Normal distribution with shape [size, z_dim] and
applies re-parametrization trick. It is actually sampling from latent
space distributions with N(mu, var) computed in `_encoder` function.
Parameters
----------
No parameters are needed.
Returns
-------
The computed Tensor of samples with shape [size, z_dim].
"""
mu, log_var = args
batch_size = K.shape(mu)[0]
z_dim = K.shape(mu)[1]
eps = K.random_normal(shape=[batch_size, z_dim])
return mu + K.exp(log_var / 2) * eps
示例6: model_encoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def model_encoder(latent_dim, input_shape, units=512, reg=lambda: l1l2(l1=1e-7, l2=1e-7), dropout=0.5):
k = 5
x = Input(input_shape)
h = Convolution2D(units / 4, k, k, border_mode='same', W_regularizer=reg())(x)
# h = SpatialDropout2D(dropout)(h)
h = MaxPooling2D(pool_size=(2, 2))(h)
h = LeakyReLU(0.2)(h)
h = Convolution2D(units / 2, k, k, border_mode='same', W_regularizer=reg())(h)
# h = SpatialDropout2D(dropout)(h)
h = MaxPooling2D(pool_size=(2, 2))(h)
h = LeakyReLU(0.2)(h)
h = Convolution2D(units / 2, k, k, border_mode='same', W_regularizer=reg())(h)
# h = SpatialDropout2D(dropout)(h)
h = MaxPooling2D(pool_size=(2, 2))(h)
h = LeakyReLU(0.2)(h)
h = Convolution2D(units, k, k, border_mode='same', W_regularizer=reg())(h)
# h = SpatialDropout2D(dropout)(h)
h = LeakyReLU(0.2)(h)
h = Flatten()(h)
mu = Dense(latent_dim, name="encoder_mu", W_regularizer=reg())(h)
log_sigma_sq = Dense(latent_dim, name="encoder_log_sigma_sq", W_regularizer=reg())(h)
z = Lambda(lambda (_mu, _lss): _mu + K.random_normal(K.shape(_mu)) * K.exp(_lss / 2),
output_shape=lambda (_mu, _lss): _mu)([mu, log_sigma_sq])
return Model(x, z, name="encoder")
示例7: model_encoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def model_encoder(latent_dim, input_shape, hidden_dim=1024, reg=lambda: l1l2(1e-5, 0), batch_norm_mode=0):
x = Input(input_shape, name="x")
h = Flatten()(x)
h = Dense(hidden_dim, name="encoder_h1", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
h = Dense(hidden_dim / 2, name="encoder_h2", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
h = Dense(hidden_dim / 4, name="encoder_h3", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
mu = Dense(latent_dim, name="encoder_mu", W_regularizer=reg())(h)
log_sigma_sq = Dense(latent_dim, name="encoder_log_sigma_sq", W_regularizer=reg())(h)
z = merge([mu, log_sigma_sq], mode=lambda p: p[0] + K.random_normal(K.shape(p[0])) * K.exp(p[1] / 2),
output_shape=lambda x: x[0])
return Model(x, z, name="encoder")
示例8: model_encoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def model_encoder(latent_dim, input_shape, hidden_dim=1024, reg=lambda: l1(1e-5), batch_norm_mode=2):
x = Input(input_shape, name="x")
h = Flatten()(x)
h = Dense(hidden_dim, name="encoder_h1", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
h = Dense(hidden_dim / 2, name="encoder_h2", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
h = Dense(hidden_dim / 4, name="encoder_h3", W_regularizer=reg())(h)
h = BatchNormalization(mode=batch_norm_mode)(h)
h = LeakyReLU(0.2)(h)
mu = Dense(latent_dim, name="encoder_mu", W_regularizer=reg())(h)
log_sigma_sq = Dense(latent_dim, name="encoder_log_sigma_sq", W_regularizer=reg())(h)
z = merge([mu, log_sigma_sq], mode=lambda p: p[0] + K.random_normal(p[0].shape) * K.exp(p[1] / 2),
output_shape=lambda x: x[0])
return Model(x, z, name="encoder")
示例9: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def call(self, inputs, training=None):
z, gamma_k = inputs
gamma_k_sum = K.sum(gamma_k)
est_phi = K.mean(gamma_k, axis=0)
est_mu = K.dot(K.transpose(gamma_k), z) / gamma_k_sum
est_sigma = K.dot(K.transpose(z - est_mu),
gamma_k * (z - est_mu)) / gamma_k_sum
est_sigma = est_sigma + (K.random_normal(shape=(K.int_shape(z)[1], 1), mean=1e-3, stddev=1e-4) * K.eye(K.int_shape(z)[1]))
self.add_update(K.update(self.phi, est_phi), inputs)
self.add_update(K.update(self.mu, est_mu), inputs)
self.add_update(K.update(self.sigma, est_sigma), inputs)
est_sigma_diag_inv = K.eye(K.int_shape(self.sigma)[0]) / est_sigma
self.add_loss(self.lambd_diag * K.sum(est_sigma_diag_inv), inputs)
phi = K.in_train_phase(est_phi, self.phi, training)
mu = K.in_train_phase(est_mu, self.mu, training)
sigma = K.in_train_phase(est_sigma, self.sigma, training)
return GaussianMixtureComponent._calc_component_density(z, phi, mu, sigma)
示例10: _buildEncoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def _buildEncoder(self, x, latent_rep_size, max_length, epsilon_std = 0.01):
h = Convolution1D(9, 9, activation = 'relu', name='conv_1')(x)
h = Convolution1D(9, 9, activation = 'relu', name='conv_2')(h)
h = Convolution1D(10, 11, activation = 'relu', name='conv_3')(h)
h = Flatten(name='flatten_1')(h)
h = Dense(435, activation = 'relu', name='dense_1')(h)
def sampling(args):
z_mean_, z_log_var_ = args
batch_size = K.shape(z_mean_)[0]
epsilon = K.random_normal(shape=(batch_size, latent_rep_size), mean=0., std = epsilon_std)
return z_mean_ + K.exp(z_log_var_ / 2) * epsilon
z_mean = Dense(latent_rep_size, name='z_mean', activation = 'linear')(h)
z_log_var = Dense(latent_rep_size, name='z_log_var', activation = 'linear')(h)
def vae_loss(x, x_decoded_mean):
x = K.flatten(x)
x_decoded_mean = K.flatten(x_decoded_mean)
xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis = -1)
return xent_loss + kl_loss
return (vae_loss, Lambda(sampling, output_shape=(latent_rep_size,), name='lambda')([z_mean, z_log_var]))
示例11: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def call(self, x, mask=None, training=None):
m = K.not_equal(x, 0.)
noise_x = x + K.random_normal(shape=K.shape(x),
mean=0.,
stddev=self.sigma)
noise_x = noise_x * K.cast(m, K.floatx())
return K.in_train_phase(noise_x, x, training=training)
开发者ID:microsoft,项目名称:View-Adaptive-Neural-Networks-for-Skeleton-based-Human-Action-Recognition,代码行数:10,代码来源:transform_rnn.py
示例12: _buildEncoder
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def _buildEncoder(self, x, latent_rep_size, max_length, epsilon_std=0.01):
h = Convolution1D(9, 9, activation='relu', name='conv_1')(x)
h = Convolution1D(9, 9, activation='relu', name='conv_2')(h)
h = Convolution1D(10, 11, activation='relu', name='conv_3')(h)
h = Flatten(name='flatten_1')(h)
h = Dense(435, activation='relu', name='dense_1')(h)
def sampling(args):
z_mean_, z_log_var_ = args
batch_size = K.shape(z_mean_)[0]
epsilon = K.random_normal(
shape=(batch_size, latent_rep_size), mean=0., std=epsilon_std)
return z_mean_ + K.exp(z_log_var_ / 2) * epsilon
z_mean = Dense(latent_rep_size, name='z_mean', activation='linear')(h)
z_log_var = Dense(latent_rep_size, name='z_log_var', activation='linear')(h)
def vae_loss(x, x_decoded_mean):
x = K.flatten(x)
x_decoded_mean = K.flatten(x_decoded_mean)
xent_loss = max_length * objectives.binary_crossentropy(x, x_decoded_mean)
kl_loss = -0.5 * K.mean(
1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return xent_loss + kl_loss
return (vae_loss, Lambda(
sampling, output_shape=(latent_rep_size,),
name='lambda')([z_mean, z_log_var]))
示例13: sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def sampling(args):
z_mean, z_log_var = args
epsilon = K.random_normal(shape=(K.shape(z_mean)[0], Z_DIM), mean=0., stddev=1.)
return z_mean + K.exp(z_log_var / 2) * epsilon
示例14: _sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def _sampling(self,args):
'''
sampling function for embedding layer
'''
z_mean,z_log_var = args
epsilon = K.random_normal(shape=K.shape(z_mean),mean=self.eps_mean,
stddev=self.eps_std)
return z_mean + K.exp(z_log_var) * epsilon
示例15: sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import random_normal [as 别名]
def sampling(args):
(z_mean, z_var) = args
epsilon = K.random_normal(shape=(K.shape(z_mean)[0],
LATENT_VAR_DIM), mean=0., stddev=1.)
return z_mean + z_var * epsilon