agent/DQ_agent.py
def train(self,
        frames_to_play: int=25000000,
        batch_size: int=32,
        callback: Callable=None,
    ) -> None:
        """
        Train the network for a number of episodes (games).

        Args:
            frames_to_play: the number of frames to play the game for
            batch_size: the size of the replay history batches
            callback: an optional callback to get updates about the score,
                      loss, discount factor, and exploration rate every
                      episode
        """
        # the progress bar for the operation
        progress = tqdm(total=frames_to_play, unit='frame')
        progress.set_postfix(score='?', loss='?')

        while frames_to_play > 0:
            done = False
            score = 0
            loss = 0
            frames = 0
            # reset the game and get the initial state
            state = self._initial_state()

            while not done:
                # predict the best action based on the current state
                action = self.predict_action(state, self.exploration_rate.value)
                # step the exploration rate forward
                self.exploration_rate.step()
                # fire the action and observe the next state, reward, and flag
                next_state, reward, done = self._next_state(action)
                score += reward
                # push the memory onto the replay queue
                self.queue.push(state, action, reward, done, next_state)
                # set the state to the new state
                state = next_state
                # decrement the observation counter
                frames_to_play -= 1
                frames += 1
                # update Q from replay
                if frames_to_play % self.update_frequency == 0:
                    loss += self._replay(*self.queue.sample(size=batch_size))
                # update Target Q from online Q
                if frames_to_play % self.target_update_freq == 0:
                    self.target_model.set_weights(self.model.get_weights())

            # pass the score to the callback at the end of the episode
            if callable(callback):
                callback(self, score, loss)
            # update the progress bar
            progress.set_postfix(score=score, loss=loss)
            progress.update(frames)

        progress.close()

        Train the network for a number of episodes (games).

        Args:
            frames_to_play: the number of frames to play the game for
            batch_size: the size of the replay history batches
            callback: an optional callback to get updates about the score,
                      loss, discount factor, and exploration rate every
                      episode
        
Train the network for a number of episodes (games).
Train the network for a number of episodes ( games ) .
def train(self,
        frames_to_play: int=25000000,
        batch_size: int=32,
        callback: Callable=None,
    ) -> None:
        progress = tqdm(total=frames_to_play, unit='frame')
        progress.set_postfix(score='?', loss='?')
        while frames_to_play > 0:
            done = False
            score = 0
            loss = 0
            frames = 0
            state = self._initial_state()
            while not done:
                action = self.predict_action(state, self.exploration_rate.value)
                self.exploration_rate.step()
                next_state, reward, done = self._next_state(action)
                score += reward
                self.queue.push(state, action, reward, done, next_state)
                state = next_state
                frames_to_play -= 1
                frames += 1
                if frames_to_play % self.update_frequency == 0:
                    loss += self._replay(*self.queue.sample(size=batch_size))
                if frames_to_play % self.target_update_freq == 0:
                    self.target_model.set_weights(self.model.get_weights())
            if callable(callback):
                callback(self, score, loss)
            progress.set_postfix(score=score, loss=loss)
            progress.update(frames)
        progress.close()
def train ( self , frames_to_play : int = 25000000 , batch_size : int = 32 , callback : Callable = None , ) -> None : progress = tqdm ( total = frames_to_play , unit = 'frame' ) progress . set_postfix ( score = '?' , loss = '?' ) while frames_to_play > 0 : done = False score = 0 loss = 0 frames = 0 state = self . _initial_state ( ) while not done : action = self . predict_action ( state , self . exploration_rate . value ) self . exploration_rate . step ( ) next_state , reward , done = self . _next_state ( action ) score += reward self . queue . push ( state , action , reward , done , next_state ) state = next_state frames_to_play -= 1 frames += 1 if frames_to_play % self . update_frequency == 0 : loss += self . _replay ( * self . queue . sample ( size = batch_size ) ) if frames_to_play % self . target_update_freq == 0 : self . target_model . set_weights ( self . model . get_weights ( ) ) if callable ( callback ) : callback ( self , score , loss ) progress . set_postfix ( score = score , loss = loss ) progress . update ( frames ) progress . close ( )
Train the network for a number of episodes (games).
Train the network for a number of episodes ( games ) .
"""
        Train the network for a number of episodes (games).

        Args:
            frames_to_play: the number of frames to play the game for
            batch_size: the size of the replay history batches
            callback: an optional callback to get updates about the score,
                      loss, discount factor, and exploration rate every
                      episode
        """ # the progress bar for the operation # reset the game and get the initial state # predict the best action based on the current state # step the exploration rate forward # fire the action and observe the next state, reward, and flag # push the memory onto the replay queue # set the state to the new state # decrement the observation counter # update Q from replay # update Target Q from online Q # pass the score to the callback at the end of the episode # update the progress bar
self
frames_to_play
int
the number of frames to play the game for
the number of frames to play the game for
batch_size
int
the size of the replay history batches
the size of the replay history batches
callback
Callable
an optional callback to get updates about the score,
loss, discount factor, and exploration rate every
episode
an optional callback to get updates about the score loss discount factor and exploration rate every episode