本文整理汇总了Python中keras.backend.zeros_like方法的典型用法代码示例。如果您正苦于以下问题:Python backend.zeros_like方法的具体用法?Python backend.zeros_like怎么用?Python backend.zeros_like使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.zeros_like方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: audio_discriminate_loss2
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [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
示例2: add_boundary_energy
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def add_boundary_energy(x, b_start=None, b_end=None, mask=None):
'''Given the observations x, it adds the start boundary energy b_start (resp.
end boundary energy b_end on the start (resp. end) elements and multiplies
the mask.'''
if mask is None:
if b_start is not None:
x = K.concatenate([x[:, :1, :] + b_start, x[:, 1:, :]], axis=1)
if b_end is not None:
x = K.concatenate([x[:, :-1, :], x[:, -1:, :] + b_end], axis=1)
else:
mask = K.cast(mask, K.floatx())
mask = K.expand_dims(mask, 2)
x *= mask
if b_start is not None:
mask_r = K.concatenate([K.zeros_like(mask[:, :1]), mask[:, :-1]], axis=1)
start_mask = K.cast(K.greater(mask, mask_r), K.floatx())
x = x + start_mask * b_start
if b_end is not None:
mask_l = K.concatenate([mask[:, 1:], K.zeros_like(mask[:, -1:])], axis=1)
end_mask = K.cast(K.greater(mask, mask_l), K.floatx())
x = x + end_mask * b_end
return x
示例3: _forward
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def _forward(x, reduce_step, initial_states, U, mask=None):
'''Forward recurrence of the linear chain crf.'''
def _forward_step(energy_matrix_t, states):
alpha_tm1 = states[-1]
new_states = reduce_step(K.expand_dims(alpha_tm1, 2) + energy_matrix_t)
return new_states[0], new_states
U_shared = K.expand_dims(K.expand_dims(U, 0), 0)
if mask is not None:
mask = K.cast(mask, K.floatx())
mask_U = K.expand_dims(K.expand_dims(mask[:, :-1] * mask[:, 1:], 2), 3)
U_shared = U_shared * mask_U
inputs = K.expand_dims(x[:, 1:, :], 2) + U_shared
inputs = K.concatenate([inputs, K.zeros_like(inputs[:, -1:, :, :])], axis=1)
last, values, _ = K.rnn(_forward_step, inputs, initial_states)
return last, values
示例4: _backward
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def _backward(gamma, mask):
'''Backward recurrence of the linear chain crf.'''
gamma = K.cast(gamma, 'int32')
def _backward_step(gamma_t, states):
y_tm1 = K.squeeze(states[0], 0)
y_t = batch_gather(gamma_t, y_tm1)
return y_t, [K.expand_dims(y_t, 0)]
initial_states = [K.expand_dims(K.zeros_like(gamma[:, 0, 0]), 0)]
_, y_rev, _ = K.rnn(_backward_step,
gamma,
initial_states,
go_backwards=True)
y = K.reverse(y_rev, 1)
if mask is not None:
mask = K.cast(mask, dtype='int32')
# mask output
y *= mask
# set masked values to -1
y += -(1 - mask)
return y
示例5: __init__
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def __init__(self,
resource_path='./resources/',
learning_rate=0.0002,
decay_rate=2e-6,
gpus = 1):
self.gpus = gpus
self.learning_rate = learning_rate
self.decay_rate = decay_rate
def zero_loss(y_true, y_pred):
return K.zeros_like(y_true)
discriminator_full = load_model(resource_path + 'discriminator_full.h5', custom_objects={'Conv2D_r': Conv2D_r, 'InstanceNormalization': InstanceNormalization, 'tf': tf, 'zero_loss': zero_loss, 'ConvSN2D': ConvSN2D, 'DenseSN': DenseSN})
discriminator_full.trainable = True
discriminator_full.name = "discriminator_full"
self.model = discriminator_full
self.save_model = discriminator_full
示例6: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def call(self, u_vecs):
if self.share_weights:
u_hat_vecs = K.conv1d(u_vecs, self.W)
else:
u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])
batch_size = K.shape(u_vecs)[0]
input_num_capsule = K.shape(u_vecs)[1]
u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule,
self.num_capsule, self.dim_capsule))
u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3))
b = K.zeros_like(u_hat_vecs[:, :, :, 0]) # shape = [None, num_capsule, input_num_capsule]
for i in range(self.routings):
b = K.permute_dimensions(b, (0, 2, 1)) # shape = [None, input_num_capsule, num_capsule]
c = K.softmax(b)
c = K.permute_dimensions(c, (0, 2, 1))
b = K.permute_dimensions(b, (0, 2, 1))
outputs = self.activation(K.batch_dot(c, u_hat_vecs, [2, 2]))
if i < self.routings - 1:
b = K.batch_dot(outputs, u_hat_vecs, [2, 3])
return outputs
示例7: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def call(self, u_vecs):
if self.share_weights:
u_hat_vecs = K.conv1d(u_vecs, self.W)
else:
u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])
batch_size = K.shape(u_vecs)[0]
input_num_capsule = K.shape(u_vecs)[1]
u_hat_vecs = K.reshape(u_hat_vecs, (batch_size, input_num_capsule,
self.num_capsule, self.dim_capsule))
u_hat_vecs = K.permute_dimensions(u_hat_vecs, (0, 2, 1, 3))
# final u_hat_vecs.shape = [None, num_capsule, input_num_capsule, dim_capsule]
b = K.zeros_like(u_hat_vecs[:, :, :, 0]) # shape = [None, num_capsule, input_num_capsule]
outputs = None
for i in range(self.routings):
b = K.permute_dimensions(b, (0, 2, 1)) # shape = [None, input_num_capsule, num_capsule]
c = K.softmax(b)
c = K.permute_dimensions(c, (0, 2, 1))
b = K.permute_dimensions(b, (0, 2, 1))
outputs = self.activation(K.batch_dot(c, u_hat_vecs, [2, 2]))
if i < self.routings - 1:
b = K.batch_dot(outputs, u_hat_vecs, [2, 3])
return outputs
示例8: f1_score_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def f1_score_keras(y_true, y_pred):
#convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1)
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
#precision for each class
precision = K.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return K.mean(f1_class)
示例9: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def f1_score_taskB(y_true, y_pred):
#convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
#precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.switch(K.equal(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return f1_class
示例10: f1_score_semeval
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def f1_score_semeval(y_true, y_pred):
# convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1)
# where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true * y_pred_ones, axis=0)
# for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
# for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
# precision for each class
precision = K.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred / gold_cnt)
# f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall))
#return average f1 score over all classes
return (f1_class[0] + f1_class[2])/2.0
示例11: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def precision_keras(y_true, y_pred):
#convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#return average f1 score over all classes
return K.mean(precision)
示例12: f1_score_task3
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def f1_score_task3(y_true, y_pred):
#convert probas to 0,1
y_ppred = K.zeros_like(y_true)
y_pred_ones = K.T.set_subtensor(y_ppred[K.T.arange(y_true.shape[0]), K.argmax(y_pred, axis=-1)], 1)
#where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true*y_pred_ones, axis=0)
#for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
#for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
#precision for each class
precision = K.T.switch(K.T.eq(pred_cnt, 0), 0, y_true_pred/pred_cnt)
#recall for each class
recall = K.T.switch(K.T.eq(gold_cnt, 0), 0, y_true_pred/gold_cnt)
#f1 for each class
f1_class = K.T.switch(K.T.eq(precision + recall, 0), 0, 2*(precision*recall)/(precision+recall))
#return average f1 score over all classes
return f1_class[1]
示例13: f1_score_taskB
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def f1_score_taskB(y_true, y_pred):
# convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
# where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true * y_pred_ones, axis=0)
# for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
# for each class: how many are true members of said class
gold_cnt = K.sum(y_true, axis=0)
# precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# recall for each class
recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred / gold_cnt)
# f1 for each class
f1_class = K.switch(K.equal(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall))
# return average f1 score over all classes
return f1_class
示例14: precision_keras
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def precision_keras(y_true, y_pred):
# convert probas to 0,1
y_pred_ones = K.zeros_like(y_true)
y_pred_ones[:, K.argmax(y_pred, axis=-1)] = 1
# where y_ture=1 and y_pred=1 -> true positive
y_true_pred = K.sum(y_true * y_pred_ones, axis=0)
# for each class: how many where classified as said class
pred_cnt = K.sum(y_pred_ones, axis=0)
# precision for each class
precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt)
# return average f1 score over all classes
return K.mean(precision)
示例15: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import zeros_like [as 别名]
def call(self, X, mask=None):
if mask is not None:
assert K.ndim(mask) == 2, 'Input mask to CRF must have dim 2 if not None'
if self.test_mode == 'viterbi':
test_output = self.viterbi_decoding(X, mask)
else:
test_output = self.get_marginal_prob(X, mask)
self.uses_learning_phase = True
if self.learn_mode == 'join':
train_output = K.zeros_like(K.dot(X, self.kernel))
out = K.in_train_phase(train_output, test_output)
else:
if self.test_mode == 'viterbi':
train_output = self.get_marginal_prob(X, mask)
out = K.in_train_phase(train_output, test_output)
else:
out = test_output
return out