Python tensorflow.keras.backend.abs() Examples
The following are 9
code examples of tensorflow.keras.backend.abs().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.keras.backend
, or try the search function
.
Example #1
Source File: util.py From keras-rl2 with MIT License | 6 votes |
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
Example #2
Source File: ttfs_dyn_thresh.py From snn_toolbox with MIT License | 5 votes |
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
Example #3
Source File: losses.py From keras-unet with MIT License | 5 votes |
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
Example #4
Source File: activations.py From megnet with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #5
Source File: bilstm_siamese_network.py From DeepPavlov with Apache License 2.0 | 5 votes |
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])
Example #6
Source File: _keras_losses.py From solaris with Apache License 2.0 | 5 votes |
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
Example #7
Source File: FRN.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: FRN.py From TF.Keras-Commonly-used-models with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: train.py From keras-mobile-detectnet with MIT License | 4 votes |
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)