Chapter II: New TensorFlow entry, use checkpoint to save the model __ new TensorFlow

Source: Internet
Author: User
1. Overview

As with the old version of TensorFlow, the model needs to be saved, and this preservation is cyclical. Because in many cases the gradient will swing around the local minimum, that is to say, in many cases, the last training model is not necessarily optimal.

2. Save the Model

We can create a location where the checkpoint is saved when we build the model, and we can start by creating a folder with the following command.


You can add parameters when building a model

Classifier = Tf.estimator.DNNClassifier (
    feature_columns=my_feature_columns,
    hidden_units=[10, ten),
    n _classes=3,
#   Specifies the model (Checkpoint storage location)  
    model_dir= './checkpoint ')

After training the model, you can see the following file in the appropriate folder:


By default, only the first and last models are saved, and the event files are recorded in the folder, in addition to saving the model in a modified folder. This is somewhat different from the old version of TensorFlow.

Checkpoint: The version of the model that was created during the training. Event File: Contains information that Tensorboard uses to create a visual chart. 3, configuration model save parameters

By default, Estimator saves checkpoints to model_dir in accordance with the following schedule: Write a checkpoint every 10 minutes (600 seconds). Writes a checkpoint when the train method starts (the first iteration) and completes (the last iteration). Only 5 recently written checkpoints are kept in the directory.

You can customize the configuration file to modify the way checkpoints are saved.

Model_save_config = Tf.estimator.RunConfig (save_checkpoints_steps=500, # Make every step to save a model
                                           keep_checkpoint_max=6) # Specifies the maximum number of models
to save classifier = Tf.estimator.DNNClassifier (
    feature_columns=my_feature_columns,
    hidden_ UNITS=[10,
    n_classes=3,
#   Specify model (Checkpoint storage location)  
    model_dir= './checkpoint ',
    config=model_save_ Config

After the modifications, the model is saved as shown in the following illustration:


4. Recovery Model

The first time the estimator train method is invoked, TensorFlow saves a checkpoint to Model_dir. Each time you call the estimator train, eval, or Predict method, the following occurs: Estimator builds the model diagram by running MODEL_FN (). (For more information on MODEL_FN (), see Creating a Custom Estimator.) Estimator initializes the weights of the new model based on the data stored in the most recently written checkpoint.

In other words, as the following illustration shows, once a checkpoint is present, TensorFlow rebuilds the model each time you call train (), evaluate (), or predict ().


Note: If you change the model structure and then train again after training, the contents of the original checkpoint are incompatible with the system error



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.