gpt4 book ai didi

python - 如何初始化现有 Keras 模型的变量?

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:12 25 4
gpt4 key购买 nike

我想从头开始重新训练 Keras 模型 Inception_v3。

模型在这里定义: https://github.com/keras-team/keras-applications/blob/master/keras_applications/inception_v3.py

我看了一些帖子,

列出的解决方案是:

  1. 卡住层(这不是我想要的...)

    for layer in model.layers:
    layer.trainable = False
  2. https://stackoverflow.com/a/51727616/7748163

通过检查初始化器来重置所有层:

def reset_weights(model):
session = K.get_session()
for layer in model.layers:
if hasattr(layer, 'kernel_initializer'):
layer.kernel_initializer.run(session=session)
if hasattr(layer, 'bias_initializer'):
layer.bias_initializer.run(session=session)
  1. 使用tf.variables_initializer

      model = InceptionV3()
    for layer in model.layers:
    sess.run(tf.variables_initializer(layer.weights))

    引用:https://stackoverflow.com/a/56634827/7748163

  2. 我认为最好的一个,但会引发错误。

    sess = tf.Session()
    for layer in model.layers:
    for v in layer.__dict__:
    v_arg = getattr(layer,v)
    if hasattr(v_arg,'initializer'):
    initializer_method = getattr(v_arg, 'initializer')
    initializer_method.run(session=sess)
    print('reinitializing layer {}.{}'.format(layer.name, v))

但是,它们都不适用于 Inception_v3。

BatchNorm层的错误信息:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable batch_normalization_9/moving_mean from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/batch_normalization_9/moving_mean/N10tensorflow3VarE does not exist.
[[{{node batch_normalization_9_1/AssignMovingAvg/ReadVariableOp}}]]
[[metrics_1/categorical_accuracy/Identity/_469]]

那么,如何重新训练已有的Keras模型,并初始化变量呢?从 Keras 应用程序重新训练模型的最佳做法是什么?

进一步讨论:

https://github.com/keras-team/keras/issues/341

最佳答案

为什么不简单地不要求权重?

model = Inception_V3(..., weights=None,...)

https://github.com/keras-team/keras-applications/blob/master/keras_applications/inception_v3.py/#L100

关于python - 如何初始化现有 Keras 模型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com