<commit_before># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
r"""Checkpoint converter for Canned Estimators in TF 1.x.

This checkpoint converter tool is mainly for Canned Estimators, including DNN
Linear and DNNLinearCombined estimators. The allowed optimizers to be converted
include Adam, Adagrad, Ftrl, RMSProp, and SGD.

Note that, this converter is not suitable for the case where 'dnn_optimizer'
and 'linear_optimizer' in DNNLinearCombined model are the same.

If your current canned estimators and checkpoints are from TF 1.x, after you
migrate the canned estimator to v2 with `tf.keras.optimizers.*`, the converted
checkpoint allow you to restore and retrain the model in TF 2.0.

Usage:
  python checkpoint_convert.py '/path/to/checkpoint' '/path/to/graph.pbtxt' \
      '/path/to/new_checkpoint'

For example, if there is a V1 checkpoint to be converted and the files include:
  /tmp/my_checkpoint/model.ckpt-100.data-00000-of-00001
  /tmp/my_checkpoint/model.ckpt-100.index
  /tmp/my_checkpoint/model.ckpt-100.meta
  /tmp/my_checkpoint/graph.pbtxt

use the following command:
  mkdir /tmp/my_converted_checkpoint &&
  python checkpoint_convert.py \
      /tmp/my_checkpoint/model.ckpt-100 /tmp/my_checkpoint/graph.pbtxt \
      /tmp/my_converted_checkpoint/model.ckpt-100

This will generate three converted checkpoint files corresponding to the three
old checkpoint files in the new directory:
  /tmp/my_converted_checkpoint/model.ckpt-100.data-00000-of-00001
  /tmp/my_converted_checkpoint/model.ckpt-100.index
  /tmp/my_converted_checkpoint/model.ckpt-100.meta
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import tensorflow as tf
from google.protobuf import text_format
from tensorflow.core.framework import graph_pb2
from tensorflow.python.keras.optimizer_v2 import adagrad
from tensorflow.python.keras.optimizer_v2 import adam
from tensorflow.python.keras.optimizer_v2 import ftrl
from tensorflow.python.keras.optimizer_v2 import gradient_descent
from tensorflow.python.keras.optimizer_v2 import rmsprop

# Optimizer name mapping from v1 to v2.
OPT_NAME_V1_TO_V2 = {
    'Adagrad': 'Adagrad',
    'RMSProp': 'RMSprop',
    'Ftrl': 'Ftrl',
    'Adam': 'Adam',
    'SGD': 'SGD',
}

# Hyper-paratmeters of optimizer in checkpoint.
HP_IN_CKPT = {
    'Adam': {
        'beta1_power': 'training/Adam/beta_1',
        'beta2_power': 'training/Adam/beta_2',
    },
}

# Optimzier variable name mapping from v1 to v2.
OPT_VAR_NAME_V1_TO_V2 = {
    'Adam': {
        'Adam': 'm',
        'Adam_1': 'v',
    },
    'Ftrl': {
        'Ftrl': 'accumulator',
        'Ftrl_1': 'linear',
    },
    'RMSProp': {
        'RMSProp': 'rms',
        'RMSProp_1': None,
    },
    'Adagrad': {
        'Adagrad': 'accumulator',
    },
}

# Hyper-paratmeters of optimizer in graph.
HP_IN_GRAPH = {
    'Adam': ['decay', 'learning_rate'],
    'Ftrl': [
        'decay', 'l1_regularization_strength', 'l2_regularization_strength',
        'learning_rate', 'learning_rate_power'
    ],
    'RMSProp': ['decay', 'learning_rate', 'momentum', 'rho'],
    'Adagrad': ['decay', 'learning_rate'],
    'SGD': ['decay', 'learning_rate', 'momentum'],
}

# optimizer v2 instance.
OPT_V2_INSTANCE = {
    'Adagrad': adagrad.Adagrad(),<commit_msg>Added beta parameter from FTRL paper to optimizer classes (such as the one in Keras).<commit_after># Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
r"""Checkpoint converter for Canned Estimators in TF 1.x.

This checkpoint converter tool is mainly for Canned Estimators, including DNN
Linear and DNNLinearCombined estimators. The allowed optimizers to be converted
include Adam, Adagrad, Ftrl, RMSProp, and SGD.

Note that, this converter is not suitable for the case where 'dnn_optimizer'
and 'linear_optimizer' in DNNLinearCombined model are the same.

If your current canned estimators and checkpoints are from TF 1.x, after you
migrate the canned estimator to v2 with `tf.keras.optimizers.*`, the converted
checkpoint allow you to restore and retrain the model in TF 2.0.

Usage:
  python checkpoint_convert.py '/path/to/checkpoint' '/path/to/graph.pbtxt' \
      '/path/to/new_checkpoint'

For example, if there is a V1 checkpoint to be converted and the files include:
  /tmp/my_checkpoint/model.ckpt-100.data-00000-of-00001
  /tmp/my_checkpoint/model.ckpt-100.index
  /tmp/my_checkpoint/model.ckpt-100.meta
  /tmp/my_checkpoint/graph.pbtxt

use the following command:
  mkdir /tmp/my_converted_checkpoint &&
  python checkpoint_convert.py \
      /tmp/my_checkpoint/model.ckpt-100 /tmp/my_checkpoint/graph.pbtxt \
      /tmp/my_converted_checkpoint/model.ckpt-100

This will generate three converted checkpoint files corresponding to the three
old checkpoint files in the new directory:
  /tmp/my_converted_checkpoint/model.ckpt-100.data-00000-of-00001
  /tmp/my_converted_checkpoint/model.ckpt-100.index
  /tmp/my_converted_checkpoint/model.ckpt-100.meta
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import sys
import tensorflow as tf
from google.protobuf import text_format
from tensorflow.core.framework import graph_pb2
from tensorflow.python.keras.optimizer_v2 import adagrad
from tensorflow.python.keras.optimizer_v2 import adam
from tensorflow.python.keras.optimizer_v2 import ftrl
from tensorflow.python.keras.optimizer_v2 import gradient_descent
from tensorflow.python.keras.optimizer_v2 import rmsprop

# Optimizer name mapping from v1 to v2.
OPT_NAME_V1_TO_V2 = {
    'Adagrad': 'Adagrad',
    'RMSProp': 'RMSprop',
    'Ftrl': 'Ftrl',
    'Adam': 'Adam',
    'SGD': 'SGD',
}

# Hyper-paratmeters of optimizer in checkpoint.
HP_IN_CKPT = {
    'Adam': {
        'beta1_power': 'training/Adam/beta_1',
        'beta2_power': 'training/Adam/beta_2',
    },
}

# Optimzier variable name mapping from v1 to v2.
OPT_VAR_NAME_V1_TO_V2 = {
    'Adam': {
        'Adam': 'm',
        'Adam_1': 'v',
    },
    'Ftrl': {
        'Ftrl': 'accumulator',
        'Ftrl_1': 'linear',
    },
    'RMSProp': {
        'RMSProp': 'rms',
        'RMSProp_1': None,
    },
    'Adagrad': {
        'Adagrad': 'accumulator',
    },
}

# Hyper-paratmeters of optimizer in graph.
HP_IN_GRAPH = {
    'Adam': ['decay', 'learning_rate'],
    'Ftrl': [
        'decay', 'l1_regularization_strength', 'l2_regularization_strength',
        'beta', 'learning_rate', 'learning_rate_power'
    ],
    'RMSProp': ['decay', 'learning_rate', 'momentum', 'rho'],
    'Adagrad': ['decay', 'learning_rate'],
    'SGD': ['decay', 'learning_rate', 'momentum'],
}

# optimizer v2 instance.
OPT_V2_INSTANCE = {
    'Adagrad': adagrad.Adagrad(),
{'fraction_non_alphanumeric': 0.12122723871289598, 'fraction_numerical': 0.02070341731105014, 'mean_word_length': 4.052930056710775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6988044', 'n_tokens_mistral': 2718, 'n_tokens_neox': 2557, 'n_words': 758}