在使用tensorflow agents库中的dqn agent.collect_policy.action(time_step)方法时,可能会遇到一个tensorflow.python.framework.errors_impl.invalidargumenterror。完整的错误信息通常包含以下关键短语:'then' and 'else' must have the same size. but received: [1] vs. [] [op:select]。这表明在内部的条件选择操作(如tf.where)中,两个分支(then和else)的张量维度不一致,一个期望有大小为[1]的维度,而另一个却为空[]。
值得注意的是,agent.policy.action(time_step)通常可以正常工作,这使得问题更难以定位。这种差异通常源于collect_policy在探索阶段(例如epsilon_greedy_policy)内部处理逻辑的复杂性,它可能包含需要更严格维度匹配的条件操作。
此错误的根本原因在于TimeStepSpec中定义的各个张量的形状与实际传入TimeStep实例的张量形状之间存在批次维度处理上的不一致。
在tf_agents中,TimeStepSpec(以及action_spec和observation_spec)定义的是单个时间步或单个观测/动作的元素形状,它不包含批次维度。例如,如果一个奖励值是一个标量,那么其TensorSpec的形状应该定义为shape=()。
然而,当我们在实际创建TimeStep实例并将数据传递给策略时,这些数据通常需要包含一个批次维度,即使批次大小为1。这意味着一个标量值在实际张量中应该表示为shape=(1,)(例如,tf.convert_to_tensor([reward]))。
当TimeStepSpec定义为shape=(1,)(错误地包含了批次维度)而实际数据也以shape=(1,)传入时,表面上看起来匹配,但当tf_agents内部处理并移除批次维度进行与TimeStepSpec的匹配时,可能会导致问题。更常见的情况是,当TimeStepSpec定义为shape=(1,)而内部期望shape=(),或者反之,导致了维度不匹配。
具体到InvalidArgumentError: 'then' and 'else' must have the same size. but received: [1] vs. [],这通常发生在epsilon_greedy_policy的内部,它会根据一个条件(例如,是否进行探索)选择不同的动作。如果用于选择的张量(例如,动作或相关值)在维度上与期望不符,就会触发此错误。例如,如果一个分支生成了一个期望的[1]大小的张量,而另一个分支由于TimeStepSpec的错误定义,导致生成了一个空张量[],则会引发此错误。
解决此问题的关键是确保TimeStepSpec中的形状定义与实际TimeStep中张量的形状处理方式保持一致,特别是要理解TimeStepSpec定义的是非批次维度的形状。
对于标量值(如reward、discount、step_type),其TensorSpec的形状应定义为shape=(),表示单个元素是一个标量。对于观测值,如果它是一个一维数组(例如[4,4,4,4,4,6]),并且amountMachines是其长度,那么其TensorSpec的形状应定义为shape=(amountMachines,)。
错误的 TimeStepSpec 示例:
time_step_spec = TimeStep( step_type = tensor_spec.BoundedTensorSpec(shape=(1,), dtype=tf.int32, minimum=0, maximum=2), reward = tensor_spec.TensorSpec(shape=(1,), dtype=tf.float32), # 错误:对于标量应为 shape=() discount = tensor_spec.TensorSpec(shape=(1,), dtype=tf.float32), # 错误:对于标量应为 shape=() observation = tensor_spec.TensorSpec(shape=(1,amountMachines), dtype=tf.int32) # 错误:如果 amountMachines 是特征维度,不应包含额外的 (1,) )
正确的 TimeStepSpec 示例:
from tf_agents.specs import tensor_spec from tf_agents.trajectories import TimeStep import tensorflow as tf # 假设 amountMachines 是你的观测向量的长度 amountMachines = 6 time_step_spec = TimeStep( step_type = tensor_spec.BoundedTensorSpec(shape=(), dtype=tf.int32, minimum=0, maximum=2), # 修正为 shape=() reward = tensor_spec.TensorSpec(shape=(), dtype=tf.float32), # 修正为 shape=() discount = tensor_spec.TensorSpec(shape=(), dtype=tf.float32), # 修正为 shape=() observation = tensor_spec.TensorSpec(shape=(amountMachines,), dtype=tf.int32) # 修正为 (amountMachines,) ) num_possible_actions = 729 action_spec = tensor_spec.BoundedTensorSpec( shape=(), dtype=tf.int32, minimum=0, maximum=num_possible_actions - 1) # 其他 Agent 初始化代码... # agent = dqn_agent.DqnAgent(...) # agent.initialize()
在创建实际的TimeStep实例时,即使批次大小为1,也需要确保每个张量都包含一个批次维度。这意味着对于标量数据,需要将其封装在列表中并转换为张量(例如tf.convert_to_tensor([value]),这将产生shape=(1,))。对于观测数据,如果它是一个一维数组,需要使用tf.expand_dims来添加批次维度。
正确的 TimeStep 实例构造示例:
from tf_agents.trajectories import TimeStep import tensorflow as tf import numpy as np # 假设这些值是你的当前环境状态 step_type_val = 0 # 示例值 reward_val = 0.0 # 示例值 discount_val = 0.95 # 示例值 current_state_np = np.array([4,4,4,4,4,6], dtype=np.int32) # 示例观测值 # 将NumPy数组转换为Tensor,并添加批次维度 # current_state_np 的 shape 是 (amountMachines,),需要变为 (1, amountMachines) current_state_batch = tf.expand_dims(tf.convert_to_tensor(current_state_np, dtype=tf.int32), axis=0) # 构造 TimeStep 实例 time_step = TimeStep( step_type=tf.convert_to_tensor([step_type_val], dtype=tf.int32), # 标量数据添加批次维度 reward=tf.convert_to_tensor([reward_val], dtype=tf.float32), # 标量数据添加批次维度 discount=tf.convert_to_tensor([discount_val], dtype=tf.float32), # 标量数据添加批次维度 observation=current_state_batch # 观测数据已添加批次维度 ) # 现在可以安全地调用 collect_policy # action_step = agent.collect_policy.action(time_step)
通过以上修正,确保TimeStepSpec准确反映了单个数据元素的形状,并且在构造TimeStep实例时正确地包含了批次维度,即可解决collect_policy中出现的“'then' and 'else' must have the same size”错误。这是tf_agents库中一个常见的陷阱,理解其内部的维度处理机制是高效使用该库的关键。
以上就是TensorFlow DQN collect_policy 运行时维度不匹配错误解析与解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号