tensorflow回归模型预测天气 tensorflow回归关键点
本文旨在解决使用 TensorFlow Agents 的 DQNcollect_policy 时遇到的 InvalidArgumentError,该错误解析“'then' 和 'else' 必须具有相同的值”核心问题是TimeStepSpec中对单个元素形状的定义与实际TimeStep数据中包含批次维度的张量形状之间存在不一致。通过统一TimeStepSpec和TimeStep中张量的维度处理方式,特别是对于处理批量大小为1的情况,可以有效解决此问题。1. 问题描述
在使用tensorflow代理库中的dqn agent.collect_policy.action(time_step)方法时,可能会遇到一个tensorflow.python.framework.errors_impl.invalidargumenterror。完整的错误信息通常包含以下关键字:'then'和'else'必须具有相同的大小。但得到: ),而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_specfrom tf_agents.trajectories import TimeStepimport 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 = 729action_spec = tensor_spec.BoundedTensorSpec( shape=(), dtype=tf.int32,minimum=0,maximum=num_possible_actions - 1)#其他代理初始化代码...#agent = dqn_agent.DqnAgent(...)#agent.initialize()登录后复制3.2构造TimeStep实例时添加模块模块
在创建实际的TimeStep实例时,即使批量大小为1,也需要确保每个张量都包含一个模块。
这意味着对于标量数据,需要将其封装在列表中并转换为张量(例如 tf.convert_to_tensor(, dtype=np.int32) # 完成安装值# 将NumPy转换阵列为Tensor,并添加批次维度# current_state_np 的形状是 (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), # 标量数据添加模块奖励=tf.convert_to_tensor([reward_val], dtype=tf.float32), # 标量数据添加批次维度 discount=tf.convert_to_tensor([discount_val], dtype=tf.float32), # 标量数据添加批次维度观察=current_state_batch # 基础数据已批次维度)#现在可以安全添加地调用collect_policy# action_step = agent.collect_policy.action(time_step)登录后复制4. 注意事项与总结TensorSpec vs.实际张量形状:始终记得TensorSpec定义的是单个元素的形状,不包括批次维度。而实际制定策略的张量(如TimeStep中的各个部分)必须包含批次维度,即使批次大小为1。批次大小为1的特殊性:当batch_size=1时,标量数据(例如奖学金)在TensorSpec中应为shape=(),但在实际张量中应为shape=(1,)。支持数据(如初始)在TensorSpec中应为shape=(N,),但在实际张量中应为shape=(1, N)。tf.expand_dims的使用:对于非标量初始值,如果其原始形状不包含批次维度,请务必使用tf.expand_dims(tensor, axis=0)来添加批次维度,相当于[batch_size,...]的形状。调试技巧: 当遇到这类维度错误时,仔细检查agent.time_step_spec和agent.action_spec的定义,并与你实际创建的TimeStep和ActionStep中的张量形状进行比对。可以使用tensor.shape和tensor_spec.shape来打印并验证。
通过以上,确保TimeStepSpec准确反映了单个数据元素的形状,并且在构造TimeStep实例时正确地包含了维度,即可解决collect_policy中出现的“‘然后’和” ‘else’必须具有相同的大小”错误。
这是tf_agents库中一个常见的陷阱,了解其内部的维度处理高效机制是使用该库的关键。
以上就是TensorFlow DQNcollect_policy时运行维度不匹配错误解析与解决方案的详细内容,更多请关注乐哥常识网相关其他!