autorl/agents.py
def train(self, n_episodes, max_steps=1000, epsilon=0.01, epsilon_schedule=10, buffer_size=128,
              batch_size=16, weight_freeze=None, network=None):
        """
        For each episode, play with the epsilon-greedy policy and record
        the states, actions, and rewards. At each step, use the action value
        function and a set of random 4-tuples from the replay buffer to bootstrap
        the q-learning targets and update the network.

        :param n_episodes:
        :param max_steps:
        :param epsilon:
        :param epsilon_schedule:
        :param buffer_size:
        :param batch_size:
        :param weight_freeze:
        :param network:
        :return:
        """
        if self._q_network is None:
            self._q_network = self.configure(network)

        self._buffer_size = buffer_size

        if weight_freeze is not None:
            self._frozen_q_network = self.configure(network)
            self._set_frozen_weights()

        max_reward = 0.0
        total_steps = 0
        freeze_step = 0
        loss = 0.0

        for i in range(n_episodes):

            if epsilon_schedule is not None:
                if i % epsilon_schedule == 0:
                    epsilon = epsilon*0.999

            obs = self.reset()
            obs = obs.reshape([1] + self._state_dim)
            ep_reward = 0.0

            for j in range(max_steps):
                total_steps += 1

                self.render()
                action = self.policy(obs, epsilon=epsilon)
                new_obs, reward, done, info = self.observe(action)
                new_obs = new_obs.reshape([1] + self._state_dim)
                ep_reward += reward

                # add to the replay buffer (evicts an old record if already full)
                # and sample to generate a batch for building targets and training
                self._buffer_add((obs, action, reward, new_obs))
                tuple_batch = self._buffer_batch(batch_size)
                batch_x, batch_y = self._batch(tuple_batch, weight_freeze)

                # train
                loss, grads = train.grad(self._q_network, batch_x, batch_y)
                updates = zip(grads, self._q_network.trainable_variables)
                self._q_network.optimizer.apply_gradients(updates)

                if done:
                    max_reward = max(max_reward, ep_reward)
                    print("Current max reward: {}".format(max_reward))
                    break
                else:
                    obs = new_obs

            print("Finished game {}...".format(i+1))
            print("Current loss: {}".format(loss))

            # if we're using frozen weights, check if its time to update
            # by dividing the total steps by the weight freeze param
            # and taking the floor. if the integer result is greater
            # than the current freeze_step integer value, then it's time
            if weight_freeze is not None:
                current_step = int(total_steps / weight_freeze)
                if current_step > freeze_step:
                    self._set_frozen_weights()
                    freeze_step = current_step

        For each episode, play with the epsilon-greedy policy and record
        the states, actions, and rewards. At each step, use the action value
        function and a set of random 4-tuples from the replay buffer to bootstrap
        the q-learning targets and update the network.

        :param n_episodes:
        :param max_steps:
        :param epsilon:
        :param epsilon_schedule:
        :param buffer_size:
        :param batch_size:
        :param weight_freeze:
        :param network:
        :return:
        
For each episode, play with the epsilon-greedy policy and record
the states, actions, and rewards. At each step, use the action value
function and a set of random 4-tuples from the replay buffer to bootstrap
the q-learning targets and update the network.
For each episode play with the epsilon - greedy policy and record the states actions and rewards . At each step use the action value function and a set of random 4 - tuples from the replay buffer to bootstrap the q - learning targets and update the network .
def train(self, n_episodes, max_steps=1000, epsilon=0.01, epsilon_schedule=10, buffer_size=128,
              batch_size=16, weight_freeze=None, network=None):
        if self._q_network is None:
            self._q_network = self.configure(network)
        self._buffer_size = buffer_size
        if weight_freeze is not None:
            self._frozen_q_network = self.configure(network)
            self._set_frozen_weights()
        max_reward = 0.0
        total_steps = 0
        freeze_step = 0
        loss = 0.0
        for i in range(n_episodes):
            if epsilon_schedule is not None:
                if i % epsilon_schedule == 0:
                    epsilon = epsilon*0.999
            obs = self.reset()
            obs = obs.reshape([1] + self._state_dim)
            ep_reward = 0.0
            for j in range(max_steps):
                total_steps += 1
                self.render()
                action = self.policy(obs, epsilon=epsilon)
                new_obs, reward, done, info = self.observe(action)
                new_obs = new_obs.reshape([1] + self._state_dim)
                ep_reward += reward
                self._buffer_add((obs, action, reward, new_obs))
                tuple_batch = self._buffer_batch(batch_size)
                batch_x, batch_y = self._batch(tuple_batch, weight_freeze)
                loss, grads = train.grad(self._q_network, batch_x, batch_y)
                updates = zip(grads, self._q_network.trainable_variables)
                self._q_network.optimizer.apply_gradients(updates)
                if done:
                    max_reward = max(max_reward, ep_reward)
                    print("Current max reward: {}".format(max_reward))
                    break
                else:
                    obs = new_obs
            print("Finished game {}...".format(i+1))
            print("Current loss: {}".format(loss))
            if weight_freeze is not None:
                current_step = int(total_steps / weight_freeze)
                if current_step > freeze_step:
                    self._set_frozen_weights()
                    freeze_step = current_step
def train ( self , n_episodes , max_steps = 1000 , epsilon = 0.01 , epsilon_schedule = 10 , buffer_size = 128 , batch_size = 16 , weight_freeze = None , network = None ) : if self . _q_network is None : self . _q_network = self . configure ( network ) self . _buffer_size = buffer_size if weight_freeze is not None : self . _frozen_q_network = self . configure ( network ) self . _set_frozen_weights ( ) max_reward = 0.0 total_steps = 0 freeze_step = 0 loss = 0.0 for i in range ( n_episodes ) : if epsilon_schedule is not None : if i % epsilon_schedule == 0 : epsilon = epsilon * 0.999 obs = self . reset ( ) obs = obs . reshape ( [ 1 ] + self . _state_dim ) ep_reward = 0.0 for j in range ( max_steps ) : total_steps += 1 self . render ( ) action = self . policy ( obs , epsilon = epsilon ) new_obs , reward , done , info = self . observe ( action ) new_obs = new_obs . reshape ( [ 1 ] + self . _state_dim ) ep_reward += reward self . _buffer_add ( ( obs , action , reward , new_obs ) ) tuple_batch = self . _buffer_batch ( batch_size ) batch_x , batch_y = self . _batch ( tuple_batch , weight_freeze ) loss , grads = train . grad ( self . _q_network , batch_x , batch_y ) updates = zip ( grads , self . _q_network . trainable_variables ) self . _q_network . optimizer . apply_gradients ( updates ) if done : max_reward = max ( max_reward , ep_reward ) print ( "Current max reward: {}" . format ( max_reward ) ) break else : obs = new_obs print ( "Finished game {}..." . format ( i + 1 ) ) print ( "Current loss: {}" . format ( loss ) ) if weight_freeze is not None : current_step = int ( total_steps / weight_freeze ) if current_step > freeze_step : self . _set_frozen_weights ( ) freeze_step = current_step
For each episode, play with the epsilon-greedy policy and record
the states, actions, and rewards.
For each episode play with the epsilon - greedy policy and record the states actions and rewards .
"""
        For each episode, play with the epsilon-greedy policy and record
        the states, actions, and rewards. At each step, use the action value
        function and a set of random 4-tuples from the replay buffer to bootstrap
        the q-learning targets and update the network.

        :param n_episodes:
        :param max_steps:
        :param epsilon:
        :param epsilon_schedule:
        :param buffer_size:
        :param batch_size:
        :param weight_freeze:
        :param network:
        :return:
        """ # add to the replay buffer (evicts an old record if already full) # and sample to generate a batch for building targets and training # train # if we're using frozen weights, check if its time to update # by dividing the total steps by the weight freeze param # and taking the floor. if the integer result is greater # than the current freeze_step integer value, then it's time
None
self
n_episodes
None
max_steps
None
epsilon
None
epsilon_schedule
None
buffer_size
None
batch_size
None
weight_freeze
None
network
None