本文整理汇总了Python中tensorflow.keras.optimizers方法的典型用法代码示例。如果您正苦于以下问题:Python keras.optimizers方法的具体用法?Python keras.optimizers怎么用?Python keras.optimizers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.keras
的用法示例。
在下文中一共展示了keras.optimizers方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_optimizer
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import optimizers [as 别名]
def init_optimizer(optimizer_string, logger=None, **kwargs):
"""
Same as 'init_losses', but for optimizers.
Please refer to the 'init_losses' docstring.
"""
optimizer = _init(
optimizer_string,
tf_funcs=[optimizers, addon_optimizers],
custom_funcs=None,
logger=logger
)[0]
return optimizer(**kwargs)
示例2: init_activation
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import optimizers [as 别名]
def init_activation(activation_string, logger=None, **kwargs):
"""
Same as 'init_losses', but for optimizers.
Please refer to the 'init_losses' docstring.
"""
activation = _init(
activation_string,
tf_funcs=[activations, addon_activations],
custom_funcs=None,
logger=logger
)[0]
return activation
示例3: compile
# 需要导入模块: from tensorflow import keras [as 别名]
# 或者: from tensorflow.keras import optimizers [as 别名]
def compile(self, model: Model, optimizer_name: str, loss_name: str,
learning_rate: Optional[Union[float, List[float]]],
learning_rate_decay: Optional[Union[float, str]]) -> Model:
"""
Compile model with given optimizer and loss
Args:
model: keras uncompiled model
optimizer_name: name of optimizer from keras.optimizers
loss_name: loss function name (from keras.losses)
learning_rate: learning rate.
learning_rate_decay: learning rate decay.
Returns:
"""
optimizer_func = getattr(tensorflow.keras.optimizers, optimizer_name, None)
if callable(optimizer_func):
if isinstance(learning_rate, float) and isinstance(learning_rate_decay, float):
# in this case decay will be either given in config or, by default, learning_rate_decay=0.
self.optimizer = optimizer_func(lr=learning_rate, decay=learning_rate_decay)
else:
self.optimizer = optimizer_func()
else:
raise AttributeError("Optimizer {} is not defined in `tensorflow.keras.optimizers`".format(optimizer_name))
loss_func = getattr(tensorflow.keras.losses, loss_name, None)
if callable(loss_func):
loss = loss_func
else:
raise AttributeError("Loss {} is not defined".format(loss_name))
model.compile(optimizer=self.optimizer,
loss=loss)
return model