本文整理汇总了Python中keras.backend.mean方法的典型用法代码示例。如果您正苦于以下问题:Python backend.mean方法的具体用法?Python backend.mean怎么用?Python backend.mean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.mean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gradient_penalty_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def gradient_penalty_loss(self, y_true, y_pred, averaged_samples):
"""
Computes gradient penalty based on prediction and weighted real / fake samples
"""
gradients = K.gradients(y_pred, averaged_samples)[0]
# compute the euclidean norm by squaring ...
gradients_sqr = K.square(gradients)
# ... summing over the rows ...
gradients_sqr_sum = K.sum(gradients_sqr,
axis=np.arange(1, len(gradients_sqr.shape)))
# ... and sqrt
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
# compute lambda * (1 - ||grad||)^2 still for each single sample
gradient_penalty = K.square(1 - gradient_l2_norm)
# return the mean as loss over all the batch samples
return K.mean(gradient_penalty)
示例2: generate_pattern
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def generate_pattern(layer_name, filter_index, size=150):
# 过滤器可视化函数
layer_output = model.get_layer(layer_name).output
loss = K.mean(layer_output[:, :, :, filter_index])
grads = K.gradients(loss, model.input)[0]
grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)
iterate = K.function([model.input], [loss, grads])
input_img_data = np.random.random((1, size, size, 3)) * 20 + 128.
step = 1
for _ in range(40):
loss_value, grads_value = iterate([input_img_data])
input_img_data += grads_value * step
img = input_img_data[0]
return deprocess_image(img)
示例3: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def call(self, x):
mean = K.mean(x, axis=-1)
std = K.std(x, axis=-1)
if len(x.shape) == 3:
mean = K.permute_dimensions(
K.repeat(mean, x.shape.as_list()[-1]),
[0,2,1]
)
std = K.permute_dimensions(
K.repeat(std, x.shape.as_list()[-1]),
[0,2,1]
)
elif len(x.shape) == 2:
mean = K.reshape(
K.repeat_elements(mean, x.shape.as_list()[-1], 0),
(-1, x.shape.as_list()[-1])
)
std = K.reshape(
K.repeat_elements(mean, x.shape.as_list()[-1], 0),
(-1, x.shape.as_list()[-1])
)
return self._g * (x - mean) / (std + self._epsilon) + self._b
示例4: test_helper
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def test_helper(func, exponent, layer, lr, dropout_first, dropout_middle,
dropout_last, alpha, prefix='SWO GBP ', postfix='',
with_comparison=False):
print('Test %s, %s, %s, %s, %s %s %s' % (exponent, layer, lr, dropout_first,
dropout_middle, dropout_last, alpha))
model = func(exponent=exponent, lr=lr, layers=layer,
dropout_first=dropout_first, dropout_middle=dropout_middle,
dropout_last=dropout_last, prefix=prefix, postfix=postfix,
alpha=alpha)
model.train(200)
val_loss = np.mean(model.history['history']['val_loss'][-5:])
# if with_comparison:
# swo = inst.get_swaptiongen(inst.hullwhite_analytic)
# _, values = swo.compare_history(model, dates=dates)
#
return (val_loss, layer, exponent, lr, dropout_first, dropout_middle,
dropout_last, alpha)
示例5: audio_discriminate_loss2
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def audio_discriminate_loss2(gamma=0.1,beta = 2*0.1,num_speaker=2):
def loss_func(S_true,S_pred,gamma=gamma,beta=beta,num_speaker=num_speaker):
sum_mtr = K.zeros_like(S_true[:,:,:,:,0])
for i in range(num_speaker):
sum_mtr += K.square(S_true[:,:,:,:,i]-S_pred[:,:,:,:,i])
for j in range(num_speaker):
if i != j:
sum_mtr -= gamma*(K.square(S_true[:,:,:,:,i]-S_pred[:,:,:,:,j]))
for i in range(num_speaker):
for j in range(i+1,num_speaker):
#sum_mtr -= beta*K.square(S_pred[:,:,:,i]-S_pred[:,:,:,j])
#sum_mtr += beta*K.square(S_true[:,:,:,:,i]-S_true[:,:,:,:,j])
pass
#sum = K.sum(K.maximum(K.flatten(sum_mtr),0))
loss = K.mean(K.flatten(sum_mtr))
return loss
return loss_func
示例6: prepareSamples
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def prepareSamples(self, cnum = 0, num = 1000): #8x8 images, bottom row is constant
try:
os.mkdir("Results/Samples-c" + str(cnum))
except:
x = 0
im = self.im.get_class(cnum)
e = self.GAN.E.predict(im, batch_size = BATCH_SIZE * k_images)
mean = np.mean(e, axis = 0)
std = np.std(e, axis = 0)
n = noise(num)
nc = nClass(num, mean, std)
im = self.GAN.G.predict([n, nc], batch_size = BATCH_SIZE)
for i in range(im.shape[0]):
x = Image.fromarray(np.uint8(im[i]*255), mode = 'RGB')
x.save("Results/Samples-c" + str(cnum) + "/im ("+str(i+1)+").png")
示例7: rpn_class_loss_graph
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def rpn_class_loss_graph(rpn_match, rpn_class_logits):
"""RPN anchor classifier loss.
rpn_match: [batch, anchors, 1]. Anchor match type. 1=positive,
-1=negative, 0=neutral anchor.
rpn_class_logits: [batch, anchors, 2]. RPN classifier logits for FG/BG.
"""
# Squeeze last dim to simplify
rpn_match = tf.squeeze(rpn_match, -1)
# Get anchor classes. Convert the -1/+1 match to 0/1 values.
anchor_class = K.cast(K.equal(rpn_match, 1), tf.int32)
# Positive and Negative anchors contribute to the loss,
# but neutral anchors (match value = 0) don't.
indices = tf.where(K.not_equal(rpn_match, 0))
# Pick rows that contribute to the loss and filter out the rest.
rpn_class_logits = tf.gather_nd(rpn_class_logits, indices)
anchor_class = tf.gather_nd(anchor_class, indices)
# Cross entropy loss
loss = K.sparse_categorical_crossentropy(target=anchor_class,
output=rpn_class_logits,
from_logits=True)
loss = K.switch(tf.size(loss) > 0, K.mean(loss), tf.constant(0.0))
return loss
示例8: gen_adv_loss
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def gen_adv_loss(logits, y, loss='logloss', mean=False):
"""
Generate the loss function.
"""
if loss == 'training':
# use the model's output instead of the true labels to avoid
# label leaking at training time
y = K.cast(K.equal(logits, K.max(logits, 1, keepdims=True)), "float32")
y = y / K.sum(y, 1, keepdims=True)
out = K.categorical_crossentropy(y, logits, from_logits=True)
elif loss == 'logloss':
out = K.categorical_crossentropy(y, logits, from_logits=True)
else:
raise ValueError("Unknown loss: {}".format(loss))
if mean:
out = K.mean(out)
# else:
# out = K.sum(out)
return out
示例9: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
prediction = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(prediction * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# 상태가 입력, 큐함수가 출력인 인공신경망 생성
示例10: predict
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def predict(self, x):
r"""
Predict quantiles of the conditional distribution P(y|x).
Forward propagates the inputs in `x` through the network to
obtain the predicted quantiles `y`.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` m-dimensional inputs
for which to predict the conditional quantiles.
Returns:
Array of shape `(n, k)` with the columns corresponding to the k
quantiles of the network.
"""
predictions = np.stack(
[m.predict((x - self.x_mean) / self.x_sigma) for m in self.models])
return np.mean(predictions, axis=0)
示例11: posterior_mean
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def posterior_mean(self, x):
r"""
Computes the posterior mean by computing the first moment of the
estimated posterior CDF.
Arguments:
x(np.array): Array of shape `(n, m)` containing `n` inputs for which
to predict the posterior mean.
Returns:
Array containing the posterior means for the provided inputs.
"""
y_pred, qs = self.cdf(x)
mus = y_pred[-1] - np.trapz(qs, x=y_pred)
return mus
示例12: sampling
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [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
示例13: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例14: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None,), dtype='int32')
y = K.placeholder(shape=(None,), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
示例15: optimizer
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import mean [as 别名]
def optimizer(self):
a = K.placeholder(shape=(None, ), dtype='int32')
y = K.placeholder(shape=(None, ), dtype='float32')
py_x = self.model.output
a_one_hot = K.one_hot(a, self.action_size)
q_value = K.sum(py_x * a_one_hot, axis=1)
error = K.abs(y - q_value)
quadratic_part = K.clip(error, 0.0, 1.0)
linear_part = error - quadratic_part
loss = K.mean(0.5 * K.square(quadratic_part) + linear_part)
optimizer = RMSprop(lr=0.00025, epsilon=0.01)
updates = optimizer.get_updates(self.model.trainable_weights, [], loss)
train = K.function([self.model.input, a, y], [loss], updates=updates)
return train
# approximate Q function using Convolution Neural Network
# state is input and Q Value of each action is output of network
# dueling network's Q Value is sum of advantages and state value