本文整理汇总了Python中keras.backend.update方法的典型用法代码示例。如果您正苦于以下问题:Python backend.update方法的具体用法?Python backend.update怎么用?Python backend.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [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
示例2: merge_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def merge_updates(updates):
"""Average repeated updates of the same variable"""
merged_updates = {}
for update in updates:
variable, value = unpack_assignment(update)
key = variable_key(variable)
if key not in merged_updates:
merged_updates[key] = [variable, []]
merged_updates[key][1].append(value)
ret = []
for k, v in iteritems(merged_updates):
variable = v[0]
values = v[1]
n = len(values)
if n == 1:
ret.append(K.update(variable, value[0]))
else:
ret.append(K.update(variable, sum(values) / n))
return ret
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [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)
示例4: apply_box_deltas_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def apply_box_deltas_graph(boxes, deltas):
"""Applies the given deltas to the given boxes.
boxes: [N, (y1, x1, y2, x2)] boxes to update
deltas: [N, (dy, dx, log(dh), log(dw))] refinements to apply
"""
# Convert to y, x, h, w
height = boxes[:, 2] - boxes[:, 0]
width = boxes[:, 3] - boxes[:, 1]
center_y = boxes[:, 0] + 0.5 * height
center_x = boxes[:, 1] + 0.5 * width
# Apply deltas
center_y += deltas[:, 0] * height
center_x += deltas[:, 1] * width
height *= tf.exp(deltas[:, 2])
width *= tf.exp(deltas[:, 3])
# Convert back to y1, x1, y2, x2
y1 = center_y - 0.5 * height
x1 = center_x - 0.5 * width
y2 = y1 + height
x2 = x1 + width
result = tf.stack([y1, x1, y2, x2], axis=1, name="apply_box_deltas_out")
return result
开发者ID:wwoody827,项目名称:cvpr-2018-autonomous-driving-autopilot-solution,代码行数:24,代码来源:model_inceptionresnet.py
示例5: add_weightnorm_param_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def add_weightnorm_param_updates(updates, new_V_param, new_g_param, W, V_scaler):
ps = K.get_variable_shape(new_V_param)
norm_axes = [i for i in range(len(ps) - 1)]
# update W and V_scaler
new_V_norm = tf.sqrt(tf.reduce_sum(tf.square(new_V_param), norm_axes))
new_V_scaler = new_g_param / new_V_norm
new_W = tf.reshape(new_V_scaler, [1] * len(norm_axes) + [-1]) * new_V_param
updates.append(K.update(W, new_W))
updates.append(K.update(V_scaler, new_V_scaler))
# data based initialization for a given Keras model
示例6: data_based_init
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def data_based_init(model, input):
# input can be dict, numpy array, or list of numpy arrays
if type(input) is dict:
feed_dict = input
elif type(input) is list:
feed_dict = {tf_inp: np_inp for tf_inp,np_inp in zip(model.inputs,input)}
else:
feed_dict = {model.inputs[0]: input}
# add learning phase if required
if model.uses_learning_phase and K.learning_phase() not in feed_dict:
feed_dict.update({K.learning_phase(): 1})
# get all layer name, output, weight, bias tuples
layer_output_weight_bias = []
for l in model.layers:
if hasattr(l, 'W') and hasattr(l, 'b'):
assert(l.built)
layer_output_weight_bias.append( (l.name,l.get_output_at(0),l.W,l.b) ) # if more than one node, only use the first
# iterate over our list and do data dependent init
sess = K.get_session()
for l,o,W,b in layer_output_weight_bias:
print('Performing data dependent initialization for layer ' + l)
m,v = tf.nn.moments(o, [i for i in range(len(o.get_shape())-1)])
s = tf.sqrt(v + 1e-10)
updates = tf.group(W.assign(W/tf.reshape(s,[1]*(len(W.get_shape())-1)+[-1])), b.assign((b-m)/s))
sess.run(updates, feed_dict)
示例7: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr *= (1. / (1. + self.decay * K.cast(self.iterations, K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx()) + 1
lr_t = lr * (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]
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
self.weights = [self.iterations] + ms + vs + vhats
for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square(g)
vhat_t = K.maximum(vhat, v_t)
p_t = p - lr_t * m_t / (K.sqrt(vhat_t) + self.epsilon)
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
self.updates.append(K.update(vhat, vhat_t))
new_p = p_t
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
示例8: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr * (1. / (1. + self.decay * K.cast(self.iterations,
K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx()) + 1
beta_1_t = K.pow(self.beta_1, t)
beta_2_t = K.pow(self.beta_2, t)
rho = 2 / (1 - self.beta_2) - 1
rho_t = rho - 2 * t * beta_2_t / (1 - beta_2_t)
r_t = K.sqrt(
K.relu(rho_t - 4) * K.relu(rho_t - 2) * rho / ((rho - 4) * (rho - 2) * rho_t)
)
flag = K.cast(rho_t > 4, K.floatx())
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)
mhat_t = m_t / (1 - beta_1_t)
vhat_t = K.sqrt(v_t / (1 - beta_2_t))
p_t = p - lr * mhat_t * (flag * r_t / (vhat_t + self.epsilon) + (1 - flag))
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
示例9: __call__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def __call__(self, y_true, y_pred): # y_true, y_pred shape: (BS, row, col, ch * 2)
data_true, generator_true = y_true[:, :, :, 0:3], y_true[:, :, :, 3:6]
data_pred, generator_pred = y_pred[:, :, :, 0:3], y_pred[:, :, :, 3:6]
loss_data = K.mean(K.abs(data_true - data_pred), axis=[1, 2, 3])
loss_generator = K.mean(K.abs(generator_true - generator_pred), axis=[1, 2, 3])
ret = loss_data - self.k_var * loss_generator
# for updating values in each epoch, use `updates` mechanism
# DiscriminatorModel collects Loss Function's updates attributes
mean_loss_data = K.mean(loss_data)
mean_loss_gen = K.mean(loss_generator)
# update K
new_k = self.k_var + self.lambda_k * (self.gamma * mean_loss_data - mean_loss_gen)
new_k = K.clip(new_k, 0, 1)
self.updates.append(K.update(self.k_var, new_k))
# calculate M-Global
m_global = mean_loss_data + K.abs(self.gamma * mean_loss_data - mean_loss_gen)
self.updates.append(K.update(self.m_global_var, m_global))
# let loss_real_x mean_loss_data
self.updates.append(K.update(self.loss_real_x_var, mean_loss_data))
# let loss_gen_x mean_loss_gen
self.updates.append(K.update(self.loss_gen_x_var, mean_loss_gen))
return ret
示例10: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
weights = self.get_weights()
self.updates = [K.update_add(self.iterations, 1)]
scaled_lr = self.lr
w_norm = K.sqrt(K.sum([K.sum(K.square(weight))
for weight in weights]))
g_norm = K.sqrt(K.sum([K.sum(K.square(grad))
for grad in grads]))
scaled_lr = K.switch(K.greater(w_norm * g_norm, K.zeros([1])),
K.expand_dims((self.eeta * w_norm /
(g_norm + self.weight_decay * w_norm +
self.epsilon)) * self.lr),
K.ones([1]) * self.lr)
if K.backend() == 'theano':
scaled_lr = scaled_lr[0] # otherwise theano raise broadcasting error
# momentum
moments = [K.zeros(K.int_shape(param), dtype=K.dtype(param))
for param in params]
self.weights = [self.iterations] + moments
for param, grad, moment in zip(params, grads, moments):
v0 = (moment * self.momentum)
v1 = scaled_lr * grad # velocity
veloc = v0 - v1
self.updates.append(K.update(moment, veloc))
if self.nesterov:
new_param = param + (veloc * self.momentum) - v1
else:
new_param = param + veloc
# Apply constraints.
if getattr(param, 'constraint', None) is not None:
new_param = param.constraint(new_param)
self.updates.append(K.update(param, new_param))
return self.updates
示例11: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr * (1. / (1. + self.decay * K.cast(self.iterations,
K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx()) + 1
lr_t = lr * (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]
vhats = [K.zeros(1) for _ in params]
self.weights = [self.iterations] + ms + vs + vhats
for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
g2 = K.square(g)
m_t = (self.beta_1 * m) + (1. - self.beta_1) * g
v_t = v - (1. - self.beta_2) * K.sign(v - g2) * g2
p_t = p - 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))
new_p = p_t
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
示例12: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def __init__(self, lr=0.0025, beta_1=0.6, beta_2=0.999,
epsilon=1e-8, decay=0., **kwargs):
super(FTML, self).__init__(**kwargs)
self.__dict__.update(locals())
self.iterations = K.variable(0)
self.lr = K.variable(lr)
self.beta_1 = K.variable(beta_1)
self.beta_2 = K.variable(beta_2)
self.decay = K.variable(decay)
self.epsilon = epsilon
self.inital_decay = decay
示例13: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.inital_decay > 0:
lr *= (1. / (1. + self.decay * self.iterations))
t = self.iterations + 1
lr_t = lr / (1. - K.pow(self.beta_1, t))
shapes = [K.int_shape(p) for p in params]
zs = [K.zeros(shape) for shape in shapes]
vs = [K.zeros(shape) for shape in shapes]
ds = [K.zeros(shape) for shape in shapes]
self.weights = [self.iterations] + zs + vs + ds
for p, g, z, v, d in zip(params, grads, zs, vs, ds):
v_t = self.beta_2 * v + (1. - self.beta_2) * K.square(g)
d_t = (K.sqrt(v_t / (1. - K.pow(self.beta_2, t)))
+ self.epsilon) / lr_t
sigma_t = d_t - self.beta_1 * d
z_t = self.beta_1 * z + (1. - self.beta_1) * g - sigma_t * p
p_t = - z_t / d_t
self.updates.append(K.update(z, z_t))
self.updates.append(K.update(v, v_t))
self.updates.append(K.update(d, d_t))
new_p = p_t
# Apply constraints.
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates
示例14: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, accum_iters=10, **kwargs):
super(AdamAccumulate, self).__init__(**kwargs)
self.__dict__.update(locals())
self.iterations = K.variable(0)
self.lr = K.variable(lr)
self.beta_1 = K.variable(beta_1)
self.beta_2 = K.variable(beta_2)
if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.accum_iters = K.variable(accum_iters)
示例15: get_updates
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import update [as 别名]
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
self.updates = [(self.iterations, self.iterations + 1)]
t = self.iterations + 1
lr_t = self.lr * 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]
gs = [K.zeros(K.int_shape(p), dtype=K.dtype(p)) for p in params]
self.weights = ms + vs
flag = K.equal(t % self.accum_iters, 0)
flag = K.cast(flag, dtype='float32')
for p, g, m, v, gg in zip(params, grads, ms, vs, gs):
gg_t = (1 - flag) * (gg + g)
m_t = (self.beta_1 * m) + (1. - self.beta_1) * (gg + flag * g) / self.accum_iters
v_t = (self.beta_2 * v) + (1. - self.beta_2) * K.square((gg + flag * g) / self.accum_iters)
p_t = p - flag * lr_t * m_t / (K.sqrt(v_t) + self.epsilon)
self.updates.append((m, flag * m_t + (1 - flag) * m))
self.updates.append((v, flag * v_t + (1 - flag) * v))
self.updates.append((gg, gg_t))
# apply constraints.
new_p = p_t
if getattr(p, 'constraint', None) is not None:
new_p = p.constraint(new_p)
self.updates.append(K.update(p, new_p))
return self.updates