<jupyter_start><jupyter_text>Copyright 2021 The TensorFlow Authors.<jupyter_code>#@title 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
#
# https://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.<jupyter_output><empty_output><jupyter_text>Migration Samples: TF1 vs TF2              View on TensorFlow.org                Run in Google Colab                View source on GitHub        Download notebook   TensorFlow team has prepared code samples that demonstrate the equivalence between TF1 and TF2, with a focus on the high-level training API elements. We hope this lets you identify the similarities between your existing TF1 workflow and the available samples, and find a concrete path to move to TF2. This is work in progress and more samples are being added.  SetupLet's start with a couple of necessary TensorFlow imports,<jupyter_code>import tensorflow as tf
import tensorflow.compat.v1 as tf1<jupyter_output><empty_output><jupyter_text>and prepare some simple data for demonstration:<jupyter_code>features = [[1., 1.5], [2., 2.5], [3., 3.5]]
labels = [[0.3], [0.5], [0.7]]
eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]]
eval_labels = [[0.8], [0.9], [1.]]<jupyter_output><empty_output><jupyter_text>Sample 1: Training and evaluation with a trivial dense layer.  TF1: Estimator.train/evaluate<jupyter_code>def _input_fn():
  return tf1.data.Dataset.from_tensor_slices((features, labels)).batch(1)

def _eval_input_fn():
  return tf1.data.Dataset.from_tensor_slices(
      (eval_features, eval_labels)).batch(1)

def _model_fn(features, labels, mode):
  logits = tf1.layers.Dense(1)(features)
  loss = tf1.losses.mean_squared_error(labels=labels, predictions=logits)
  optimizer = tf1.train.AdagradOptimizer(0.05)
  train_op = optimizer.minimize(loss, global_step=tf1.train.get_global_step())
  return tf1.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

estimator = tf1.estimator.Estimator(model_fn=_model_fn)
estimator.train(_input_fn)
estimator.evaluate(_eval_input_fn)<jupyter_output><empty_output><jupyter_text>TF2: Keras training API<jupyter_code>dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1)
eval_dataset = tf.data.Dataset.from_tensor_slices(
      (eval_features, eval_labels)).batch(1)

model = tf.keras.models.Sequential([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05)

model.compile(optimizer, "mse")
model.fit(dataset)
model.evaluate(eval_dataset, return_dict=True)<jupyter_output><empty_output><jupyter_text>TF2: Keras training API with Custom Training Step Keras allows you to provide customized training step function for your model's forward and backward passes, and at the same time takes advantage of the built-in training support such as callbacks, distribution with `tf.distribute`, etc.<jupyter_code>class CustomModel(tf.keras.Sequential):
  """A custom sequential model that has train_step overridden."""

  def train_step(self, data):
    batch_data, labels = data

    with tf.GradientTape() as tape:
      predictions = self(batch_data, training=True)
      # Compute the loss value (loss function is configured in `compile()`)
      loss = self.compiled_loss(labels, predictions)

    # Compute gradients
    gradients = tape.gradient(loss, self.trainable_variables)
    # Update weights
    self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))
    # Update metrics (includes the metric that tracks the loss)
    self.compiled_metrics.update_state(labels, predictions)
    # Return a dict mapping metric names to current value
    return {m.name: m.result() for m in self.metrics}

dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1)
eval_dataset = tf.data.Dataset.from_tensor_slices(
      (eval_features, eval_labels)).batch(1)

model = CustomModel([tf.keras.layers.Dense(1)])
optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05)

model.compile(optimizer, "mse")
model.fit(dataset)
model.evaluate(eval_dataset, return_dict=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10213243546576879, 'fraction_numerical': 0.016835016835016835, 'mean_word_length': 5.276056338028169, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '114169', 'license': 'Apache-2.0', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1382, 'n_words': 393, 'path': 'site/en/guide/migration_samples.ipynb', 'repo_name': 'diyue-ye/docs', 'size': '4455'}