python中epsilon什么意思_Python backend.epsilon方法代码示例

本文整理汇总了Python中keras.backend.epsilon方法的典型用法代码示例。如果您正苦于以下问题:Python backend.epsilon方法的具体用法?Python backend.epsilon怎么用?Python backend.epsilon使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在模块keras.backend的用法示例。

在下文中一共展示了backend.epsilon方法的23个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: call

​点赞 6

# 需要导入模块: from keras import backend [as 别名]

# 或者: from keras.backend import epsilon [as 别名]

def call(self, x, mask=None):

# computes a probability distribution over the timesteps

# uses 'max trick' for numerical stability

# reshape is done to avoid issue with Tensorflow

# and 1-dimensional weights

logits = K.dot(x, self.W)

x_shape = K.shape(x)

logits = K.reshape(logits, (x_shape[0], x_shape[1]))

ai = K.exp(logits - K.max(logits, axis=-1, keepdims=True))

# masked timesteps have zero weight

if mask is not None:

mask = K.cast(mask, K.floatx())

ai = ai * mask

att_weights = ai / (K.sum(ai, axis=1, keepdims=True) + K.epsilon())

weighted_input = x * K.expand_dims(att_weights)

result = K.sum(weighted_input, axis=1)

if self.return_attention:

return [result, att_weights]

return result

开发者ID:minerva-ml,项目名称:steppy-toolkit,代码行数:22,

示例2: get_config

​点赞 6

# 需要导入模块: from keras import backend [as 别名]

# 或者: from keras.backend import epsilon [as 别名]

def get_config(self):

config = {

'learning_rate': float(K_eval(self.learning_rate)),

'beta_1': float(K_eval(self.beta_1)),

'beta_2': float(K_eval(self.beta_2)),

'decay': float(K_eval(self.decay)),

'batch_size': int(self.batch_size),

'total_iterations': int(self.total_iterations),

'weight_decays': self.weight_decays,

'lr_multipliers': self.lr_multipliers,

'use_cosine_annealing': self.use_cosine_annealing,

't_cur': int(K_eval(self.t_cur)),

'eta_t': float(K_eval(self.eta_t)),

'eta_min': float(K_eval(self.eta_min)),

'eta_max': float(K_eval(self.eta_max)),

'init_verbose': self.init_verbose,

'epsilon': self.epsilon,

'amsgrad': self.amsgrad

}

base_config = super(AdamW, self).get_config()

return dict(list(base_config.items()) + list(config.items()))

开发者ID:OverLordGoldDragon,项目名称:keras-adamw,代码行数:23,

示例3: get_config

​点赞 6

# 需要导入模块: from keras import backend [as 别名]

# 或者: from keras.backend import epsilon [as 别名]

def get_config(self):

config = {

'lr': float(K.get_value(self.lr)),

'beta_1': float(K.get_value(self.beta_1)),

'beta_2': float(K.get_value(self.beta_2)),

'decay': float(K.get_value(self.decay)),

'batch_size': int(self.batch_size),

'total_iterations': int(self.total_iterations),

'weight_decays': self.weight_decays,

'lr_multipliers': self.lr_multipliers,

'use_cosine_annealing': self.use_cosine_annealing,

't_cur': int(K.get_value(self.t_cur)),

'eta_t': float(K.eval(self.eta_t)),

'eta_min': float(K.get_value(self.eta_min)),

'eta_max': float(K.get_value(self.eta_max)),

'init_verbose': self.init_verbose,

'epsilon': self.epsilon,

'amsgrad': self.amsgrad

}

base_config = super(AdamW, self).get_config()

return dict(list(base_config.items()) + list(config.items()))

开发者ID:OverLordGoldDragon,项目名称:keras-adamw,代码行数:23,

示例4: call

​点赞 6

# 需要导入模块: from keras import backend [as 别名]

# 或者: from keras.backend import epsilon [as 别名]

def call(self, x, mask=None):

uit = dot_product(x, self.W)

if self.bias:

uit += self.b

uit = K.tanh(uit)

ait = dot_product(uit, self.u)

a = K.exp(ait)

# apply mask after the exp. will be re-normalized next

if mask is not None:

# Cast the mask to floatX to avoid float64 upcasting in theano

a *= K.cast(mask, K.floatx())

# in some cases especially in the early stages of training the sum may be almost zero

# and this results in NaN's. A workaround is to add a very small positive number ε to the sum.

# a /= K.cast(K.sum(a, axis=1, keepdims=True), K.floatx())

a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())

a = K.expand_dims(a)

weighted_input = x * a

return K.sum(weighted_input, axis=1)

开发者ID:Hsankesara,项目名称:DeepResearch,代码行数:26,

示例5: deprocess_image

​点赞 6

# 需要导入模块: from keras imp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个简单的 Python VAE 算法进行数据生成增强的代码示例: ```python import numpy as np import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers # 加载数据集 (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # 数据预处理 x_train = x_train.reshape(-1, 784).astype("float32") / 255. x_test = x_test.reshape(-1, 784).astype("float32") / 255. # 定义 VAE 模型 latent_dim = 2 encoder_inputs = keras.Input(shape=(784,)) x = layers.Dense(256, activation="relu")(encoder_inputs) x = layers.Dense(128, activation="relu")(x) z_mean = layers.Dense(latent_dim, name="z_mean")(x) z_log_var = layers.Dense(latent_dim, name="z_log_var")(x) encoder = keras.Model(encoder_inputs, [z_mean, z_log_var], name="encoder") latent_inputs = keras.Input(shape=(latent_dim,)) x = layers.Dense(128, activation="relu")(latent_inputs) x = layers.Dense(256, activation="relu")(x) decoder_outputs = layers.Dense(784, activation="sigmoid")(x) decoder = keras.Model(latent_inputs, decoder_outputs, name="decoder") class VAE(keras.Model): def __init__(self, encoder, decoder, **kwargs): super(VAE, self).__init__(**kwargs) self.encoder = encoder self.decoder = decoder def train_step(self, data): if isinstance(data, tuple): data = data[] with tf.GradientTape() as tape: z_mean, z_log_var = self.encoder(data) z = self.sampling((z_mean, z_log_var)) reconstruction = self.decoder(z) reconstruction_loss = tf.reduce_mean( keras.losses.binary_crossentropy(data, reconstruction) ) reconstruction_loss *= 784 kl_loss = 1 + z_log_var - tf.square(z_mean) - tf.exp(z_log_var) kl_loss = tf.reduce_mean(kl_loss) kl_loss *= -.5 total_loss = reconstruction_loss + kl_loss grads = tape.gradient(total_loss, self.trainable_weights) self.optimizer.apply_gradients(zip(grads, self.trainable_weights)) return { "loss": total_loss, "reconstruction_loss": reconstruction_loss, "kl_loss": kl_loss, } def call(self, data): z_mean, z_log_var = self.encoder(data) z = self.sampling((z_mean, z_log_var)) return self.decoder(z) def sampling(self, args): z_mean, z_log_var = args batch = tf.shape(z_mean)[] dim = tf.shape(z_mean)[1] epsilon = tf.keras.backend.random_normal(shape=(batch, dim)) return z_mean + tf.exp(.5 * z_log_var) * epsilon # 训练 VAE 模型 vae = VAE(encoder, decoder) vae.compile(optimizer=keras.optimizers.Adam()) vae.fit(x_train, epochs=30, batch_size=128) # 生成新的数据 n = 10 digit_size = 28 figure = np.zeros((digit_size * n, digit_size * n)) grid_x = np.linspace(-4, 4, n) grid_y = np.linspace(-4, 4, n)[::-1] for i, yi in enumerate(grid_y): for j, xi in enumerate(grid_x): z_sample = np.array([[xi, yi]]) x_decoded = vae.decoder.predict(z_sample) digit = x_decoded[].reshape(digit_size, digit_size) figure[i * digit_size : (i + 1) * digit_size, j * digit_size : (j + 1) * digit_size] = digit # 显示生成的新数据 import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) plt.imshow(figure, cmap="Greys_r") plt.show() ``` 希望这个代码示例能够帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值