版权所有 2023 The TF-Agents Authors.
在 TensorFlow.org 上查看 | 在 Google Colab 中运行 | 在 GitHub 上查看源代码 | 下载笔记本 |
简介
tf_agents.utils.common.Checkpointer
是一个实用程序,用于将训练状态、策略状态和回放缓冲区状态保存到/从本地存储加载。
tf_agents.policies.policy_saver.PolicySaver
是一个工具,仅用于保存/加载策略,它比 Checkpointer
更轻量级。您可以使用 PolicySaver
部署模型,而无需了解创建策略的代码。
在本教程中,我们将使用 DQN 训练模型,然后使用 Checkpointer
和 PolicySaver
来展示如何以交互方式存储和加载状态和模型。请注意,我们将使用 TF2.0 的新 saved_model 工具和格式来使用 PolicySaver
。
设置
如果您尚未安装以下依赖项,请运行
sudo apt-get update
sudo apt-get install -y xvfb ffmpeg python-opengl
pip install pyglet
pip install 'imageio==2.4.0'
pip install 'xvfbwrapper==0.2.9'
pip install tf-agents[reverb]
pip install tf-keras
import os
# Keep using keras-2 (tf-keras) rather than keras-3 (keras).
os.environ['TF_USE_LEGACY_KERAS'] = '1'
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import base64
import imageio
import io
import matplotlib
import matplotlib.pyplot as plt
import os
import shutil
import tempfile
import tensorflow as tf
import zipfile
import IPython
try:
from google.colab import files
except ImportError:
files = None
from tf_agents.agents.dqn import dqn_agent
from tf_agents.drivers import dynamic_step_driver
from tf_agents.environments import suite_gym
from tf_agents.environments import tf_py_environment
from tf_agents.eval import metric_utils
from tf_agents.metrics import tf_metrics
from tf_agents.networks import q_network
from tf_agents.policies import policy_saver
from tf_agents.policies import py_tf_eager_policy
from tf_agents.policies import random_tf_policy
from tf_agents.replay_buffers import tf_uniform_replay_buffer
from tf_agents.trajectories import trajectory
from tf_agents.utils import common
tempdir = os.getenv("TEST_TMPDIR", tempfile.gettempdir())
2023-12-22 12:17:45.769390: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered 2023-12-22 12:17:45.769434: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered 2023-12-22 12:17:45.771084: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
# Set up a virtual display for rendering OpenAI gym environments.
import xvfbwrapper
xvfbwrapper.Xvfb(1400, 900, 24).start()
DQN 代理
我们将设置 DQN 代理,就像在之前的 colab 中一样。默认情况下,详细信息被隐藏,因为它们不是此 colab 的核心部分,但您可以点击“显示代码”以查看详细信息。
超参数
env_name = "CartPole-v1"
collect_steps_per_iteration = 100
replay_buffer_capacity = 100000
fc_layer_params = (100,)
batch_size = 64
learning_rate = 1e-3
log_interval = 5
num_eval_episodes = 10
eval_interval = 1000
环境
train_py_env = suite_gym.load(env_name)
eval_py_env = suite_gym.load(env_name)
train_env = tf_py_environment.TFPyEnvironment(train_py_env)
eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)
代理
数据收集
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/autograph/impl/api.py:377: ReplayBuffer.get_next (from tf_agents.replay_buffers.replay_buffer) is deprecated and will be removed in a future version. Instructions for updating: Use `as_dataset(..., single_deterministic_pass=False) instead.
训练代理
视频生成
生成视频
通过生成视频来检查策略的性能。
print ('global_step:')
print (global_step)
run_episodes_and_create_video(agent.policy, eval_env, eval_py_env)
global_step: <tf.Variable 'global_step:0' shape=() dtype=int64, numpy=0>
设置检查点和策略保存器
现在我们准备使用检查点和策略保存器。
检查点
checkpoint_dir = os.path.join(tempdir, 'checkpoint')
train_checkpointer = common.Checkpointer(
ckpt_dir=checkpoint_dir,
max_to_keep=1,
agent=agent,
policy=agent.policy,
replay_buffer=replay_buffer,
global_step=global_step
)
策略保存器
policy_dir = os.path.join(tempdir, 'policy')
tf_policy_saver = policy_saver.PolicySaver(agent.policy)
训练一次迭代
print('Training one iteration....')
train_one_iteration()
Training one iteration.... WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py:1260: calling foldr_v2 (from tensorflow.python.ops.functional_ops) with back_prop=False is deprecated and will be removed in a future version. Instructions for updating: back_prop=False is deprecated. Consider using tf.stop_gradient instead. Instead of: results = tf.foldr(fn, elems, back_prop=False) Use: results = tf.nest.map_structure(tf.stop_gradient, tf.foldr(fn, elems)) WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py:1260: calling foldr_v2 (from tensorflow.python.ops.functional_ops) with back_prop=False is deprecated and will be removed in a future version. Instructions for updating: back_prop=False is deprecated. Consider using tf.stop_gradient instead. Instead of: results = tf.foldr(fn, elems, back_prop=False) Use: results = tf.nest.map_structure(tf.stop_gradient, tf.foldr(fn, elems)) iteration: 1 loss: 0.936349093914032
保存到检查点
train_checkpointer.save(global_step)
恢复检查点
为了使此操作正常工作,应以与创建检查点时相同的方式重新创建整套对象。
train_checkpointer.initialize_or_restore()
global_step = tf.compat.v1.train.get_global_step()
还保存策略并将其导出到某个位置
tf_policy_saver.save(policy_dir)
WARNING:absl:`0/step_type` is not a valid tf.function parameter name. Sanitizing to `arg_0_step_type`. WARNING:absl:`0/reward` is not a valid tf.function parameter name. Sanitizing to `arg_0_reward`. WARNING:absl:`0/discount` is not a valid tf.function parameter name. Sanitizing to `arg_0_discount`. WARNING:absl:`0/observation` is not a valid tf.function parameter name. Sanitizing to `arg_0_observation`. WARNING:absl:`0/step_type` is not a valid tf.function parameter name. Sanitizing to `arg_0_step_type`. INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/saved_model/nested_structure_coder.py:458: UserWarning: Encoding a StructuredValue with type tfp.distributions.Deterministic_ACTTypeSpec; loading this StructuredValue will require that this type be imported and registered. warnings.warn("Encoding a StructuredValue with type %s; loading this " INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
策略可以在没有任何关于用于创建它的代理或网络的知识的情况下加载。这使得策略的部署变得更加容易。
加载保存的策略并检查其性能
saved_policy = tf.saved_model.load(policy_dir)
run_episodes_and_create_video(saved_policy, eval_env, eval_py_env)
导出和导入
colab 的其余部分将帮助您导出/导入检查点和策略目录,以便您可以在以后继续训练并在无需再次训练的情况下部署模型。
现在,您可以返回到“训练一次迭代”并进行多次训练,以便您稍后了解差异。当您开始看到略微更好的结果时,请继续执行以下操作。
创建 zip 文件并上传 zip 文件(双击查看代码)
从检查点目录创建压缩文件。
train_checkpointer.save(global_step)
checkpoint_zip_filename = create_zip_file(checkpoint_dir, os.path.join(tempdir, 'exported_cp'))
下载 zip 文件。
if files is not None:
files.download(checkpoint_zip_filename) # try again if this fails: https://github.com/googlecolab/colabtools/issues/469
训练一段时间(10-15 次)后,下载检查点 zip 文件,然后转到“运行时 > 重新启动并运行所有内容”以重置训练,然后返回此单元格。现在,您可以上传下载的 zip 文件并继续训练。
upload_and_unzip_file_to(checkpoint_dir)
train_checkpointer.initialize_or_restore()
global_step = tf.compat.v1.train.get_global_step()
上传检查点目录后,返回“训练一次迭代”以继续训练,或返回“生成视频”以检查加载的策略的性能。
或者,您可以保存策略(模型)并恢复它。与检查点不同,您无法继续训练,但您仍然可以部署模型。请注意,下载的文件比检查点文件小得多。
tf_policy_saver.save(policy_dir)
policy_zip_filename = create_zip_file(policy_dir, os.path.join(tempdir, 'exported_policy'))
INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/saved_model/nested_structure_coder.py:458: UserWarning: Encoding a StructuredValue with type tfp.distributions.Deterministic_ACTTypeSpec; loading this StructuredValue will require that this type be imported and registered. warnings.warn("Encoding a StructuredValue with type %s; loading this " INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
if files is not None:
files.download(policy_zip_filename) # try again if this fails: https://github.com/googlecolab/colabtools/issues/469
上传下载的策略目录(exported_policy.zip)并检查保存的策略的性能。
upload_and_unzip_file_to(policy_dir)
saved_policy = tf.saved_model.load(policy_dir)
run_episodes_and_create_video(saved_policy, eval_env, eval_py_env)
SavedModelPyTFEagerPolicy
如果您不想使用 TF 策略,那么您也可以通过使用 py_tf_eager_policy.SavedModelPyTFEagerPolicy
直接使用 Python 环境中的 saved_model。
请注意,这仅在启用急切模式时有效。
eager_py_policy = py_tf_eager_policy.SavedModelPyTFEagerPolicy(
policy_dir, eval_py_env.time_step_spec(), eval_py_env.action_spec())
# Note that we're passing eval_py_env not eval_env.
run_episodes_and_create_video(eager_py_policy, eval_py_env, eval_py_env)
将策略转换为 TFLite
有关更多详细信息,请参阅 TensorFlow Lite 转换器。
converter = tf.lite.TFLiteConverter.from_saved_model(policy_dir, signature_keys=["action"])
tflite_policy = converter.convert()
with open(os.path.join(tempdir, 'policy.tflite'), 'wb') as f:
f.write(tflite_policy)
2023-12-22 12:18:02.611792: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format. 2023-12-22 12:18:02.611828: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency. Summary on the non-converted ops: --------------------------------- * Accepted dialects: tfl, builtin, func * Non-Converted Ops: 11, Total Ops 28, % non-converted = 39.29 % * 11 ARITH ops - arith.constant: 11 occurrences (i64: 2, f32: 4, i32: 5) (i64: 1) (i32: 1) (i64: 1) (i32: 1) (f32: 2) (i64: 1) (i64: 1) (i64: 1, f32: 1) (i32: 2) (i32: 2)
在 TFLite 模型上运行推理
有关更多详细信息,请参阅 TensorFlow Lite 推理。
import numpy as np
interpreter = tf.lite.Interpreter(os.path.join(tempdir, 'policy.tflite'))
policy_runner = interpreter.get_signature_runner()
print(policy_runner._inputs)
{'0/discount': 1, '0/observation': 2, '0/reward': 3, '0/step_type': 0}
policy_runner(**{
'0/discount':tf.constant(0.0),
'0/observation':tf.zeros([1,4]),
'0/reward':tf.constant(0.0),
'0/step_type':tf.constant(0)})
{'action': array([1])}