本文整理汇总了Python中tensorflow.keras.backend.abs方法的典型用法代码示例。如果您正苦于以下问题:Python backend.abs方法的具体用法?Python backend.abs怎么用?Python backend.abs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.abs方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: huber_loss
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def huber_loss(y_true, y_pred, clip_value):
# Huber loss, see https://en.wikipedia.org/wiki/Huber_loss and
# https://medium.com/@karpathy/yes-you-should-understand-backprop-e2f06eab496b
# for details.
assert clip_value > 0.
x = y_true - y_pred
if np.isinf(clip_value):
# Spacial case for infinity since Tensorflow does have problems
# if we compare `K.abs(x) < np.inf`.
return .5 * K.square(x)
condition = K.abs(x) < clip_value
squared_loss = .5 * K.square(x)
linear_loss = clip_value * (K.abs(x) - .5 * clip_value)
import tensorflow as tf
if hasattr(tf, 'select'):
return tf.select(condition, squared_loss, linear_loss) # condition, true, false
else:
return tf.where(condition, squared_loss, linear_loss) # condition, true, false
示例2: spike_call
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def spike_call(call):
def decorator(self, x):
updates = []
if hasattr(self, 'kernel'):
store_old_kernel = self._kernel.assign(self.kernel)
store_old_bias = self._bias.assign(self.bias)
updates += [store_old_kernel, store_old_bias]
with tf.control_dependencies(updates):
new_kernel = k.abs(self.kernel)
new_bias = k.zeros_like(self.bias)
assign_new_kernel = self.kernel.assign(new_kernel)
assign_new_bias = self.bias.assign(new_bias)
updates += [assign_new_kernel, assign_new_bias]
with tf.control_dependencies(updates):
c = call(self, x)[self.batch_size:]
cc = k.concatenate([c, c], 0)
updates = [self.missing_impulse.assign(cc)]
with tf.control_dependencies(updates):
updates = [self.kernel.assign(self._kernel),
self.bias.assign(self._bias)]
elif 'AveragePooling' in self.name:
c = call(self, x)[self.batch_size:]
cc = k.concatenate([c, c], 0)
updates = [self.missing_impulse.assign(cc)]
else:
updates = []
with tf.control_dependencies(updates):
# Only call layer if there are input spikes. This is to prevent
# accumulation of bias.
self.impulse = \
tf.cond(k.any(k.not_equal(x[:self.batch_size], 0)),
lambda: call(self, x),
lambda: k.zeros_like(self.mem))
psp = self.update_neurons()[:self.batch_size]
return k.concatenate([psp,
self.prospective_spikes[self.batch_size:]], 0)
return decorator
示例3: jaccard_distance
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def jaccard_distance(y_true, y_pred, smooth=100):
"""Jaccard distance for semantic segmentation.
Also known as the intersection-over-union loss.
This loss is useful when you have unbalanced numbers of pixels within an image
because it gives all classes equal weight. However, it is not the defacto
standard for image segmentation.
For example, assume you are trying to predict if
each pixel is cat, dog, or background.
You have 80% background pixels, 10% dog, and 10% cat.
If the model predicts 100% background
should it be be 80% right (as with categorical cross entropy)
or 30% (with this loss)?
The loss has been modified to have a smooth gradient as it converges on zero.
This has been shifted so it converges on 0 and is smoothed to avoid exploding
or disappearing gradient.
Jaccard = (|X & Y|)/ (|X|+ |Y| - |X & Y|)
= sum(|A*B|)/(sum(|A|)+sum(|B|)-sum(|A*B|))
# Arguments
y_true: The ground truth tensor.
y_pred: The predicted tensor
smooth: Smoothing factor. Default is 100.
# Returns
The Jaccard distance between the two tensors.
# References
- [What is a good evaluation measure for semantic segmentation?](
http://www.bmva.org/bmvc/2013/Papers/paper0032/paper0032.pdf)
"""
intersection = K.sum(K.abs(y_true * y_pred), axis=-1)
sum_ = K.sum(K.abs(y_true) + K.abs(y_pred), axis=-1)
jac = (intersection + smooth) / (sum_ - intersection + smooth)
return (1 - jac) * smooth
示例4: softplus2
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def softplus2(x):
"""
out = log(exp(x)+1) - log(2)
softplus function that is 0 at x=0, the implementation aims at avoiding overflow
Args:
x: (Tensor) input tensor
Returns:
(Tensor) output tensor
"""
return kb.relu(x) + kb.log(0.5*kb.exp(-kb.abs(x)) + 0.5)
示例5: _diff_mult_dist
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def _diff_mult_dist(self, inputs: List[Tensor]) -> Tensor:
input1, input2 = inputs
a = K.abs(input1 - input2)
b = Multiply()(inputs)
return K.concatenate([input1, input2, a, b])
示例6: k_jaccard_loss
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def k_jaccard_loss(y_true, y_pred):
"""Jaccard distance for semantic segmentation.
Modified from the `keras-contrib` package.
"""
eps = 1e-12 # for stability
y_pred = K.clip(y_pred, eps, 1-eps)
intersection = K.sum(K.abs(y_true*y_pred), axis=-1)
sum_ = K.sum(K.abs(y_true) + K.abs(y_pred), axis=-1)
jac = intersection/(sum_ - intersection)
return 1 - jac
示例7: frn_layer_paper
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def frn_layer_paper(x, tau, beta, gamma, epsilon=1e-6):
# x: Input tensor of shape [BxHxWxC].
# tau, beta, gamma: Variables of shape [1, 1, 1, C].
# eps: A scalar constant or learnable variable.
# Compute the mean norm of activations per channel.
nu2 = tf.reduce_mean(tf.square(x), axis=[1, 2], keepdims=True)
# Perform FRN.
x = x * tf.math.rsqrt(nu2 + tf.abs(epsilon))
# Return after applying the Offset-ReLU non-linearity.
return tf.maximum(gamma * x + beta, tau)
示例8: frn_layer_keras
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def frn_layer_keras(x, tau, beta, gamma, epsilon=1e-6):
# x: Input tensor of shape [BxHxWxC].
# tau, beta, gamma: Variables of shape [1, 1, 1, C].
# eps: A scalar constant or learnable variable.
# Compute the mean norm of activations per channel.
nu2 = K.mean(K.square(x), axis=[1, 2], keepdims=True)
# Perform FRN.
x = x * 1 / K.sqrt(nu2 + K.abs(epsilon))
# Return after applying the Offset-ReLU non-linearity.
return K.maximum(gamma * x + beta, tau)
示例9: main
# 需要导入模块: from tensorflow.keras import backend [as 别名]
# 或者: from tensorflow.keras.backend import abs [as 别名]
def main(batch_size: int = 24,
epochs: int = 384,
train_path: str = 'train',
val_path: str = 'val',
weights=None,
workers: int = 8):
# We use an extra input during training to discount bounding box loss when a class is not present in an image.
discount_input = Input(shape=(7, 7), name='discount')
keras_model = MobileDetectNetModel.complete_model(extra_inputs=[discount_input])
keras_model.summary()
if weights is not None:
keras_model.load_weights(weights, by_name=True)
train_seq = MobileDetectNetSequence(train_path, stage="train", batch_size=batch_size)
val_seq = MobileDetectNetSequence(val_path, stage="val", batch_size=batch_size)
callbacks = []
def region_loss(classes):
def loss_fn(y_true, y_pred):
# Don't penalize bounding box errors when there is no object present
return 10 * (classes * K.abs(y_pred[:, :, :, 0] - y_true[:, :, :, 0]) +
classes * K.abs(y_pred[:, :, :, 1] - y_true[:, :, :, 1]) +
classes * K.abs(y_pred[:, :, :, 2] - y_true[:, :, :, 2]) +
classes * K.abs(y_pred[:, :, :, 3] - y_true[:, :, :, 3]))
return loss_fn
keras_model.compile(optimizer=Nadam(lr=0.001), loss=['mean_absolute_error',
region_loss(discount_input),
'binary_crossentropy'])
filepath = "weights-{epoch:02d}-{val_loss:.4f}-multi-gpu.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
callbacks.append(checkpoint)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=5, min_lr=0.00001, verbose=1)
callbacks.append(reduce_lr)
try:
os.mkdir('logs')
except FileExistsError:
pass
tensorboard = TensorBoard(log_dir='logs/%s' % time.strftime("%Y-%m-%d_%H-%M-%S"))
callbacks.append(tensorboard)
keras_model.fit_generator(train_seq,
validation_data=val_seq,
epochs=epochs,
steps_per_epoch=np.ceil(len(train_seq) / batch_size),
validation_steps=np.ceil(len(val_seq) / batch_size),
callbacks=callbacks,
use_multiprocessing=True,
workers=workers,
shuffle=True)