【问题标题】:Transfer learning/ retraining with TensorFlow Estimators使用 TensorFlow Estimators 进行迁移学习/再训练
【发布时间】:2018-04-12 23:53:20
【问题描述】:

我一直无法弄清楚如何通过新的 TF Estimator API 使用迁移学习/最后一层再训练。

Estimator 需要一个model_fn,其中包含documentation 中定义的网络架构、训练和评估操作。 model_fn 使用 CNN 架构的示例是 here

如果我想重新训练最后一层,例如 inception 架构,我不确定是否需要在这个model_fn 中指定整个模型,然后加载预训练的权重,或者是否需要有一种方法可以像“传统”方法一样使用保存的图形(例如 here)。

这已作为issue 提出,但仍处于开放状态,我不清楚答案。

【问题讨论】:

    标签: python tensorflow pre-trained-model


    【解决方案1】:

    可以在模型定义期间加载元图,并使用 SessionRunHook 从 ckpt 文件加载权重。

    def model(features, labels, mode, params):
        # Create the graph here
    
        return tf.estimator.EstimatorSpec(mode, 
                predictions,
                loss,
                train_op,
                training_hooks=[RestoreHook()])
    

    SessionRunHook 可以是:

    class RestoreHook(tf.train.SessionRunHook):
    
        def after_create_session(self, session, coord=None):
            if session.run(tf.train.get_or_create_global_step()) == 0:
                # load weights here
    

    这样,权重在第一步加载,并在训练期间保存在模型检查点中。

    【讨论】:

    • 我认为通过这种方式你可以恢复权重并使用它们而不是随机初始化,但是冻结一些层以保持它们的权重呢?
    猜你喜欢
    • 2018-11-27
    • 2022-09-27
    • 2020-08-06
    • 2016-05-18
    • 2021-04-17
    • 1970-01-01
    • 2018-02-10
    • 2019-12-07
    • 2019-03-08
    相关资源
    最近更新 更多