本文整理匯總了Python中tensorflow.keras.backend.mean方法的典型用法代碼示例。如果您正苦於以下問題:Python backend.mean方法的具體用法?Python backend.mean怎麽用?Python backend.mean使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.keras.backend
的用法示例。
在下文中一共展示了backend.mean方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: call
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def call(self, inputs):
features = inputs[0]
fltr = inputs[1]
# Convolution
output = [] # Stores the parallel filters
for k in range(self.order):
output_k = features
for i in range(self.iterations):
output_k = self.gcs([output_k, features, fltr], k, i)
output.append(output_k)
# Average stacks
output = K.stack(output, axis=-1)
output = K.mean(output, axis=-1)
output = self.activation(output)
return output
示例2: mean_dice
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def mean_dice(self, y_true, y_pred):
""" weighted mean dice across all patches and labels """
# compute dice, which will now be [batch_size, nb_labels]
dice_metric = self.dice(y_true, y_pred)
# weigh the entries in the dice matrix:
if self.weights is not None:
dice_metric *= self.weights
if self.vox_weights is not None:
dice_metric *= self.vox_weights
# return one minus mean dice as loss
mean_dice_metric = K.mean(dice_metric)
tf.verify_tensor_all_finite(mean_dice_metric, 'metric not finite')
return mean_dice_metric
示例3: loss
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def loss(self, y_true, y_pred):
""" the loss. Assumes y_pred is prob (in [0,1] and sum_row = 1) """
# compute dice, which will now be [batch_size, nb_labels]
dice_metric = self.dice(y_true, y_pred)
# loss
dice_loss = 1 - dice_metric
# weigh the entries in the dice matrix:
if self.weights is not None:
dice_loss *= self.weights
# return one minus mean dice as loss
mean_dice_loss = K.mean(dice_loss)
tf.verify_tensor_all_finite(mean_dice_loss, 'Loss not finite')
return mean_dice_loss
示例4: build
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def build(self, input_shape):
# Create mean and count
# These are weights because just maintaining variables don't get saved with the model, and we'd like
# to have these numbers saved when we save the model.
# But we need to make sure that the weights are untrainable.
self.mean = self.add_weight(name='mean',
shape=input_shape[1:],
initializer='zeros',
trainable=False)
self.count = self.add_weight(name='count',
shape=[1],
initializer='zeros',
trainable=False)
# self.mean = K.zeros(input_shape[1:], name='mean')
# self.count = K.variable(0.0, name='count')
super(MeanStream, self).build(input_shape) # Be sure to call this somewhere!
示例5: _mean_update
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def _mean_update(pre_mean, pre_count, x, pre_cap=None):
# compute this batch stats
this_sum = tf.reduce_sum(x, 0)
this_bs = tf.cast(K.shape(x)[0], 'float32') # this batch size
# increase count and compute weights
new_count = pre_count + this_bs
alpha = this_bs/K.minimum(new_count, pre_cap)
# compute new mean. Note that once we reach self.cap (e.g. 1000), the 'previous mean' matters less
new_mean = pre_mean * (1-alpha) + (this_sum/this_bs) * alpha
return (new_mean, new_count)
##########################################
## FFT Layers
##########################################
示例6: sampling
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def sampling(args):
"""Reparameterization trick by sampling
fr an isotropic unit Gaussian.
# Arguments:
args (tensor): mean and log of variance of Q(z|X)
# Returns:
z (tensor): sampled latent vector
"""
z_mean, z_log_var = args
# K is the keras backend
batch = K.shape(z_mean)[0]
dim = K.int_shape(z_mean)[1]
# by default, random_normal has mean=0 and std=1.0
epsilon = K.random_normal(shape=(batch, dim))
return z_mean + K.exp(0.5 * z_log_var) * epsilon
示例7: sampling
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def sampling(args):
"""Implements reparameterization trick by sampling
from a gaussian with zero mean and std=1.
Arguments:
args (tensor): mean and log of variance of Q(z|X)
Returns:
sampled latent vector (tensor)
"""
z_mean, z_log_var = args
batch = K.shape(z_mean)[0]
dim = K.int_shape(z_mean)[1]
# by default, random_normal has mean=0 and std=1.0
epsilon = K.random_normal(shape=(batch, dim))
return z_mean + K.exp(0.5 * z_log_var) * epsilon
示例8: sampling
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def sampling(args):
"""Reparameterization trick by sampling
fr an isotropic unit Gaussian.
# Arguments:
args (tensor): mean and log of variance of Q(z|X)
# Returns:
z (tensor): sampled latent vector
"""
z_mean, z_log_var = args
batch = K.shape(z_mean)[0]
dim = K.int_shape(z_mean)[1]
# by default, random_normal has mean=0 and std=1.0
epsilon = K.random_normal(shape=(batch, dim))
return z_mean + K.exp(0.5 * z_log_var) * epsilon
示例9: mi_loss
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [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], :]
# implentation of MINE loss (Eq 13.7.3)
loss = K.mean(pred_xy) \
- K.log(K.mean(K.exp(pred_x_y)))
return -loss
示例10: alignment_eval
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def alignment_eval(y_true, y_pred, image_size):
"""
y_true is defined in Radian [-pi, pi] (ZYZ convention) for rotation, and voxels for translation
y_pred is in [0, 1] from sigmoid activation, need to scale y_pred for comparison
"""
ang_d = []
loc_d = []
for i in range(len(y_true)):
a = angle_zyz_difference(ang1 = y_true[i][:3], ang2 = y_pred[i][:3] * 2 * np.pi - np.pi)
b = np.linalg.norm(np.round(y_true[i][3:6]) - np.round((y_pred[i][3:6] * 2 - 1) * (image_size/2) ))
ang_d.append(a)
loc_d.append(b)
print('Rotation error: ', np.mean(ang_d), '+/-', np.std(ang_d), 'Translation error: ', np.mean(loc_d), '+/-', np.std(loc_d), '----------')
示例11: dice_soft
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def dice_soft(y_true, y_pred, smooth=0.00001):
# Identify axis
axis = identify_axis(y_true.get_shape())
# Calculate required variables
intersection = y_true * y_pred
intersection = K.sum(intersection, axis=axis)
y_true = K.sum(y_true, axis=axis)
y_pred = K.sum(y_pred, axis=axis)
# Calculate Soft Dice Similarity Coefficient
dice = ((2 * intersection) + smooth) / (y_true + y_pred + smooth)
# Obtain mean of Dice & return result score
dice = K.mean(dice)
return dice
示例12: dice_crossentropy
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def dice_crossentropy(y_truth, y_pred):
# Obtain Soft DSC
dice = dice_soft_loss(y_truth, y_pred)
# Obtain Crossentropy
crossentropy = K.categorical_crossentropy(y_truth, y_pred)
crossentropy = K.mean(crossentropy)
# Return sum
return dice + crossentropy
#-----------------------------------------------------#
# Tversky loss #
#-----------------------------------------------------#
# Reference: #
# Sadegh et al. (2017) #
# Tversky loss function for image segmentation #
# using 3D fully convolutional deep networks #
#-----------------------------------------------------#
# alpha=beta=0.5 : dice coefficient #
# alpha=beta=1 : jaccard #
# alpha+beta=1 : produces set of F*-scores #
#-----------------------------------------------------#
示例13: convert_reduce_mean
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def convert_reduce_mean(node, params, layers, lambda_func, node_name, keras_name):
"""
Convert reduce mean.
:param node: current operation node
:param params: operation attributes
:param layers: available keras layers
:param lambda_func: function for keras Lambda layer
:param node_name: internal converter name
:param keras_name: resulting layer name
:return: None
"""
if len(node.input) != 1:
assert AttributeError('More than 1 input for reduce mean layer.')
input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)
def target_layer(x, axis=params['axes'], keepdims=params['keepdims']):
import tensorflow.keras.backend as K
return K.mean(x, keepdims=(keepdims == 1), axis=axis)
lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)
layers[node_name].set_shape(layers[node_name].shape)
lambda_func[keras_name] = target_layer
示例14: get_iou_score
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def get_iou_score(class_weights=1., smooth=SMOOTH, per_image=True, threshold=None):
"""Change default parameters of IoU/Jaccard score
Args:
class_weights: 1. or list of class weights, len(weights) = C
smooth: value to avoid division by zero
per_image: if ``True``, metric is calculated as mean over images in batch (B),
else over whole batch
threshold: value to round predictions (use ``>`` comparison), if ``None`` prediction prediction will not be round
Returns:
``callable``: IoU/Jaccard score
"""
def score(gt, pr):
return iou_score(gt, pr, class_weights=class_weights, smooth=smooth, per_image=per_image, threshold=threshold)
return score
示例15: get_f_score
# 需要導入模塊: from tensorflow.keras import backend [as 別名]
# 或者: from tensorflow.keras.backend import mean [as 別名]
def get_f_score(class_weights=1, beta=1, smooth=SMOOTH, per_image=True, threshold=None):
"""Change default parameters of F-score score
Args:
class_weights: 1. or list of class weights, len(weights) = C
smooth: value to avoid division by zero
beta: f-score coefficient
per_image: if ``True``, metric is calculated as mean over images in batch (B),
else over whole batch
threshold: value to round predictions (use ``>`` comparison), if ``None`` prediction prediction will not be round
Returns:
``callable``: F-score
"""
def score(gt, pr):
return f_score(gt, pr, class_weights=class_weights, beta=beta, smooth=smooth, per_image=per_image, threshold=threshold)
return score