【问题标题】:Tensoflow Estimator: how to use tf.graph_util.convert_variables_to_constantsTensorflow Estimator:如何使用 tf.graph_util.convert_variables_to_constants
【发布时间】:2018-07-18 08:32:09
【问题描述】:

我想知道是否可以在训练/评估循环中使用函数 tf.graph_util.convert_variables_to_constants(以存储图形的冻结版本),而我正在使用自定义估算器。例如:

best_validation_accuracy = -1
for _ in range(steps // how_often_validation):

    # Train the model
    estimator.train(input_fn=train_input_fn, steps=how_often_validation)

    # Evaluate the model
    validation_accuracy = estimator.evaluate(input_fn=eval_input_fn)

    # Save best model
    if validation_accuracy["accuracy"] > best_validation_accuracy:
        best_validation_accuracy = validation_accuracy["accuracy"]
        # Save best model perfomances
        # I WANT TO USE  tf.graph_util.convert_variables_to_constants HERE

【问题讨论】:

    标签: python tensorflow tensorflow-estimator


    【解决方案1】:

    要使用函数tf.graph_util.convert_variables_to_constants,您需要模型的图形和会话。

    经过TensorFlow code defining the estimators,看来:

    • 此代码已弃用,
    • 图表是动态创建的,不容易访问(至少,我无法检索它)。

    因此,我们将不得不使用旧方法。

    当您调用estimator.train 时,模型的检查点将保存在指定目录(estimator.model_dir)中。您可以使用这些文件来访问图形和会话并冻结变量,如下所示:

    1。加载元图

    saver = tf.train.import_meta_graph('/path/to/meta')
    

    2。负载重量

    sess = tf.Session
    saver.restore(sess, '/path/to/weights')
    

    3。冻结变量

    tf.graph_util.convert_variables_to_constants(sess,
                                                 sess.graph.as_graph_def(),
                                                 ['output'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-09
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多