本文整理汇总了Python中keras.backend.epsilon方法的典型用法代码示例。如果您正苦于以下问题:Python backend.epsilon方法的具体用法?Python backend.epsilon怎么用?Python backend.epsilon使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.epsilon方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: call
# 需要导入模块: 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
示例2: get_config
# 需要导入模块: 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()))
示例3: get_config
# 需要导入模块: 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()))
示例4: call
# 需要导入模块: 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)
示例5: deprocess_image
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def deprocess_image(x):
# normalize tensor: center on 0., ensure std is 0.1
x -= x.mean()
x /= (x.std() + K.epsilon())
x *= 0.1
# clip to [0, 1]
x += 0.5
x = np.clip(x, 0, 1)
# convert to RGB array
x *= 255
if K.image_data_format() == 'channels_first':
x = x.transpose((1, 2, 0))
x = np.clip(x, 0, 255).astype('uint8')
return x
示例6: smoothing
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def smoothing(im, mode = None):
# utility function to smooth an image
if mode is None:
return im
elif mode == 'L2':
# L2 norm
return im / (np.sqrt(np.mean(np.square(im))) + K.epsilon())
elif mode == 'GaussianBlur':
# Gaussian Blurring with width of 3
return filters.gaussian_filter(im,1/8)
elif mode == 'Decay':
# Decay regularization
decay = 0.98
return decay * im
elif mode == 'Clip_weak':
# Clip weak pixel regularization
percentile = 1
threshold = np.percentile(np.abs(im),percentile)
im[np.where(np.abs(im) < threshold)] = 0
return im
else:
# print error message
print('Unknown smoothing parameter. No smoothing implemented.')
return im
示例7: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0., amsgrad=False, accum_iters=1, **kwargs):
if accum_iters < 1:
raise ValueError('accum_iters must be >= 1')
super(AdamAccumulate, self).__init__(**kwargs)
with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
self.lr = K.variable(lr, name='lr')
self.beta_1 = K.variable(beta_1, name='beta_1')
self.beta_2 = K.variable(beta_2, name='beta_2')
self.decay = K.variable(decay, name='decay')
if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.amsgrad = amsgrad
self.accum_iters = K.variable(accum_iters, K.dtype(self.iterations))
self.accum_iters_float = K.cast(self.accum_iters, K.floatx())
示例8: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def call(self, x, mask=None):
eij = dot_product(x, self.W)
if self.bias:
eij += self.b
eij = K.tanh(eij)
a = K.exp(eij)
if mask is not None:
a *= K.cast(mask, K.floatx())
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
weighted_input = x * K.expand_dims(a)
result = K.sum(weighted_input, axis=1)
if self.return_attention:
return [result, a]
return result
示例9: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
t = K.cast(self.iterations, K.floatx()) + 1
lr_t = self.learning_rate * (K.sqrt(1. - K.pow(self.beta_2, t)) / (1. - K.pow(self.beta_1, t)))
ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
self.weights = [self.iterations] + ms + vs
for p, g, m, v in zip(params, grads, ms, vs):
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
p_t = lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
self.updates.append(K.update_sub(p, p_t))
return self.updates
示例10: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def call(self, x, mask=None):
# size of x :[batch_size, sel_len, attention_dim]
# size of u :[batch_size, attention_dim]
# uit = tanh(xW+b)
uit = K.tanh(K.bias_add(K.dot(x, self.W), self.b))
ait = K.dot(uit, self.u)
ait = K.squeeze(ait, -1)
ait = K.exp(ait)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
ait *= K.cast(mask, K.floatx())
ait /= K.cast(K.sum(ait, axis=1, keepdims=True) + K.epsilon(), K.floatx())
ait = K.expand_dims(ait)
weighted_input = x * ait
output = K.sum(weighted_input, axis=1)
return output
示例11: crossentropy_reed_wrap
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def crossentropy_reed_wrap(_beta):
def crossentropy_reed_core(y_true, y_pred):
"""
This loss function is proposed in:
Reed et al. "Training Deep Neural Networks on Noisy Labels with Bootstrapping", 2014
:param y_true:
:param y_pred:
:return:
"""
# hyper param
print(_beta)
y_pred = K.clip(y_pred, K.epsilon(), 1)
# (1) dynamically update the targets based on the current state of the model: bootstrapped target tensor
# use predicted class proba directly to generate regression targets
y_true_update = _beta * y_true + (1 - _beta) * y_pred
# (2) compute loss as always
_loss = -K.sum(y_true_update * K.log(y_pred), axis=-1)
return _loss
return crossentropy_reed_core
示例12: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def __init__(self, action_space, batch_size=32, screen=(84, 84), swap_freq=200):
from keras.optimizers import RMSprop
# -----
self.screen = screen
self.input_depth = 1
self.past_range = 3
self.observation_shape = (self.input_depth * self.past_range,) + self.screen
self.batch_size = batch_size
_, _, self.train_net, adventage = build_network(self.observation_shape, action_space.n)
self.train_net.compile(optimizer=RMSprop(epsilon=0.1, rho=0.99),
loss=[value_loss(), policy_loss(adventage, args.beta)])
self.pol_loss = deque(maxlen=25)
self.val_loss = deque(maxlen=25)
self.values = deque(maxlen=25)
self.entropy = deque(maxlen=25)
self.swap_freq = swap_freq
self.swap_counter = self.swap_freq
self.unroll = np.arange(self.batch_size)
self.targets = np.zeros((self.batch_size, action_space.n))
self.counter = 0
示例13: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def __init__(self, lr=0.001, final_lr=0.1, beta_1=0.9, beta_2=0.999, gamma=1e-3,
epsilon=None, decay=0., amsbound=False, weight_decay=0.0, **kwargs):
super(AdaBound, self).__init__(**kwargs)
if not 0. <= gamma <= 1.:
raise ValueError("Invalid `gamma` parameter. Must lie in [0, 1] range.")
with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
self.lr = K.variable(lr, name='lr')
self.beta_1 = K.variable(beta_1, name='beta_1')
self.beta_2 = K.variable(beta_2, name='beta_2')
self.decay = K.variable(decay, name='decay')
self.final_lr = final_lr
self.gamma = gamma
if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.amsbound = amsbound
self.weight_decay = float(weight_decay)
self.base_lr = float(lr)
示例14: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def call(self, inputs, mask=None, **kwargs):
if isinstance(inputs, list):
query, key, value = inputs
else:
query = key = value = inputs
if isinstance(mask, list):
mask = mask[1]
feature_dim = K.shape(query)[-1]
e = K.batch_dot(query, key, axes=2) / K.sqrt(K.cast(feature_dim, dtype=K.floatx()))
e = K.exp(e - K.max(e, axis=-1, keepdims=True))
if self.history_only:
query_len, key_len = K.shape(query)[1], K.shape(key)[1]
indices = K.tile(K.expand_dims(K.arange(key_len), axis=0), [query_len, 1])
upper = K.expand_dims(K.arange(key_len), axis=-1)
e *= K.expand_dims(K.cast(indices <= upper, K.floatx()), axis=0)
if mask is not None:
e *= K.cast(K.expand_dims(mask, axis=-2), K.floatx())
a = e / (K.sum(e, axis=-1, keepdims=True) + K.epsilon())
v = K.batch_dot(a, value)
if self.return_attention:
return [v, a]
return v
示例15: bootstrapped_crossentropy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import epsilon [as 别名]
def bootstrapped_crossentropy(y_true, y_pred, bootstrap_type='hard', alpha=0.95):
target_tensor = y_true
prediction_tensor = y_pred
_epsilon = _to_tensor(K.epsilon(), prediction_tensor.dtype.base_dtype)
prediction_tensor = K.tf.clip_by_value(prediction_tensor, _epsilon, 1 - _epsilon)
prediction_tensor = K.tf.log(prediction_tensor / (1 - prediction_tensor))
if bootstrap_type == 'soft':
bootstrap_target_tensor = alpha * target_tensor + (1.0 - alpha) * K.tf.sigmoid(prediction_tensor)
else:
bootstrap_target_tensor = alpha * target_tensor + (1.0 - alpha) * K.tf.cast(
K.tf.sigmoid(prediction_tensor) > 0.5, K.tf.float32)
return K.mean(K.tf.nn.sigmoid_cross_entropy_with_logits(
labels=bootstrap_target_tensor, logits=prediction_tensor))