【问题标题】:Reinforcement learning cost function强化学习成本函数
【发布时间】:2019-03-12 16:37:52
【问题描述】:

新手问题 我正在使用 TensorFlow 编写一个 OpenAI Gym pong 播放器,到目前为止,我已经能够基于随机初始化创建网络,以便它随机返回以向上或向下移动玩家桨。

在一个时期结束后(计算机赢了 21 场比赛),我收集了一组观察结果、动作和分数。游戏的最终观察得到一个分数,并且可以根据贝尔曼方程对每个先前的观察进行评分。

现在我的问题是我还不明白的: 如何计算成本函数,以便将其作为反向传播的起始梯度传播?我完全理解了监督学习,但在这里我们没有任何标签可以再次评分。

如何开始优化网络?

也许指向现有代码或一些文献的指针会有所帮助。

这是我计算奖励的地方:

def compute_observation_rewards(self, gamma, up_score_probabilities):
        """
        Applies Bellman equation and determines reward for each stored observation
        :param gamma: Learning decay
        :param up_score_probabilities: Probabilities for up score
        :returns: List of scores for each move
        """

        score_sum = 0
        discounted_rewards = []
        # go backwards through all observations
        for i, p in enumerate(reversed(self._states_score_action)):
            o = p[0]
            s = p[1]

            if s != 0:
                score_sum = 0

            score_sum = score_sum * gamma + s
            discounted_rewards.append(score_sum)

        # # normalize scores
        discounted_rewards = np.array(discounted_rewards)
        discounted_rewards -= np.mean(discounted_rewards)
        discounted_rewards /= np.std(discounted_rewards)

        return discounted_rewards

下面是我的网络:

with tf.variable_scope('NN_Model', reuse=tf.AUTO_REUSE):

        layer1 = tf.layers.conv2d(inputs,
                                3,
                                3,
                                strides=(1, 1),
                                padding='valid',
                                data_format='channels_last',
                                dilation_rate=(1, 1),
                                activation= tf.nn.relu, 
                                use_bias=True,
                                bias_initializer=tf.zeros_initializer(),
                                trainable=True,
                                name='layer1'
                            )
        # (N - F + 1) x (N - F + 1)
        # => layer1 should be 
        # (80 - 3 + 1) * (80 - 3 + 1) = 78 x 78

        pool1 = tf.layers.max_pooling2d(layer1,
                                        pool_size=5,
                                        strides=2,
                                        name='pool1')

        # int((N - f) / s +1) 
        # (78 - 5) / 2 + 1 = 73/2 + 1 = 37

        layer2 = tf.layers.conv2d(pool1,
                                5,
                                5,
                                strides=(2, 2),
                                padding='valid',
                                data_format='channels_last',
                                dilation_rate=(1, 1),
                                activation= tf.nn.relu, 
                                use_bias=True,
                                kernel_initializer=tf.random_normal_initializer(),
                                bias_initializer=tf.zeros_initializer(),
                                trainable=True,
                                name='layer2',
                                reuse=None
                            )

        # ((N + 2xpadding - F) / stride + 1) x ((N + 2xpadding - F) / stride + 1)
        # => layer1 should be 
        # int((37 + 0 - 5) / 2) + 1 
        # 16 + 1 = 17

        pool2 = tf.layers.max_pooling2d(layer2,
                                        pool_size=3,
                                        strides=2,
                                        name='pool2')

        # int((N - f) / s +1) 
        # (17 - 3) / 2 + 1 = 7 + 1 = 8

        flat1 = tf.layers.flatten(pool2, 'flat1')

        # Kx64

        full1 = tf.contrib.layers.fully_connected(flat1,
                                            num_outputs=1,
                                            activation_fn=tf.nn.sigmoid,
                                            weights_initializer=tf.contrib.layers.xavier_initializer(),
                                            biases_initializer=tf.zeros_initializer(),
                                            trainable=True,
                                            scope=None
                                        )

【问题讨论】:

    标签: reinforcement-learning tensorflow backpropagation gradient-descent


    【解决方案1】:

    您正在寻找的算法称为 REINFORCE。 我建议阅读Sutton and Barto's RL book 的第 13 章。

    这是书中的伪代码。

    这里,theta 是神经网络的权重集。如果您不熟悉其他符号,我建议您阅读上述书籍的第 3 章。它涵盖了基本的问题表述。

    【讨论】:

    • 谢谢,我会调查的。
    • 不客气。如果答案有帮助,您应该点赞。
    猜你喜欢
    • 2013-12-06
    • 2018-11-05
    • 2019-04-16
    • 2016-10-24
    • 2022-09-28
    • 2019-01-18
    • 2021-06-26
    • 2020-06-30
    • 2011-02-14
    相关资源
    最近更新 更多