本文整理汇总了Python中keras.losses.mean_absolute_error方法的典型用法代码示例。如果您正苦于以下问题:Python losses.mean_absolute_error方法的具体用法?Python losses.mean_absolute_error怎么用?Python losses.mean_absolute_error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.losses
的用法示例。
在下文中一共展示了losses.mean_absolute_error方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dummy_1_build_fn
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def dummy_1_build_fn(input_shape=(1,)):
model = Sequential(
[
Embedding(input_dim=9999, output_dim=200, input_length=100, trainable=True),
SpatialDropout1D(rate=0.5),
Flatten(),
Dense(100, activation="relu"),
Dense(1, activation="sigmoid"),
]
)
model.compile(
optimizer=RMSprop(lr=0.02, decay=0.001),
loss=mean_absolute_error,
metrics=["mean_absolute_error"],
)
return model
示例2: l1
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def l1(y_true, y_pred):
""" L1 metric (MAE) """
return losses.mean_absolute_error(y_true, y_pred)
示例3: photometric_consistency_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def photometric_consistency_loss(alpha):
def loss(y_true, y_pred):
return alpha * ssim(y_true, y_pred) + (1 - alpha) * mean_absolute_error(y_true, y_pred)
return loss
示例4: anomaly_correlation
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def anomaly_correlation(y_true, y_pred, mean=0., regularize_mean='mse', reverse=True):
"""
Calculate the anomaly correlation. FOR NOW, ASSUMES THAT THE CLIMATOLOGICAL MEAN IS 0, AND THEREFORE REQUIRES DATA
TO BE SCALED TO REMOVE SPATIALLY-DEPENDENT MEAN.
:param y_true: Tensor: target values
:param y_pred: Tensor: model-predicted values
:param mean: float: subtract this global mean from all predicted and target array values. IGNORED FOR NOW.
:param regularize_mean: str or None: if not None, also penalizes a form of mean squared error:
global: penalize differences in the global mean
spatial: penalize differences in spatially-averaged mean (last two dimensions)
mse: penalize the mean squared error
mae: penalize the mean absolute error
:param reverse: bool: if True, inverts the loss so that -1 is the target score
:return: float: anomaly correlation loss
"""
if regularize_mean is not None:
assert regularize_mean in ['global', 'spatial', 'mse', 'mae']
a = (K.mean(y_pred * y_true)
/ K.sqrt(K.mean(K.square(y_pred)) * K.mean(K.square(y_true))))
if regularize_mean is not None:
if regularize_mean == 'global':
m = K.abs((K.mean(y_true) - K.mean(y_pred)) / K.mean(y_true))
elif regularize_mean == 'spatial':
m = K.mean(K.abs((K.mean(y_true, axis=[-2, -1]) - K.mean(y_pred, axis=[-2, -1]))
/ K.mean(y_true, axis=[-2, -1])))
elif regularize_mean == 'mse':
m = mean_squared_error(y_true, y_pred)
elif regularize_mean == 'mae':
m = mean_absolute_error(y_true, y_pred)
if reverse:
if regularize_mean is not None:
return m - a
else:
return -a
else:
if regularize_mean:
return a - m
else:
return a
示例5: loss_dict
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def loss_dict(self):
""" Return the loss dict """
loss_dict = dict(mae=losses.mean_absolute_error,
mse=losses.mean_squared_error,
logcosh=losses.logcosh,
smooth_loss=generalized_loss,
l_inf_norm=l_inf_norm,
ssim=DSSIMObjective(),
gmsd=gmsd_loss,
pixel_gradient_diff=gradient_loss)
return loss_dict
示例6: mae
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def mae(self, hr, sr):
margin = (tf.shape(hr)[1] - tf.shape(sr)[1]) // 2
hr_crop = tf.cond(tf.equal(margin, 0), lambda: hr, lambda: hr[:, margin:-margin, margin:-margin, :])
hr = K.in_train_phase(hr_crop, hr)
hr.uses_learning_phase = True
return mean_absolute_error(hr, sr)
示例7: combined_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def combined_loss(y_true, y_pred):
'''
Uses a combination of mean_squared_error and an L1 penalty on the output of AE
'''
return mse(y_true, y_pred) + 0.01*mae(0, y_pred)
示例8: anomaly_correlation_loss
# 需要导入模块: from keras import losses [as 别名]
# 或者: from keras.losses import mean_absolute_error [as 别名]
def anomaly_correlation_loss(mean=None, regularize_mean='mse', reverse=True):
"""
Create a Keras loss function for anomaly correlation.
:param mean: ndarray or None: if not None, must be an array with the same shape as the expected prediction, except
that the first (batch) axis should have a dimension of 1.
:param regularize_mean: str or None: if not None, also penalizes a form of mean squared error:
global: penalize differences in the global mean
spatial: penalize differences in spatially-averaged mean (last two dimensions)
mse: penalize the mean squared error
mae: penalize the mean absolute error
:param reverse: bool: if True, inverts the loss so that -1 is the (minimized) target score. Must be True if
regularize_mean is not None.
:return: method: anomaly correlation loss function
"""
if mean is not None:
assert len(mean.shape) > 1
assert mean.shape[0] == 1
mean_tensor = K.variable(mean, name='anomaly_correlation_mean')
if regularize_mean is not None:
assert regularize_mean in ['global', 'spatial', 'mse', 'mae']
reverse = True
def acc_loss(y_true, y_pred):
if mean is not None:
a = (K.mean((y_pred - mean_tensor) * (y_true - mean_tensor))
/ K.sqrt(K.mean(K.square((y_pred - mean_tensor))) * K.mean(K.square((y_true - mean_tensor)))))
else:
a = (K.mean(y_pred * y_true)
/ K.sqrt(K.mean(K.square(y_pred)) * K.mean(K.square(y_true))))
if regularize_mean is not None:
if regularize_mean == 'global':
m = K.abs((K.mean(y_true) - K.mean(y_pred)) / K.mean(y_true))
elif regularize_mean == 'spatial':
m = K.mean(K.abs((K.mean(y_true, axis=[-2, -1]) - K.mean(y_pred, axis=[-2, -1]))
/ K.mean(y_true, axis=[-2, -1])))
elif regularize_mean == 'mse':
m = mean_squared_error(y_true, y_pred)
elif regularize_mean == 'mae':
m = mean_absolute_error(y_true, y_pred)
if reverse:
if regularize_mean is not None:
return m - a
else:
return -a
else:
if regularize_mean:
return a - m
else:
return a
return acc_loss
# Compatibility names