0

TLDR:我有一个包含 2 个模块的网络,1. 图像位置预测(x 和 y 的回归)输入:2. Q 值函数预测。我想监督每个使用渐变胶带的损失。

因此,为了简要介绍一下背景,我正在研究一个 RL 问题,我想在给定图像的情况下预测 Q 值。我正在比较几种方法。我创建了一个网络,可以在给定图像坐标的情况下预测 Q 值。现在我想使用 CNN 来预测给定图像在图像坐标中的代理位置。

我的问题是我想监督 2 个单独的损失。一种用于在给定估计图像坐标的情况下预测 Q 值。还有一个用于估计给定图像的代理的图像坐标。

这是我这样做的模型:

inputPos = k.layers.Input(shape=(2,),name="pose")
inputPic = k.layers.Input(shape=(240,240,3,),name="img")


c1 = k.layers.Conv2D(64,(3,3))(inputPic)
c2 = k.layers.Conv2D(32,(3,3),dilation_rate=2)(c1)
m1 = k.layers.MaxPool2D()(c2)
f1 = k.layers.Flatten()(m1)
dc = k.layers.Dense(20,activation=k.activations.relu)(f1)

######I estimate pose here and will want to supervise a loss###########
interOutPos = k.layers.Dense(2,activation=k.activations.relu,name='poseEst')(dc)

######## now is the same as simple agent but input will be pos est
d2 = k.layers.Dense(25)(interOutPos)
d2 = k.layers.LeakyReLU()(d2)


####I also want to supervise a loss for these Q_values#######
o1 = k.layers.Dense(self.steeringBins,activation=k.activations.linear)(d2)
o2 = k.layers.Dense(self.forceBins,activation=k.activations.linear)(d2)


m = k.Model(inputs=[inputPos,inputPic], outputs=[o1,o2,interOutPos])

我一直在尝试我的姿态估计梯度,因为所有这些都没有。这是我目前使用渐变胶带的尝试:

with tf.GradientTape(persistent=True) as  tape:
    qValues = self.model([np.asarray([ x for x in states[:,0]]),np.asarray([ x for x in states[:,1]])])

    qValuesForSteerActionsTaken = tf.reduce_sum(tf.multiply(qValues[0], maskSteer), axis=1)
    qValuesForForceActionsTaken = tf.reduce_sum(tf.multiply(qValues[1], maskForce), axis=1)

    q_loss = q_loss_function(dSteer, qValuesForSteerActionsTaken) + q_loss_function(dForce,
                                                                              qValuesForForceActionsTaken)
    pose = np.asarray([x for x in states[:, 0]])
    pose_loss = pose_loss_function(pose, predictedRewardForNextState[2])



q_losses.append(q_loss)

layer_names = [layer.name for layer in self.model.layers]
layer_idx = layer_names.index('poseEst') + 1


qlayers = self.model.trainable_variables[layer_idx:]#all layers from pose estimation to output
locLayers = self.model.trainable_variables[:layer_idx]#all layers from input to pose estimation

#update q net
posgrads = tape.gradient(pose_loss, locLayers)
qgrads = tape.gradient(q_loss, qlayers)

我的感觉是我需要在 loc 层中包含输出层?我真的不太确定我已经敲了几个小时的头,任何帮助将不胜感激。

4

0 回答 0