q2_linear.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:cs234_reinforcement_learning 作者: hbghhy 项目源码 文件源码
def add_optimizer_op(self, scope):
        """
        Set self.train_op and self.grad_norm
        """

        ##############################################################
        """
        TODO: 1. get Adam Optimizer (remember that we defined self.lr in the placeholders
                section)
              2. compute grads wrt to variables in scope for self.loss
              3. clip the grads by norm with self.config.clip_val if self.config.grad_clip
                is True
              4. apply the gradients and store the train op in self.train_op
               (sess.run(train_op) must update the variables)
              5. compute the global norm of the gradients and store this scalar
                in self.grad_norm

        HINT: you may find the following functinos useful
            - tf.get_collection
            - optimizer.compute_gradients
            - tf.clip_by_norm
            - optimizer.apply_gradients
            - tf.global_norm

             you can access config variable by writing self.config.variable_name

        (be sure that you set self.train_op and self.grad_norm)
        """
        ##############################################################
        #################### YOUR CODE HERE - 8-12 lines #############

        optimizer = tf.train.AdamOptimizer(learning_rate=self.lr)
        variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=scope)
        gradients, v = list(zip(*optimizer.compute_gradients(self.loss, variables)))

        if self.config.grad_clip:
            gradients, _ = tf.clip_by_global_norm(gradients, self.config.clip_val)

        # Use the clipped gradients for optimization
        self.grad_norm = tf.global_norm(gradients)
        self.train_op = optimizer.apply_gradients(list(zip(gradients, v)))

        ##############################################################
        ######################## END YOUR CODE #######################
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号