简体   繁体   English

Keras:如何重置优化器状态?

[英]Keras: how to reset optimizer state?

How to reset optimizer state in keras?如何在 keras 中重置优化器状态?

Looking at Optimizer class I can't see such a method: https://github.com/keras-team/keras/blob/613aeff37a721450d94906df1a3f3cc51e2299d4/keras/optimizers.py#L60查看优化器类我看不到这样的方法: https : //github.com/keras-team/keras/blob/613aeff37a721450d94906df1a3f3cc51e2299d4/keras/optimizers.py#L60

Also what is actually self.updates and self.weights ?还有什么是self.updatesself.weights

There isn't an "easy" way to reset the "states", but you can always simply recompile your model with a new optimizer (model's weights are preserved):没有一种“简单”的方法来重置“状态”,但您总是可以简单地使用新的优化器重新编译您的模型(模型的权重被保留):

newOptimizer = Adadelta()
model.compile(optimizer=newOptimizer)     

You can also use the method set_weights(weightsListInNumpy) (not recommended), in the base class Optimizer , but this would be rather cumbersome as you would need to know all initial values and shapes, which sometimes may not be trivial zeroes .您还可以在基类Optimizer使用set_weights(weightsListInNumpy)方法(不推荐),但这会相当麻烦,因为您需要知道所有初始值和形状,有时可能不是简单的零。

Now, the property self.weights doesn't do much, but the functions that save and load optimizers will save and load this property.现在,属性self.weights没有做太多事情,但是保存和加载优化器的函数将保存和加载这个属性。 It's a list of tensors and should not be changed directly.这是张量列表,不应直接更改。 At most use K.set_value(...) in each entry of the list.最多在列表的每个条目中使用K.set_value(...) You can see the weights in saving the optimizer in the _serialize_model method .您可以在_serialize_model方法中查看保存优化器的weights

The self.updates are something a little more complex to understand. self.updates理解有点复杂。 It stores the variables that will be updated with every batch that is processed by the model in training.它存储将随模型在训练中处理的每个批次更新的变量。 But it's a symbolic graph variable.但它是一个符号图形变量。

The self.updates , as you can see in the code, is always appended with a K.update(var, value) or K.update_add(var, value) .self.updates ,你可以在代码中看到,总是与附加K.update(var, value)K.update_add(var, value) This is the correct way to tell the graph that these values should be updated every iteration.这是告诉图形应该在每次迭代中更新这些值的正确方法。

Usually, the updated vars are iterations , params (the model's weights), moments , accumulators , etc.通常,更新的变量是iterationsparams (模型的权重)、 momentsaccumulators等。

I don't think there is a universal method for this, but you should be able to reset the state of your optimizer by initializing the variables holding it.我不认为有一个通用的方法,但是您应该能够通过初始化保存优化器的变量来重置优化器的状态。 This would need to be done with the TensorFlow API, though.不过,这需要使用 TensorFlow API 来完成。 The state variables depend on the specific kind of optimizer.状态变量取决于特定类型的优化器。 For example, if you have a Adam optimizer ( source ), you could do the following:例如,如果您有一个Adam优化器 ( source ),您可以执行以下操作:

from keras.optimizers import Adam
from keras import backend as K

optimizer = Adam(...)
# These depend on the optimizer class
optimizer_state = [optimizer.iterations, optimizer.lr, optimizer.beta_1,
                   optimizer.beta_2, optimizer.decay]
optimizer_reset = tf.variables_initializer(optimizer_state)

# Later when you want to reset the optimizer
K.get_session().run(optimizer_reset)

The optimizer is just adjusting the wheihts of your model, thus the information is stored in the model, not in the optimizer.优化器只是调整模型的 wheihts,因此信息存储在模型中,而不是优化器中。

That means you can't reset an optimizer in a way you might think.这意味着您无法以您可能认为的方式重置优化器。 You need to reset (or maybe easyier, recreate) your model.您需要重置(或者更简单,重新创建)您的模型。

That means you also can optimize your model with an optimizer A, stop after some epochs, and continue optimizing your model with optimizer B not loosing the progress optimizer A made allready.这意味着您还可以使用优化器 A 优化您的模型,在一些时期后停止,并继续使用优化器 B 优化您的模型,而不会丢失优化器 A 已经做好的进度。

I don't know exactly what self.updates and self.weights are there for.我不知道self.updatesself.weights到底是self.updates用的。 But because those are internal variables of the class someone needs to know/read about the optimizer class itself and understand its code.但是因为这些是类的内部变量,所以有人需要了解/阅读优化器类本身并理解其代码。 Here we need to wait fore someone who dived deeper into the sourcecode of keras.在这里,我们需要等待深入研究 keras 源代码的人。

EDIT编辑

You can just recreate your optimizer for example:您可以重新创建优化器,例如:

model = Seqeuential()
...
...
...

model.compile(optimizer=keras.optimizers.Adadelta(lr = 5, loss='mean_squared_error')
model.fit(X, y, epochs=10)

model.compile(optimizer=keras.optimizers.Adadelta(lr = 0.5, loss='mean_squared_error')
model.fit(X, y, epochs=10)

With the above code you train 10 epochs with learning rate 5, compile your model with a new optimizer, and continue for another 10 epochs with learning rate 0.5.使用上面的代码,您以学习率 5 训练 10 个时期,使用新的优化器编译您的模型,然后继续以学习率 0.5 进行另外 10 个时期。 The weights which you could also call your training progress do not get lost if you compile your model again.如果您再次编译模型,您也可以称之为训练进度的权重不会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM