本文整理匯總了Python中tensorflow.keras.backend.epsilon方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.epsilon方法的具體用法?Python backend.epsilon怎麽用?Python backend.epsilon使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.epsilon方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: main
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def main():
model = create_model(trainable=TRAINABLE)
model.summary()
if TRAINABLE:
model.load_weights(WEIGHTS)
train_datagen = DataGenerator(TRAIN_CSV)
validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV))
optimizer = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(loss=loss, optimizer=optimizer, metrics=[])
checkpoint = ModelCheckpoint("model-{val_dice:.2f}.h5", monitor="val_dice", verbose=1, save_best_only=True,
save_weights_only=True, mode="max")
stop = EarlyStopping(monitor="val_dice", patience=PATIENCE, mode="max")
reduce_lr = ReduceLROnPlateau(monitor="val_dice", factor=0.2, patience=5, min_lr=1e-6, verbose=1, mode="max")
model.fit_generator(generator=train_datagen,
epochs=EPOCHS,
callbacks=[validation_datagen, checkpoint, reduce_lr, stop],
workers=THREADS,
use_multiprocessing=MULTI_PROCESSING,
shuffle=True,
verbose=1)
示例2: focal_loss
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def focal_loss(alpha=0.9, gamma=2):
def focal_loss_with_logits(logits, targets, alpha, gamma, y_pred):
weight_a = alpha * (1 - y_pred) ** gamma * targets
weight_b = (1 - alpha) * y_pred ** gamma * (1 - targets)
return (tf.math.log1p(tf.exp(-tf.abs(logits))) + tf.nn.relu(-logits)) * (weight_a + weight_b) + logits * weight_b
def loss(y_true, y_pred):
y_pred = tf.clip_by_value(y_pred, tf.keras.backend.epsilon(), 1 - tf.keras.backend.epsilon())
logits = tf.math.log(y_pred / (1 - y_pred))
loss = focal_loss_with_logits(logits=logits, targets=y_true, alpha=alpha, gamma=gamma, y_pred=y_pred)
return tf.reduce_mean(loss)
return loss
示例3: main
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def main():
model = create_model()
train_datagen = DataGenerator(TRAIN_CSV)
validation_datagen = Validation(generator=DataGenerator(VALIDATION_CSV))
optimizer = Adam(lr=1e-3, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(loss={"coords" : log_mse, "classes" : focal_loss()}, loss_weights={"coords" : 1, "classes" : 1}, optimizer=optimizer, metrics=[])
checkpoint = ModelCheckpoint("model-{val_iou:.2f}.h5", monitor="val_iou", verbose=1, save_best_only=True,
save_weights_only=True, mode="max")
stop = EarlyStopping(monitor="val_iou", patience=PATIENCE, mode="max")
reduce_lr = ReduceLROnPlateau(monitor="val_iou", factor=0.2, patience=10, min_lr=1e-7, verbose=1, mode="max")
model.summary()
model.fit_generator(generator=train_datagen,
epochs=EPOCHS,
callbacks=[validation_datagen, checkpoint, reduce_lr, stop],
workers=THREADS,
use_multiprocessing=MULTI_PROCESSING,
shuffle=True,
verbose=1)
示例4: focal_loss_binary
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def focal_loss_binary(y_true, y_pred):
"""Binary cross-entropy focal loss
"""
gamma = 2.0
alpha = 0.25
pt_1 = tf.where(tf.equal(y_true, 1),
y_pred,
tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0),
y_pred,
tf.zeros_like(y_pred))
epsilon = K.epsilon()
# clip to prevent NaN and Inf
pt_1 = K.clip(pt_1, epsilon, 1. - epsilon)
pt_0 = K.clip(pt_0, epsilon, 1. - epsilon)
weight = alpha * K.pow(1. - pt_1, gamma)
fl1 = -K.sum(weight * K.log(pt_1))
weight = (1 - alpha) * K.pow(pt_0, gamma)
fl0 = -K.sum(weight * K.log(1. - pt_0))
return fl1 + fl0
示例5: focal_loss_categorical
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def focal_loss_categorical(y_true, y_pred):
"""Categorical cross-entropy focal loss"""
gamma = 2.0
alpha = 0.25
# scale to ensure sum of prob is 1.0
y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
# clip the prediction value to prevent NaN and Inf
epsilon = K.epsilon()
y_pred = K.clip(y_pred, epsilon, 1. - epsilon)
# calculate cross entropy
cross_entropy = -y_true * K.log(y_pred)
# calculate focal loss
weight = alpha * K.pow(1 - y_pred, gamma)
cross_entropy *= weight
return K.sum(cross_entropy, axis=-1)
示例6: mi_loss
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def mi_loss(self, y_true, y_pred):
""" MINE loss function
Arguments:
y_true (tensor): Not used since this is
unsupervised learning
y_pred (tensor): stack of predictions for
joint T(x,y) and marginal T(x,y)
"""
size = self.args.batch_size
# lower half is pred for joint dist
pred_xy = y_pred[0: size, :]
# upper half is pred for marginal dist
pred_x_y = y_pred[size : y_pred.shape[0], :]
loss = K.mean(K.exp(pred_x_y))
loss = K.clip(loss, K.epsilon(), np.finfo(float).max)
loss = K.mean(pred_xy) - K.log(loss)
return -loss
示例7: surv_likelihood
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def surv_likelihood(n_intervals):
"""Create custom Keras loss function for neural network survival model.
Arguments
n_intervals: the number of survival time intervals
Returns
Custom loss function that can be used with Keras
"""
def loss(y_true, y_pred):
"""
Required to have only 2 arguments by Keras.
Arguments
y_true: Tensor.
First half of the values is 1 if individual survived that interval, 0 if not.
Second half of the values is for individuals who failed, and is 1 for time interval during which failure occured, 0 for other intervals.
See make_surv_array function.
y_pred: Tensor, predicted survival probability (1-hazard probability) for each time interval.
Returns
Vector of losses for this minibatch.
"""
cens_uncens = 1. + y_true[:,0:n_intervals] * (y_pred-1.) #component for all individuals
uncens = 1. - y_true[:,n_intervals:2*n_intervals] * y_pred #component for only uncensored individuals
return K.sum(-K.log(K.clip(K.concatenate((cens_uncens,uncens)),K.epsilon(),None)),axis=-1) #return -log likelihood
return loss
示例8: mcor
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def mcor(y_true, y_pred):
# matthews_correlation
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
y_neg = 1 - y_pos
tp = K.sum(y_pos * y_pred_pos)
tn = K.sum(y_neg * y_pred_neg)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
numerator = (tp * tn - fp * fn)
denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
return numerator / (denominator + K.epsilon())
示例9: mcor
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def mcor(y_true, y_pred):
# Matthews correlation
y_pred_pos = K.round(K.clip(y_pred, 0, 1))
y_pred_neg = 1 - y_pred_pos
y_pos = K.round(K.clip(y_true, 0, 1))
y_neg = 1 - y_pos
tp = K.sum(y_pos * y_pred_pos)
tn = K.sum(y_neg * y_pred_neg)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)
numerator = (tp * tn - fp * fn)
denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
return numerator / (denominator + K.epsilon())
示例10: recall
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def recall(y_true, y_pred):
"""Precision for foreground pixels.
Calculates pixelwise recall TP/(TP + FN).
"""
# count true positives
truth = K.round(K.clip(y_true, K.epsilon(), 1))
pred_pos = K.round(K.clip(y_pred, K.epsilon(), 1))
true_pos = K.sum(K.cast(K.all(K.stack([truth, pred_pos], axis=2), axis=2),
dtype='float64'))
truth_ct = K.sum(K.round(K.clip(y_true, K.epsilon(), 1)))
if truth_ct == 0:
return 0
recall = true_pos/truth_ct
return recall
示例11: result
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def result(self):
recall = tf.math.divide_no_nan(self.tp, self.tp + self.fn)
precision = tf.math.divide_no_nan(self.tp, self.tp + self.fp)
f1 = tf.math.divide_no_nan(2 * precision * recall, precision + recall + K.epsilon())
return f1
示例12: _cosine_dist
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def _cosine_dist(x, y):
"""Computes the inner product (cosine distance) between two tensors.
Parameters
----------
x: tf.Tensor
Input Tensor
y: tf.Tensor
Input Tensor
"""
denom = (backend.sqrt(backend.sum(tf.square(x)) * backend.sum(tf.square(y))) +
backend.epsilon())
return backend.dot(x, tf.transpose(y)) / denom
示例13: __init__
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def __init__(self,
batch_size,
n_input=128,
gaussian_expand=False,
init='glorot_uniform',
activation='tanh',
epsilon=1e-3,
momentum=0.99,
**kwargs):
"""
Parameters
----------
batch_size: int
number of molecules in a batch
n_input: int, optional
number of features for each input molecule
gaussian_expand: boolean. optional
Whether to expand each dimension of atomic features by gaussian histogram
init: str, optional
Weight initialization for filters.
activation: str, optional
Activation function applied
"""
super(WeaveGather, self).__init__(**kwargs)
self.n_input = n_input
self.batch_size = batch_size
self.gaussian_expand = gaussian_expand
self.init = init # Set weight initialization
self.activation = activation # Get activations
self.activation_fn = activations.get(activation)
self.epsilon = epsilon
self.momentum = momentum
示例14: get_config
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def get_config(self):
config = super(WeaveGather, self).get_config()
config['batch_size'] = self.batch_size
config['n_input'] = self.n_input
config['gaussian_expand'] = self.gaussian_expand
config['init'] = self.init
config['activation'] = self.activation
config['epsilon'] = self.epsilon
config['momentum'] = self.momentum
return config
示例15: normalize_A
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import epsilon [as 別名]
def normalize_A(A):
"""
Computes symmetric normalization of A, dealing with sparse A and batch mode
automatically.
:param A: Tensor or SparseTensor with rank k = {2, 3}.
:return: Tensor or SparseTensor of rank k.
"""
D = degrees(A)
D = tf.sqrt(D)[:, None] + K.epsilon()
perm = (0, 2, 1) if K.ndim(A) == 3 else (1, 0)
output = (A / D) / ops.transpose(D, perm=perm)
return output