[英]DQN understanding input and output (layer)
我对 DQN 的输入和 output(层)有疑问。
例如
两点:P1(x1, y1) 和 P2(x2, y2)
P1 必须走向 P2
我有以下信息:
P1 有 4 个可能的操作:
如何设置输入和 output 层?
那是对的吗? 我与 output 有什么关系? 我得到了 4 个 arrays,每个有 4 个值作为 output。 在 output 上做 argmax 是否正确?
编辑:
输入/State:
# Current position P1
state_pos = [x_POS, y_POS]
state_pos = np.asarray(state_pos, dtype=np.float32)
# Current position P2
state_wp = [wp_x, wp_y]
state_wp = np.asarray(state_wp, dtype=np.float32)
# Distance P1 - P2
state_dist_wp = [wp_x - x_POS, wp_y - y_POS]
state_dist_wp = np.asarray(state_dist_wp, dtype=np.float32)
# Direction P1 - P2
distance = [wp_x - x_POS, wp_y - y_POS]
norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
state_direction_wp = [distance[0] / norm, distance[1] / norm]
state_direction_wp = np.asarray(state_direction_wp, dtype=np.float32)
state = [state_pos, state_wp, state_dist_wp, state_direction_wp]
state = np.array(state)
网络:
def __init__(self):
self.q_net = self._build_dqn_model()
self.epsilon = 1
def _build_dqn_model(self):
q_net = Sequential()
q_net.add(Dense(4, input_shape=(4,2), activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(4, activation='linear', kernel_initializer='he_uniform'))
rms = tf.optimizers.RMSprop(lr = 1e-4)
q_net.compile(optimizer=rms, loss='mse')
return q_net
def random_policy(self, state):
return np.random.randint(0, 4)
def collect_policy(self, state):
if np.random.random() < self.epsilon:
return self.random_policy(state)
return self.policy(state)
def policy(self, state):
# Here I get 4 arrays with 4 values each as output
action_q = self.q_net(state)
在第一个 Dense 层中添加input_shape=(4,2)
导致 output 形状为(None, 4, 4)
。 用以下方式定义 q_net 可以解决它:
q_net = Sequential()
q_net.add(Reshape(target_shape=(8,), input_shape=(4,2)))
q_net.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
q_net.add(Dense(4, activation='linear', kernel_initializer='he_uniform'))
rms = tf.optimizers.RMSprop(lr = 1e-4)
q_net.compile(optimizer=rms, loss='mse')
return q_net
在这里, q_net.add(Reshape(target_shape=(8,), input_shape=(4,2)))
将 (None, 4, 2) 输入重塑为 (None, 8) [这里,None 表示批处理形状]。
为了验证,打印q_net.output_shape
它应该是(None, 4)
[而在前一种情况下它是(None, 4, 4)
]。
你还需要做一件事。 回想一下input_shape
没有考虑批量形状。 我的意思是, input_shape=(4,2)
期望输入形状为 (batch_shape, 4, 2)。 通过打印q_net.input_shape
进行验证,它应该是 output (None, 4, 2)
。 现在,您需要做的是 - 在您的输入中添加一个批次维度。 只需执行以下操作:
state_with_batch_dim = np.expand_dims(state,0)
并将state_with_batch_dim
作为输入传递给 q_net。 例如,您可以调用您编写的policy
方法,如policy(np.expand_dims(state,0))
并获取维度(batch_shape, 4)
[在本例中为(1,4)
] 的 output。
以下是您最初问题的答案:
Reshape
层,则节点或单元的概念不适合那里。 您可以将Reshape
层视为一个占位符,它采用形状为 (None, 4, 2) 的张量并输出形状为 (None, 8) 的重构张量。argmax
即可找到 q 值。向 DQN 提供一些有关其当前所面临方向的信息也可能是有意义的。 您可以将其设置为 (Current Pos X, Current Pos Y, X From Goal, Y From Goal, Direction)。
output 层应该按照您确定的顺序(上、左、下、右)。 Argmax 层适用于该问题。 确切的代码取决于您是否使用 TF / Pytorch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.