{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "wJcYs_ERTnnI" }, "source": [ "##### Copyright 2021 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2022-12-14T20:31:20.211138Z", "iopub.status.busy": "2022-12-14T20:31:20.210812Z", "iopub.status.idle": "2022-12-14T20:31:20.214468Z", "shell.execute_reply": "2022-12-14T20:31:20.213921Z" }, "id": "HMUDt0CiUJk9" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "id": "77z2OchJTk0l" }, "source": [ "# 从 Estimator 迁移到 Keras API\n", "\n", "\n", " \n", " \n", " \n", " \n", "
在 TensorFlow.org 上查看 在 Google Colab 运行\n", " 在 Github 上查看源代码\n", " 下载笔记本
" ] }, { "cell_type": "markdown", "metadata": { "id": "meUTrR4I6m1C" }, "source": [ "本指南演示了如何从 TensorFlow 1 的 `tf.estimator.Estimator` API 迁移到 TensorFlow 2 的 `tf.keras` API。首先,您将使用 `tf.estimator.Estimator` 设置并运行一个用于训练和评估的基本模型。然后,您将使用 `tf.keras` API 在 TensorFlow 2 中执行对应步骤。此外,您还将了解如何通过子类化 `tf.keras.Model` 和使用 `tf.GradientTape` 来自定义训练步骤。\n", "\n", "- 在 TensorFlow 1 中,可以使用高级 `tf.estimator.Estimator` API 训练和评估模型,以及执行推断和保存模型(用于提供)。\n", "- 在 TensorFlow 2 中,使用 Keras API 执行上述任务,例如[模型构建](https://tensorflow.google.cn/guide/keras/custom_layers_and_models)、梯度应用、[训练](https://tensorflow.google.cn/guide/keras/customizing_what_happens_in_fit)、评估和预测。\n", "\n", "(要将模型/检查点保存工作流迁移到 TensorFlow 2,请查看 [SavedModel](saved_model.ipynb) 和[检查点](checkpoint_saved.ipynb)迁移指南。)" ] }, { "cell_type": "markdown", "metadata": { "id": "YdZSoIXEbhg-" }, "source": [ "## 安装\n", "\n", "从导入和一个简单的数据集开始:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:20.217941Z", "iopub.status.busy": "2022-12-14T20:31:20.217524Z", "iopub.status.idle": "2022-12-14T20:31:22.102778Z", "shell.execute_reply": "2022-12-14T20:31:22.102075Z" }, "id": "iE0vSfMXumKI" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2022-12-14 20:31:21.142310: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory\n", "2022-12-14 20:31:21.142404: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory\n", "2022-12-14 20:31:21.142413: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.\n" ] } ], "source": [ "import tensorflow as tf\n", "import tensorflow.compat.v1 as tf1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:22.107285Z", "iopub.status.busy": "2022-12-14T20:31:22.106588Z", "iopub.status.idle": "2022-12-14T20:31:22.110919Z", "shell.execute_reply": "2022-12-14T20:31:22.110198Z" }, "id": "m7rnGxsXtDkV" }, "outputs": [], "source": [ "features = [[1., 1.5], [2., 2.5], [3., 3.5]]\n", "labels = [[0.3], [0.5], [0.7]]\n", "eval_features = [[4., 4.5], [5., 5.5], [6., 6.5]]\n", "eval_labels = [[0.8], [0.9], [1.]]" ] }, { "cell_type": "markdown", "metadata": { "id": "4uXff1BEssdE" }, "source": [ "## TensorFlow 1:使用 tf.estimator.Estimator 进行训练和评估\n", "\n", "此示例展示了如何在 TensorFlow 1 中使用 `tf.estimator.Estimator` 执行训练和评估。\n", "\n", "首先定义几个函数:训练数据的输入函数,评估数据的评估输入函数,以及告知 `Estimator` 如何使用特征和标签定义训练运算的模型函数:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:22.114243Z", "iopub.status.busy": "2022-12-14T20:31:22.113812Z", "iopub.status.idle": "2022-12-14T20:31:22.118569Z", "shell.execute_reply": "2022-12-14T20:31:22.118052Z" }, "id": "lqe9obf7suIj" }, "outputs": [], "source": [ "def _input_fn():\n", " return tf1.data.Dataset.from_tensor_slices((features, labels)).batch(1)\n", "\n", "def _eval_input_fn():\n", " return tf1.data.Dataset.from_tensor_slices(\n", " (eval_features, eval_labels)).batch(1)\n", "\n", "def _model_fn(features, labels, mode):\n", " logits = tf1.layers.Dense(1)(features)\n", " loss = tf1.losses.mean_squared_error(labels=labels, predictions=logits)\n", " optimizer = tf1.train.AdagradOptimizer(0.05)\n", " train_op = optimizer.minimize(loss, global_step=tf1.train.get_global_step())\n", " return tf1.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)" ] }, { "cell_type": "markdown", "metadata": { "id": "44bf417bf9c0" }, "source": [ "实例化您的 `Estimator`,并训练模型:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:22.121746Z", "iopub.status.busy": "2022-12-14T20:31:22.121356Z", "iopub.status.idle": "2022-12-14T20:31:26.505113Z", "shell.execute_reply": "2022-12-14T20:31:26.504436Z" }, "id": "922720812527" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Using default config.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:Using temporary folder as model directory: /tmpfs/tmp/tmpz5dhkiz0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Using config: {'_model_dir': '/tmpfs/tmp/tmpz5dhkiz0', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true\n", "graph_options {\n", " rewrite_options {\n", " meta_optimizer_iterations: ONE\n", " }\n", "}\n", ", '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/training/training_util.py:396: Variable.initialized_value (from tensorflow.python.ops.variables) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling model_fn.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/training/adagrad.py:138: calling Constant.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "Call initializer instance with the dtype argument instead of passing it to the constructor\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Done calling model_fn.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Create CheckpointSaverHook.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Graph was finalized.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Running local_init_op.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Done running local_init_op.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 0...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Saving checkpoints for 0 into /tmpfs/tmp/tmpz5dhkiz0/model.ckpt.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 0...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:loss = 0.25646034, step = 0\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling checkpoint listeners before saving checkpoint 3...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Saving checkpoints for 3 into /tmpfs/tmp/tmpz5dhkiz0/model.ckpt.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling checkpoint listeners after saving checkpoint 3...\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Loss for final step: 0.012408661.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "estimator = tf1.estimator.Estimator(model_fn=_model_fn)\n", "estimator.train(_input_fn)" ] }, { "cell_type": "markdown", "metadata": { "id": "17c9933c2d89" }, "source": [ "使用评估集评估程序:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:26.508669Z", "iopub.status.busy": "2022-12-14T20:31:26.508134Z", "iopub.status.idle": "2022-12-14T20:31:26.895290Z", "shell.execute_reply": "2022-12-14T20:31:26.894279Z" }, "id": "HsOpjW5plH9Q" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Calling model_fn.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Done calling model_fn.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Starting evaluation at 2022-12-14T20:31:26\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Graph was finalized.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Restoring parameters from /tmpfs/tmp/tmpz5dhkiz0/model.ckpt-3\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Running local_init_op.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Done running local_init_op.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Inference Time : 0.25017s\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Finished evaluation at 2022-12-14-20:31:26\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Saving dict for global step 3: global_step = 3, loss = 0.23348819\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "INFO:tensorflow:Saving 'checkpoint_path' summary for global step 3: /tmpfs/tmp/tmpz5dhkiz0/model.ckpt-3\n" ] }, { "data": { "text/plain": [ "{'loss': 0.23348819, 'global_step': 3}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "estimator.evaluate(_eval_input_fn)" ] }, { "cell_type": "markdown", "metadata": { "id": "KEmzBjfnsxwT" }, "source": [ "## TensorFlow 2:使用内置 Keras 方法进行训练和评估\n", "\n", "此示例演示了如何在 TensorFlow 2 中使用 `Model.fit` 和 `Model.evaluate` 执行训练和评估。(可以在[使用内置方法进行训练和评估](https://tensorflow.google.cn/guide/keras/train_and_evaluate)指南中了解详情。)\n", "\n", "- 首先使用 `tf.data.Dataset` API 准备数据集流水线。\n", "- 使用一个线性 (`tf.keras.layers.Dense`) 层定义一个简单的 Keras [序贯](https://tensorflow.google.cn/guide/keras/sequential_model)模型。\n", "- 实例化一个 Adagrad 优化器 (`tf.keras.optimizers.Adagrad`)。\n", "- 通过将 `optimizer` 变量和均方差(`\"mse\"`)损失传递给 `Model.compile` 来配置模型进行训练。" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:26.898862Z", "iopub.status.busy": "2022-12-14T20:31:26.898369Z", "iopub.status.idle": "2022-12-14T20:31:26.930208Z", "shell.execute_reply": "2022-12-14T20:31:26.929226Z" }, "id": "atVciNgPs0fw" }, "outputs": [], "source": [ "dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1)\n", "eval_dataset = tf.data.Dataset.from_tensor_slices(\n", " (eval_features, eval_labels)).batch(1)\n", "\n", "model = tf.keras.models.Sequential([tf.keras.layers.Dense(1)])\n", "optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05)\n", "\n", "model.compile(optimizer=optimizer, loss=\"mse\")" ] }, { "cell_type": "markdown", "metadata": { "id": "ed17a6291959" }, "source": [ "这样,您就可以通过调用 `Model.fit` 来训练模型了:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:26.933953Z", "iopub.status.busy": "2022-12-14T20:31:26.933336Z", "iopub.status.idle": "2022-12-14T20:31:27.455998Z", "shell.execute_reply": "2022-12-14T20:31:27.455240Z" }, "id": "a0b732534501" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r", "1/3 [=========>....................] - ETA: 0s - loss: 9.7873" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r", "3/3 [==============================] - 0s 5ms/step - loss: 31.5912\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(dataset)" ] }, { "cell_type": "markdown", "metadata": { "id": "74767288a2ea" }, "source": [ "最后,使用 `Model.evaluate` 评估模型:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:27.459438Z", "iopub.status.busy": "2022-12-14T20:31:27.458892Z", "iopub.status.idle": "2022-12-14T20:31:27.554733Z", "shell.execute_reply": "2022-12-14T20:31:27.554094Z" }, "id": "Kip65sYBlKiu" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r", "1/3 [=========>....................] - ETA: 0s - loss: 86.5976" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r", "3/3 [==============================] - 0s 2ms/step - loss: 134.4299\n" ] }, { "data": { "text/plain": [ "{'loss': 134.429931640625}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.evaluate(eval_dataset, return_dict=True)" ] }, { "cell_type": "markdown", "metadata": { "id": "BuVYN0CHs5sD" }, "source": [ "## TensorFlow 2:使用自定义训练步骤和内置 Keras 方法进行训练和评估" ] }, { "cell_type": "markdown", "metadata": { "id": "gHx_RUL8xcJ3" }, "source": [ "在 TensorFlow 2 中,还可以使用 `tf.GradientTape` 编写自己的自定义训练步骤函数来执行前向和后向传递,同时仍然利用内置的训练支持,例如 `tf.keras.callbacks.Callback` 和 `tf.distribute.Strategy`。(在[自定义 Model.fit 的功能](https://tensorflow.google.cn/guide/keras/customizing_what_happens_in_fit)和[从头开始编写自定义训练循环](https://tensorflow.google.cn/guide/keras/writing_a_training_loop_from_scratch)中了解详情。)\n", "\n", "在此示例中,首先通过子类化重写 `Model.train_step` 的 `tf.keras.Sequential` 来创建自定义 `tf.keras.Model`。(详细了解如何[子类化 tf.keras.Model](https://tensorflow.google.cn/guide/keras/custom_layers_and_models))。在该类中,定义一个自定义 `train_step` 函数,此函数在一个训练步骤中为每批次数据执行前向传递和后向传递。\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:27.558321Z", "iopub.status.busy": "2022-12-14T20:31:27.557846Z", "iopub.status.idle": "2022-12-14T20:31:27.563796Z", "shell.execute_reply": "2022-12-14T20:31:27.563095Z" }, "id": "rSz_y0zOs8h2" }, "outputs": [], "source": [ "class CustomModel(tf.keras.Sequential):\n", " \"\"\"A custom sequential model that overrides `Model.train_step`.\"\"\"\n", "\n", " def train_step(self, data):\n", " batch_data, labels = data\n", "\n", " with tf.GradientTape() as tape:\n", " predictions = self(batch_data, training=True)\n", " # Compute the loss value (the loss function is configured\n", " # in `Model.compile`).\n", " loss = self.compiled_loss(labels, predictions)\n", "\n", " # Compute the gradients of the parameters with respect to the loss.\n", " gradients = tape.gradient(loss, self.trainable_variables)\n", " # Perform gradient descent by updating the weights/parameters.\n", " self.optimizer.apply_gradients(zip(gradients, self.trainable_variables))\n", " # Update the metrics (includes the metric that tracks the loss).\n", " self.compiled_metrics.update_state(labels, predictions)\n", " # Return a dict mapping metric names to the current values.\n", " return {m.name: m.result() for m in self.metrics}" ] }, { "cell_type": "markdown", "metadata": { "id": "ee7c4f94d69b" }, "source": [ "接下来,和之前一样:\n", "\n", "- 使用 `tf.data.Dataset` 准备数据集流水线。\n", "- 使用一个 `tf.keras.layers.Dense` 层定义一个简单的模型。\n", "- 实例化 Adagrad (`tf.keras.optimizers.Adagrad`)\n", "- 使用 `Model.compile` 配置用于训练的模型,同时使用均方差(`\"mse\"`)作为损失函数。" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:27.567123Z", "iopub.status.busy": "2022-12-14T20:31:27.566564Z", "iopub.status.idle": "2022-12-14T20:31:27.581057Z", "shell.execute_reply": "2022-12-14T20:31:27.580471Z" }, "id": "01fcc2b1292c" }, "outputs": [], "source": [ "dataset = tf.data.Dataset.from_tensor_slices((features, labels)).batch(1)\n", "eval_dataset = tf.data.Dataset.from_tensor_slices(\n", " (eval_features, eval_labels)).batch(1)\n", "\n", "model = CustomModel([tf.keras.layers.Dense(1)])\n", "optimizer = tf.keras.optimizers.Adagrad(learning_rate=0.05)\n", "\n", "model.compile(optimizer=optimizer, loss=\"mse\")" ] }, { "cell_type": "markdown", "metadata": { "id": "844543802ff5" }, "source": [ "调用 `Model.fit` 以训练模型:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:27.584363Z", "iopub.status.busy": "2022-12-14T20:31:27.583880Z", "iopub.status.idle": "2022-12-14T20:31:27.943096Z", "shell.execute_reply": "2022-12-14T20:31:27.942464Z" }, "id": "211be3620765" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r", "1/3 [=========>....................] - ETA: 0s - loss: 0.0147" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r", "3/3 [==============================] - 0s 3ms/step - loss: 0.1047\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(dataset)" ] }, { "cell_type": "markdown", "metadata": { "id": "c93b9d6fc9d7" }, "source": [ "最后,使用 `Model.evaluate` 评估程序:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2022-12-14T20:31:27.946683Z", "iopub.status.busy": "2022-12-14T20:31:27.946156Z", "iopub.status.idle": "2022-12-14T20:31:28.022147Z", "shell.execute_reply": "2022-12-14T20:31:28.021406Z" }, "id": "nYO2wI1SlNCG" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r", "1/3 [=========>....................] - ETA: 0s - loss: 0.1555" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\r", "3/3 [==============================] - 0s 3ms/step - loss: 0.5426\n" ] }, { "data": { "text/plain": [ "{'loss': 0.5425885319709778}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.evaluate(eval_dataset, return_dict=True)" ] }, { "cell_type": "markdown", "metadata": { "id": "e9b5c9a4747b" }, "source": [ "## 后续步骤\n", "\n", "您可能会发现有用的其他 Keras 资源:\n", "\n", "- 指南:[使用内置方法进行训练和评估](https://tensorflow.google.cn/guide/keras/train_and_evaluate)\n", "- 指南:[自定义 Model.fit 的功能](https://tensorflow.google.cn/guide/keras/customizing_what_happens_in_fit)\n", "- 指南:[从头开始编写训练循环](https://tensorflow.google.cn/guide/keras/writing_a_training_loop_from_scratch)\n", "- 指南:[通过子类化创建新的 Keras 层和模型](https://tensorflow.google.cn/guide/keras/custom_layers_and_models)\n", "\n", "以下指南有助于从 `tf.estimator` API 迁移分布策略工作流:\n", "\n", "- [从 TPUEstimator 迁移到 TPUStrategy](tpu_estimator.ipynb)\n", "- [迁移单工作进程多 GPU 训练](mirrored_strategy.ipynb)\n", "- [迁移多工作进程 CPU/GPU 训练](multi_worker_cpu_gpu_training.ipynb)" ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "migrating_estimator.ipynb", "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 0 }