本文整理汇总了Python中keras.optimizers.Adamax方法的典型用法代码示例。如果您正苦于以下问题:Python optimizers.Adamax方法的具体用法?Python optimizers.Adamax怎么用?Python optimizers.Adamax使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.optimizers
的用法示例。
在下文中一共展示了optimizers.Adamax方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_optimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_optimizer(args):
clipvalue = 0
clipnorm = 10
if args.algorithm == 'rmsprop':
optimizer = opt.RMSprop(lr=0.001, rho=0.9, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'sgd':
optimizer = opt.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adagrad':
optimizer = opt.Adagrad(lr=0.01, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adadelta':
optimizer = opt.Adadelta(lr=1.0, rho=0.95, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adam':
optimizer = opt.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adamax':
optimizer = opt.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
return optimizer
示例2: get_optimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_optimizer(config):
if config.OPTIMIZER == 'SGD':
return SGD(lr=config.LEARNING_RATE, momentum=config.LEARNING_MOMENTUM, clipnorm=config.GRADIENT_CLIP_NORM, nesterov=config.NESTEROV)
elif config.OPTIMIZER == 'RMSprop':
return RMSprop(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM)
elif config.OPTIMIZER == 'Adagrad':
return Adagrad(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM)
elif config.OPTIMIZER == 'Adadelta':
return Adadelta(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM)
elif config.OPTIMIZER == 'Adam':
return Adam(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM, amsgrad=config.AMSGRAD)
elif config.OPTIMIZER == 'Adamax':
return Adamax(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM)
elif config.OPTIMIZER == 'Nadam':
return Nadam(lr=config.LEARNING_RATE, clipnorm=config.GRADIENT_CLIP_NORM)
else:
raise Exception('Unrecognized optimizer: {}'.format(config.OPTIMIZER))
示例3: get_optimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_optimizer(name='Adadelta'):
if name == 'SGD':
return optimizers.SGD(clipnorm=1.)
if name == 'RMSprop':
return optimizers.RMSprop(clipnorm=1.)
if name == 'Adagrad':
return optimizers.Adagrad(clipnorm=1.)
if name == 'Adadelta':
return optimizers.Adadelta(clipnorm=1.)
if name == 'Adam':
return optimizers.Adam(clipnorm=1.)
if name == 'Adamax':
return optimizers.Adamax(clipnorm=1.)
if name == 'Nadam':
return optimizers.Nadam(clipnorm=1.)
return optimizers.Adam(clipnorm=1.)
示例4: get_optimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_optimizer(args):
clipvalue = 0
clipnorm = 10
if args.algorithm == 'rmsprop':
optimizer = opt.RMSprop(lr=0.0001, rho=0.9, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'sgd':
optimizer = opt.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adagrad':
optimizer = opt.Adagrad(lr=0.01, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adadelta':
optimizer = opt.Adadelta(lr=1.0, rho=0.95, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adam':
optimizer = opt.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adamax':
optimizer = opt.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
return optimizer
示例5: get_optimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_optimizer(args):
clipvalue = 0
clipnorm = 10
if args.algorithm == 'rmsprop':
optimizer = opt.RMSprop(lr=0.0005, rho=0.9, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'sgd':
optimizer = opt.SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adagrad':
optimizer = opt.Adagrad(lr=0.01, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adadelta':
optimizer = opt.Adadelta(lr=1.0, rho=0.95, epsilon=1e-06, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adam':
optimizer = opt.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
elif args.algorithm == 'adamax':
optimizer = opt.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, clipnorm=clipnorm, clipvalue=clipvalue)
return optimizer
示例6: get_learning_rate
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def get_learning_rate(self):
if hasattr(self.model, 'optimizer'):
config = self.model.optimizer.get_config()
from keras.optimizers import Adadelta, Adam, Adamax, Adagrad, RMSprop, SGD
if isinstance(self.model.optimizer, Adadelta) or isinstance(self.model.optimizer, Adam) \
or isinstance(self.model.optimizer, Adamax) or isinstance(self.model.optimizer, Adagrad)\
or isinstance(self.model.optimizer, RMSprop) or isinstance(self.model.optimizer, SGD):
return config['lr'] * (1. / (1. + config['decay'] * float(K.get_value(self.model.optimizer.iterations))))
elif 'lr' in config:
return config['lr']
示例7: test_adamax
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def test_adamax():
_test_optimizer(optimizers.Adamax())
_test_optimizer(optimizers.Adamax(decay=1e-3))
示例8: lr_normalizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def lr_normalizer(lr, optimizer):
"""Assuming a default learning rate 1, rescales the learning rate
such that learning rates amongst different optimizers are more or less
equivalent.
Parameters
----------
lr : float
The learning rate.
optimizer : keras optimizer
The optimizer. For example, Adagrad, Adam, RMSprop.
"""
from keras.optimizers import SGD, Adam, Adadelta, Adagrad, Adamax, RMSprop
from keras.optimizers import Nadam
from talos.utils.exceptions import TalosModelError
if optimizer == Adadelta:
pass
elif optimizer == SGD or optimizer == Adagrad:
lr /= 100.0
elif optimizer == Adam or optimizer == RMSprop:
lr /= 1000.0
elif optimizer == Adamax or optimizer == Nadam:
lr /= 500.0
else:
raise TalosModelError(str(optimizer) + " is not supported by lr_normalizer")
return lr
示例9: _compile
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def _compile(self, model, loss_function, optimizer, lr=0.01, decay=0.0, clipnorm=0.0):
"""Compiles a model specified with Keras.
See https://keras.io/optimizers/ for more info on each optimizer.
Args:
model: Keras model object to compile
loss_function: Keras loss_function object to compile model with
optimizer (str): the optimizer to use during training
lr (float): learning rate to use during training
decay (float): per epoch decay rate
clipnorm (float): gradient normalization threshold
"""
# The parameters of these optimizers can be freely tuned.
if optimizer == 'sgd':
optimizer_ = optimizers.SGD(lr=lr, decay=decay, clipnorm=clipnorm)
elif optimizer == 'adam':
optimizer_ = optimizers.Adam(lr=lr, decay=decay, clipnorm=clipnorm)
elif optimizer == 'adamax':
optimizer_ = optimizers.Adamax(lr=lr, decay=decay, clipnorm=clipnorm)
# It is recommended to leave the parameters of this optimizer at their
# default values (except the learning rate, which can be freely tuned).
# This optimizer is usually a good choice for recurrent neural networks
elif optimizer == 'rmsprop':
optimizer_ = optimizers.RMSprop(lr=lr, clipnorm=clipnorm)
# It is recommended to leave the parameters of these optimizers at their
# default values.
elif optimizer == 'adagrad':
optimizer_ = optimizers.Adagrad(clipnorm=clipnorm)
elif optimizer == 'adadelta':
optimizer_ = optimizers.Adadelta(clipnorm=clipnorm)
elif optimizer == 'nadam':
optimizer_ = optimizers.Nadam(clipnorm=clipnorm)
else:
err_msg = "Argument for `optimizer` is invalid, got: {}".format(optimizer)
LOGGER.error('ValueError %s', err_msg)
raise ValueError(err_msg)
model.compile(optimizer=optimizer_, loss=loss_function)
示例10: lstm_model
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def lstm_model(self):
model = Sequential()
first = True
for idx in range(len(self.paras.model['hidden_layers'])):
if idx == (len(self.paras.model['hidden_layers']) - 1):
model.add(LSTM(int(self.paras.model['hidden_layers'][idx]), return_sequences=False))
model.add(Activation(self.paras.model['activation']))
model.add(Dropout(self.paras.model['dropout']))
elif first == True:
model.add(LSTM(input_shape=(None, int(self.paras.n_features)),
units=int(self.paras.model['hidden_layers'][idx]),
return_sequences=True))
model.add(Activation(self.paras.model['activation']))
model.add(Dropout(self.paras.model['dropout']))
first = False
else:
model.add(LSTM(int(self.paras.model['hidden_layers'][idx]), return_sequences=True))
model.add(Activation(self.paras.model['activation']))
model.add(Dropout(self.paras.model['dropout']))
if self.paras.model['optimizer'] == 'sgd':
#optimizer = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
optimizer = optimizers.SGD(lr=self.paras.model['learning_rate'], decay=1e-6, momentum=0.9, nesterov=True)
elif self.paras.model['optimizer'] == 'rmsprop':
#optimizer = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
optimizer = optimizers.RMSprop(lr=self.paras.model['learning_rate']/10, rho=0.9, epsilon=1e-08, decay=0.0)
elif self.paras.model['optimizer'] == 'adagrad':
#optimizer = optimizers.Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)
optimizer = optimizers.Adagrad(lr=self.paras.model['learning_rate'], epsilon=1e-08, decay=0.0)
elif self.paras.model['optimizer'] == 'adam':
#optimizer = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
optimizer = optimizers.Adam(lr=self.paras.model['learning_rate']/10, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
elif self.paras.model['optimizer'] == 'adadelta':
optimizer = optimizers.Adadelta(lr=1.0, rho=0.95, epsilon=1e-08, decay=0.0)
elif self.paras.model['optimizer'] == 'adamax':
optimizer = optimizers.Adamax(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
elif self.paras.model['optimizer'] == 'nadam':
optimizer = optimizers.Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004)
else:
optimizer = optimizers.Adam(lr=self.paras.model['learning_rate']/10, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
# output layer
model.add(Dense(units=self.paras.model['out_layer']))
model.add(Activation(self.paras.model['out_activation']))
model.compile(loss=self.paras.model['loss'], optimizer=optimizer, metrics=['accuracy'])
return model
示例11: parse_rev_args
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def parse_rev_args(receive_msg):
""" parse reveive msgs to global variable
"""
global trainloader
global testloader
global net
# Loading Data
logger.debug("Preparing data..")
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
x_train = x_train.reshape(x_train.shape+(1,)).astype("float32")
x_test = x_test.reshape(x_test.shape+(1,)).astype("float32")
x_train /= 255.0
x_test /= 255.0
trainloader = (x_train, y_train)
testloader = (x_test, y_test)
# Model
logger.debug("Building model..")
net = build_graph_from_json(receive_msg)
# parallel model
try:
available_devices = os.environ["CUDA_VISIBLE_DEVICES"]
gpus = len(available_devices.split(","))
if gpus > 1:
net = multi_gpu_model(net, gpus)
except KeyError:
logger.debug("parallel model not support in this config settings")
if args.optimizer == "SGD":
optimizer = SGD(lr=args.learning_rate, momentum=0.9, decay=args.weight_decay)
if args.optimizer == "Adadelta":
optimizer = Adadelta(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adagrad":
optimizer = Adagrad(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adam":
optimizer = Adam(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adamax":
optimizer = Adamax(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "RMSprop":
optimizer = RMSprop(lr=args.learning_rate, decay=args.weight_decay)
# Compile the model
net.compile(
loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]
)
return 0
示例12: parse_rev_args
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def parse_rev_args(receive_msg):
""" parse reveive msgs to global variable
"""
global trainloader
global testloader
global net
# Loading Data
logger.debug("Preparing data..")
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
x_train = x_train.astype("float32")
x_test = x_test.astype("float32")
x_train /= 255.0
x_test /= 255.0
trainloader = (x_train, y_train)
testloader = (x_test, y_test)
# Model
logger.debug("Building model..")
net = build_graph_from_json(receive_msg)
# parallel model
try:
available_devices = os.environ["CUDA_VISIBLE_DEVICES"]
gpus = len(available_devices.split(","))
if gpus > 1:
net = multi_gpu_model(net, gpus)
except KeyError:
logger.debug("parallel model not support in this config settings")
if args.optimizer == "SGD":
optimizer = SGD(lr=args.learning_rate, momentum=0.9, decay=args.weight_decay)
if args.optimizer == "Adadelta":
optimizer = Adadelta(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adagrad":
optimizer = Adagrad(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adam":
optimizer = Adam(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "Adamax":
optimizer = Adamax(lr=args.learning_rate, decay=args.weight_decay)
if args.optimizer == "RMSprop":
optimizer = RMSprop(lr=args.learning_rate, decay=args.weight_decay)
# Compile the model
net.compile(
loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]
)
return 0
示例13: setOptimizer
# 需要导入模块: from keras import optimizers [as 别名]
# 或者: from keras.optimizers import Adamax [as 别名]
def setOptimizer(self, lr=None, momentum=None, loss=None, loss_weights=None, metrics=None, epsilon=1e-8,
nesterov=True, decay=0.0, clipnorm=10., clipvalue=0., optimizer=None, sample_weight_mode=None):
"""
Sets a new optimizer for the CNN model.
:param lr: learning rate of the network
:param momentum: momentum of the network (if None, then momentum = 1-lr)
:param loss: loss function applied for optimization
:param loss_weights: weights given to multi-loss models
:param metrics: list of Keras' metrics used for evaluating the model. To specify different metrics for different outputs of a multi-output model, you could also pass a dictionary, such as `metrics={'output_a': 'accuracy'}`.
:param epsilon: fuzz factor
:param decay: lr decay
:param clipnorm: gradients' clip norm
:param optimizer: string identifying the type of optimizer used (default: SGD)
:param sample_weight_mode: 'temporal' or None
"""
# Pick default parameters
if lr is None:
lr = self.lr
else:
self.lr = lr
if momentum is None:
momentum = self.momentum
else:
self.momentum = momentum
if loss is None:
loss = self.loss
else:
self.loss = loss
if metrics is None:
metrics = []
if optimizer is None or optimizer.lower() == 'sgd':
optimizer = SGD(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, momentum=momentum,
nesterov=nesterov)
elif optimizer.lower() == 'adam':
optimizer = Adam(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
elif optimizer.lower() == 'adagrad':
optimizer = Adagrad(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
elif optimizer.lower() == 'rmsprop':
optimizer = RMSprop(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
elif optimizer.lower() == 'nadam':
optimizer = Nadam(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
elif optimizer.lower() == 'adamax':
optimizer = Adamax(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
elif optimizer.lower() == 'adadelta':
optimizer = Adadelta(lr=lr, clipnorm=clipnorm, clipvalue=clipvalue, decay=decay, epsilon=epsilon)
else:
raise Exception('\tThe chosen optimizer is not implemented.')
if not self.silence:
logging.info("Compiling model...")
# compile differently depending if our model is 'Sequential', 'Model' or 'Graph'
if isinstance(self.model, Sequential) or isinstance(self.model, Model):
self.model.compile(optimizer=optimizer, metrics=metrics, loss=loss, loss_weights=loss_weights,
sample_weight_mode=sample_weight_mode)
else:
raise NotImplementedError()
if not self.silence:
logging.info("Optimizer updated, learning rate set to " + str(lr))