本文整理匯總了Python中tensorflow.keras.backend.max方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.max方法的具體用法?Python backend.max怎麽用?Python backend.max使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.max方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: amplitude_to_decibel
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def amplitude_to_decibel(x, amin=1e-10, dynamic_range=80.0):
"""[K] Convert (linear) amplitude to decibel (log10(x)).
Parameters
----------
x: Keras *batch* tensor or variable. It has to be batch because of sample-wise `K.max()`.
amin: minimum amplitude. amplitude smaller than `amin` is set to this.
dynamic_range: dynamic_range in decibel
"""
log_spec = 10 * K.log(K.maximum(x, amin)) / np.log(10).astype(K.floatx())
if K.ndim(x) > 1:
axis = tuple(range(K.ndim(x))[1:])
else:
axis = None
log_spec = log_spec - K.max(log_spec, axis=axis, keepdims=True) # [-?, 0]
log_spec = K.maximum(log_spec, -1 * dynamic_range) # [-80, 0]
return log_spec
示例2: _softmax
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def _softmax(x, axis=-1, alpha=1):
"""
building on keras implementation, with additional alpha parameter
Softmax activation function.
# Arguments
x : Tensor.
axis: Integer, axis along which the softmax normalization is applied.
alpha: a value to multiply all x
# Returns
Tensor, output of softmax transformation.
# Raises
ValueError: In case `dim(x) == 1`.
"""
x = alpha * x
ndim = K.ndim(x)
if ndim == 2:
return K.softmax(x)
elif ndim > 2:
e = K.exp(x - K.max(x, axis=axis, keepdims=True))
s = K.sum(e, axis=axis, keepdims=True)
return e / s
else:
raise ValueError('Cannot apply softmax to a tensor that is 1D')
示例3: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def call(self, x, **kwargs):
assert isinstance(x, list)
inp_a, inp_b = x
m = []
for i in range(self.output_dim):
outp_a = inp_a * self.W[i]
outp_b = inp_b * self.W[i]
outp_a = K.l2_normalize(outp_a, -1)
outp_b = K.l2_normalize(outp_b, -1)
outp = K.batch_dot(outp_a, outp_b, axes=[2, 2])
outp = K.max(outp, -1, keepdims=True)
m.append(outp)
if self.output_dim > 1:
persp = K.concatenate(m, 2)
else:
persp = m[0]
return [persp, persp]
示例4: _find_maxima
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def _find_maxima(x, coordinate_scale=1, confidence_scale=255.0):
x = K.cast(x, K.floatx())
col_max = K.max(x, axis=1)
row_max = K.max(x, axis=2)
maxima = K.max(col_max, 1)
maxima = K.expand_dims(maxima, -2) / confidence_scale
cols = K.cast(K.argmax(col_max, -2), K.floatx())
rows = K.cast(K.argmax(row_max, -2), K.floatx())
cols = K.expand_dims(cols, -2) * coordinate_scale
rows = K.expand_dims(rows, -2) * coordinate_scale
maxima = K.concatenate([cols, rows, maxima], -2)
return maxima
示例5: mean_q
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def mean_q(y_true, y_pred):
return K.mean(K.max(y_pred, axis=-1))
示例6: _hard_max
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def _hard_max(tens, axis):
"""
we can't use the argmax function in a loss, as it's not differentiable
We can use it in a metric, but not in a loss function
therefore, we replace the 'hard max' operation (i.e. argmax + onehot)
with this approximation
"""
tensmax = K.max(tens, axis=axis, keepdims=True)
eps_hot = K.maximum(tens - tensmax + K.epsilon(), 0)
one_hot = eps_hot / K.epsilon()
return one_hot
示例7: next_pred_label
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def next_pred_label(model, data_generator, verbose=False):
"""
predict the next sample batch from the generator, and compute max labels
return sample, prediction, max_labels
"""
sample = next(data_generator)
with timer.Timer('prediction', verbose):
pred = model.predict(sample[0])
sample_input = sample[0] if not isinstance(sample[0], (list, tuple)) else sample[0][0]
max_labels = pred_to_label(sample_input, pred)
return (sample, pred) + max_labels
示例8: next_label
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def next_label(model, data_generator):
"""
predict the next sample batch from the generator, and compute max labels
return max_labels
"""
batch_proc = next_pred_label(model, data_generator)
return (batch_proc[2], batch_proc[3])
示例9: max
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def max(self):
"""Get maximum value that quantized_bits class can represent."""
unsigned_bits = self.bits - self.keep_negative
if unsigned_bits > 0:
return max(1.0, np.power(2.0, self.integer))
else:
return 1.0
示例10: min
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def min(self):
"""Get minimum value that quantized_bits class can represent."""
if not self.keep_negative:
return 0.0
unsigned_bits = self.bits - self.keep_negative
if unsigned_bits > 0:
return -max(1.0, np.power(2.0, self.integer))
else:
return -1.0
示例11: _chebyshev_distance
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def _chebyshev_distance(x, y):
return K.max(K.abs(x - y), axis=-1, keepdims=True)
示例12: consecutive_indexed
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def consecutive_indexed(Y):
""" Assumes that Y is zero-indexed. """
n_classes = len(np.unique(Y[Y != np.array(-1)]))
if max(Y) >= n_classes:
return False
return True
示例13: qscore
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def qscore(y_true, y_pred):
"""Keras metric function for calculating scaled error.
:param y_true: tensor of true class labels.
:param y_pred: class output scores from network.
:returns: class error expressed as a phred score.
"""
from tensorflow.keras import backend as K
error = K.cast(K.not_equal(
K.max(y_true, axis=-1), K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx()
)
error = K.sum(error) / K.sum(K.ones_like(error))
return -10.0 * 0.434294481 * K.log(error)
示例14: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def call(self, inputs, **kwargs):
sent1 = inputs[0]
sent2 = inputs[1]
v1 = K.expand_dims(sent1, -2) * self.kernel
v2 = K.expand_dims(sent2, -2) * self.kernel
v1 = K.l2_normalize(v1, axis=-1)
v2 = K.l2_normalize(v2, axis=-1)
matching = K.max(K.sum(K.expand_dims(v1, 2) * K.expand_dims(v2, 1), axis=-1), axis=-2)
return matching
示例15: _batch_hard_triplet_loss
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import max [as 別名]
def _batch_hard_triplet_loss(self, y_true: Tensor, pairwise_dist: Tensor) -> Tensor:
mask_anchor_positive = self._get_anchor_positive_triplet_mask(y_true, pairwise_dist)
anchor_positive_dist = mask_anchor_positive * pairwise_dist
hardest_positive_dist = K.max(anchor_positive_dist, axis=1, keepdims=True)
mask_anchor_negative = self._get_anchor_negative_triplet_mask(y_true, pairwise_dist)
anchor_negative_dist = mask_anchor_negative * pairwise_dist
mask_anchor_negative = self._get_semihard_anchor_negative_triplet_mask(anchor_negative_dist,
hardest_positive_dist,
mask_anchor_negative)
max_anchor_negative_dist = K.max(pairwise_dist, axis=1, keepdims=True)
anchor_negative_dist = pairwise_dist + max_anchor_negative_dist * (1.0 - mask_anchor_negative)
hardest_negative_dist = K.min(anchor_negative_dist, axis=1, keepdims=True)
triplet_loss = K.clip(hardest_positive_dist - hardest_negative_dist + self.margin, 0.0, None)
triplet_loss = K.mean(triplet_loss)
return triplet_loss