assignment2_coding/starter_code/q2_linear.py
def add_loss_op(self, q, target_q):
        """
        Sets the loss of a batch, self.loss is a scalar

        Args:
            q: (tf tensor) shape = (batch_size, num_actions)
            target_q: (tf tensor) shape = (batch_size, num_actions)
        """
        # you may need this variable
        num_actions = self.env.action_space.n

        ##############################################################
        """
        TODO:
            The loss for an example is defined as:
                Q_samp(s) = r if done
                          = r + gamma * max_a' Q_target(s', a')
                loss = (Q_samp(s) - Q(s, a))^2
        HINT:
            - Config variables are accessible through self.config
            - You can access placeholders like self.a (for actions)
                self.r (rewards) or self.done_mask for instance
            - You may find the following functions useful
                - tf.cast
                - tf.reduce_max
                - tf.reduce_sum
                - tf.one_hot
                - tf.squared_difference
                - tf.reduce_mean
        """
        ##############################################################
        ##################### YOUR CODE HERE - 4-5 lines #############

        default = self.r + self.config.gamma * tf.reduce_max(target_q, axis=1)
        q_samp = tf.where(self.done_mask, self.r, default)
        actions = tf.one_hot(self.a, num_actions)
        q_old = tf.reduce_sum(tf.multiply(actions, q), axis=1)
        self.loss = tf.reduce_mean(tf.square(q_samp - q_old))
        print(self.loss)

        ##############################################################
        ######################## END YOUR CODE #######################


        Sets the loss of a batch, self.loss is a scalar

        Args:
            q: (tf tensor) shape = (batch_size, num_actions)
            target_q: (tf tensor) shape = (batch_size, num_actions)
        
Sets the loss of a batch, self.loss is a scalar
Sets the loss of a batch self . loss is a scalar
def add_loss_op(self, q, target_q):
        num_actions = self.env.action_space.n
        default = self.r + self.config.gamma * tf.reduce_max(target_q, axis=1)
        q_samp = tf.where(self.done_mask, self.r, default)
        actions = tf.one_hot(self.a, num_actions)
        q_old = tf.reduce_sum(tf.multiply(actions, q), axis=1)
        self.loss = tf.reduce_mean(tf.square(q_samp - q_old))
        print(self.loss)
def add_loss_op ( self , q , target_q ) : num_actions = self . env . action_space . n """
        TODO:
            The loss for an example is defined as:
                Q_samp(s) = r if done
                          = r + gamma * max_a' Q_target(s', a')
                loss = (Q_samp(s) - Q(s, a))^2
        HINT:
            - Config variables are accessible through self.config
            - You can access placeholders like self.a (for actions)
                self.r (rewards) or self.done_mask for instance
            - You may find the following functions useful
                - tf.cast
                - tf.reduce_max
                - tf.reduce_sum
                - tf.one_hot
                - tf.squared_difference
                - tf.reduce_mean
        """ default = self . r + self . config . gamma * tf . reduce_max ( target_q , axis = 1 ) q_samp = tf . where ( self . done_mask , self . r , default ) actions = tf . one_hot ( self . a , num_actions ) q_old = tf . reduce_sum ( tf . multiply ( actions , q ) , axis = 1 ) self . loss = tf . reduce_mean ( tf . square ( q_samp - q_old ) ) print ( self . loss )
Sets the loss of a batch, self.loss is a scalar
Sets the loss of a batch self . loss is a scalar
"""
        Sets the loss of a batch, self.loss is a scalar

        Args:
            q: (tf tensor) shape = (batch_size, num_actions)
            target_q: (tf tensor) shape = (batch_size, num_actions)
        """ # you may need this variable
 ##############################################################
 """
        TODO:
            The loss for an example is defined as:
                Q_samp(s) = r if done
                          = r + gamma * max_a' Q_target(s', a')
                loss = (Q_samp(s) - Q(s, a))^2
        HINT:
            - Config variables are accessible through self.config
            - You can access placeholders like self.a (for actions)
                self.r (rewards) or self.done_mask for instance
            - You may find the following functions useful
                - tf.cast
                - tf.reduce_max
                - tf.reduce_sum
                - tf.one_hot
                - tf.squared_difference
                - tf.reduce_mean
        """ ##############################################################
 ##################### YOUR CODE HERE - 4-5 lines #############
 ##############################################################
 ######################## END YOUR CODE #######################

self
q
(tf tensor) shape = (batch_size, num_actions)
( tf tensor ) shape = ( batch_size num_actions )
target_q
(tf tensor) shape = (batch_size, num_actions)
( tf tensor ) shape = ( batch_size num_actions )