{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "SB93Ge748VQs" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cellView": "form", "execution": { "iopub.execute_input": "2023-11-07T17:31:45.279921Z", "iopub.status.busy": "2023-11-07T17:31:45.279665Z", "iopub.status.idle": "2023-11-07T17:31:45.283699Z", "shell.execute_reply": "2023-11-07T17:31:45.283129Z" }, "id": "0sK8X2O9bTlz" }, "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": "HEYuO5NFwDK9" }, "source": [ "# 将 tf.summary 用法迁移到 TF 2.x\n", "\n", "
![]() | \n",
" ![]() | \n",
" ![]() | \n",
" ![]() | \n",
"
tf.compat.v1.summary
将在以下条件下自动转发到其 TF 2.x 等效项:\n",
"\n",
"- 最外层上下文为 Eager 模式\n",
"- 已设置默认 TF 2.x 摘要编写器\n",
"- 已为编写器设置非空步骤值(使用 tf.summary.SummaryWriter.as_default
、tf.summary.experimental.set_step
或 tf.compat.v1.train.create_global_step
)\n",
"\n",
"请注意,调用 TF 2.x 摘要实现时,返回值将为空字节串张量,以避免重复编写摘要。此外,输入参数转发是尽力而为的,并非所有参数都将被保留(例如,将支持 `family` 参数,而 `collections` 将被移除)。\n",
"\n",
"在 tf.compat.v1.summary.scalar
中调用 tf.summary.scalar
行为的示例:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2023-11-07T17:31:51.378506Z",
"iopub.status.busy": "2023-11-07T17:31:51.378221Z",
"iopub.status.idle": "2023-11-07T17:31:51.387955Z",
"shell.execute_reply": "2023-11-07T17:31:51.387337Z"
},
"id": "6457297c0b9d"
},
"outputs": [],
"source": [
"# Enable eager execution.\n",
"tf.compat.v1.enable_v2_behavior()\n",
"\n",
"# A default TF 2.x summary writer is available.\n",
"writer = tf.summary.create_file_writer(\"/tmp/mylogs/enable_v2_in_v1\")\n",
"# A step is set for the writer.\n",
"with writer.as_default(step=0):\n",
" # Below invokes `tf.summary.scalar`, and the return value is an empty bytestring.\n",
" tf.compat.v1.summary.scalar('float', tf.constant(1.0), family=\"family\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Pq4Fy1bSUdrZ"
},
"source": [
"### 完全迁移\n",
"\n",
"要完全迁移到 TF 2.x,您需要按如下方式调整您的代码:\n",
"\n",
"1. 必须存在通过 `.as_default()` 设置的默认写入器才能使用摘要运算\n",
"\n",
" - 这意味着在 Eager Execution 模式下执行运算或在计算图构造中使用运算\n",
" - 如果没有默认写入器,摘要运算将变为静默空运算\n",
" - 默认写入器(尚)不跨 `@tf.function` 执行边界传播(仅在跟踪函数时对其进行检测),所以最佳做法是在函数体中调用 `writer.as_default()`,并确保在使用 `@tf.function` 时,写入器对象始终存在\n",
"\n",
"2. 必须通过 `step` 参数将“步骤”值传入每个运算\n",
"\n",
" - TensorBoard 需要步骤值以将数据呈现为时间序列\n",
" - 由于 TF 1.x 中的全局步骤已被移除,因此需要执行显式传递,以确保每个运算都知道要读取的所需步骤变量\n",
" - 为了减少样板,对注册默认步骤值的实验性支持通过 `tf.summary.experimental.set_step()` 提供,但这是临时功能,如有更改,恕不另行通知\n",
"\n",
"3. 各个摘要运算的函数签名已更改\n",
"\n",
" - 现在,返回值为布尔值(指示是否实际写入了摘要)\n",
" - 第二个参数名称(如果使用)已从 `tensor` 更改为 `data`\n",
" - `collections` 参数已被移除;集合仅适用于 TF 1.x\n",
" - `family` 参数已被移除;仅使用 `tf.name_scope()`\n",
"\n",
"4. [仅针对旧计算图模式/会话执行用户]\n",
"\n",
" - 首先使用 `v1.Session.run(writer.init())` 初始化写入器\n",
"\n",
" - 使用 `v1.summary.all_v2_summary_ops()` 获取当前计算图的所有 TF 2.x 摘要运算,例如通过 `Session.run()` 执行它们\n",
"\n",
" - 使用 `v1.Session.run(writer.flush())` 刷新写入器,并以同样方式使用 `close()`\n",
"\n",
"如果您的 TF 1.x 代码已改用 `tf.contrib.summary` API,因其与 TF 2.x API 更加相似,`tf_upgrade_v2` 脚本将能够自动执行大多数迁移步骤(并针对无法完全迁移的任何用法发出警告或错误)。在大多数情况下,它只是将 API 调用重写为 `tf.compat.v2.summary`;如果只需要与 TF 2.x 兼容,那么您可以删除 `compat.v2` 并将其作为 `tf.summary` 引用。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1GUZRWSkW3ZC"
},
"source": [
"## 其他提示\n",
"\n",
"除上述重要内容以外,一些辅助方面也进行了更改:\n",
"\n",
"- 条件记录(例如“每 100 个步骤记录一次”)有所更新\n",
"\n",
" - 要控制运算和相关代码,请将其包装在常规 if 语句(可在 Eager 模式下运行,以及[通过 AutoGraph](https://tensorflow.google.cn/alpha/guide/autograph) 在 `@tf.function` 中使用)或 `tf.cond` 中\n",
" - 要仅控制摘要,请使用新的 `tf.summary.record_if()` 上下文管理器,并将其传递给您选择的布尔条件\n",
" - 以下内容替换了 TF 1.x 模式:\n",
" ```\n",
" if condition:\n",
" writer.add_summary()\n",
" ```\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9VMYrKn4Uh52"
},
"source": [
"- 不直接编写 `tf.compat.v1.Graph` - 改为使用跟踪函数\n",
"\n",
" - TF 2.x 中的计算图执行使用 `@tf.function`,而非显式计算图\n",
" - 在 TF 2.x 中,使用新的跟踪样式 API `tf.summary.trace_on()` 和 `tf.summary.trace_export()` 记录执行的函数计算图\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UGItA6U0UkDx"
},
"source": [
"- 不再使用 `tf.summary.FileWriterCache` 按 logdir 缓存全局写入器\n",
"\n",
" - 用户应实现自己的写入器对象缓存/共享方案,或者使用独立的写入器(TensorBoard [正在](https://github.com/tensorflow/tensorboard/issues/1063)实现对后者的支持)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d7BQJVcsUnMp"
},
"source": [
"- 事件文件的二进制表示已更改\n",
"\n",
" - TensorBoard 1.x 已支持新格式;此项变更仅对从事件文件手动解析摘要数据的用户存在影响\n",
" - 摘要数据现在以张量字节形式存储;您可以使用 `tf.make_ndarray(event.summary.value[0].tensor)` 将其转换为 Numpy"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "migrate.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.18"
}
},
"nbformat": 4,
"nbformat_minor": 0
}