def fit(self, env, nb_steps, action_repetition=<NUM_LIT:1>, callbacks=None, verbose=<NUM_LIT:1>,<EOL>visualize=False, nb_max_start_steps=<NUM_LIT:0>, start_step_policy=None, log_interval=<NUM_LIT>,<EOL>nb_max_episode_steps=None):
if not self.compiled:<EOL><INDENT>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>if action_repetition < <NUM_LIT:1>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(action_repetition))<EOL><DEDENT>self.training = True<EOL>callbacks = [] if not callbacks else callbacks[:]<EOL>if verbose == <NUM_LIT:1>:<EOL><INDENT>callbacks += [TrainIntervalLogger(interval=log_interval)]<EOL><DEDENT>elif verbose > <NUM_LIT:1>:<EOL><INDENT>callbacks += [TrainEpisodeLogger()]<EOL><DEDENT>if visualize:<EOL><INDENT>callbacks += [Visualizer()]<EOL><DEDENT>history = History()<EOL>callbacks += [history]<EOL>callbacks = CallbackList(callbacks)<EOL>if hasattr(callbacks, '<STR_LIT>'):<EOL><INDENT>callbacks.set_model(self)<EOL><DEDENT>else:<EOL><INDENT>callbacks._set_model(self)<EOL><DEDENT>callbacks._set_env(env)<EOL>params = {<EOL>'<STR_LIT>': nb_steps,<EOL>}<EOL>if hasattr(callbacks, '<STR_LIT>'):<EOL><INDENT>callbacks.set_params(params)<EOL><DEDENT>else:<EOL><INDENT>callbacks._set_params(params)<EOL><DEDENT>self._on_train_begin()<EOL>callbacks.on_train_begin()<EOL>episode = np.int16(<NUM_LIT:0>)<EOL>self.step = np.int16(<NUM_LIT:0>)<EOL>observation = None<EOL>episode_reward = None<EOL>episode_step = None<EOL>did_abort = False<EOL>try:<EOL><INDENT>while self.step < nb_steps:<EOL><INDENT>if observation is None:  <EOL><INDENT>callbacks.on_episode_begin(episode)<EOL>episode_step = np.int16(<NUM_LIT:0>)<EOL>episode_reward = np.float32(<NUM_LIT:0>)<EOL>self.reset_states()<EOL>observation = deepcopy(env.reset())<EOL>if self.processor is not None:<EOL><INDENT>observation = self.processor.process_observation(observation)<EOL><DEDENT>assert observation is not None<EOL>nb_random_start_steps = <NUM_LIT:0> if nb_max_start_steps == <NUM_LIT:0> else np.random.randint(nb_max_start_steps)<EOL>for _ in range(nb_random_start_steps):<EOL><INDENT>if start_step_policy is None:<EOL><INDENT>action = env.action_space.sample()<EOL><DEDENT>else:<EOL><INDENT>action = start_step_policy(observation)<EOL><DEDENT>if self.processor is not None:<EOL><INDENT>action = self.processor.process_action(action)<EOL><DEDENT>callbacks.on_action_begin(action)<EOL>observation, reward, done, info = env.step(action)<EOL>observation = deepcopy(observation)<EOL>if self.processor is not None:<EOL><INDENT>observation, reward, done, info = self.processor.process_step(observation, reward, done, info)<EOL><DEDENT>callbacks.on_action_end(action)<EOL>if done:<EOL><INDENT>warnings.warn('<STR_LIT>'.format(nb_random_start_steps))<EOL>observation = deepcopy(env.reset())<EOL>if self.processor is not None:<EOL><INDENT>observation = self.processor.process_observation(observation)<EOL><DEDENT>break<EOL><DEDENT><DEDENT><DEDENT>assert episode_reward is not None<EOL>assert episode_step is not None<EOL>assert observation is not None<EOL>callbacks.on_step_begin(episode_step)<EOL>action = self.forward(observation)<EOL>if self.processor is not None:<EOL><INDENT>action = self.processor.process_action(action)<EOL><DEDENT>reward = np.float32(<NUM_LIT:0>)<EOL>accumulated_info = {}<EOL>done = False<EOL>for _ in range(action_repetition):<EOL><INDENT>callbacks.on_action_begin(action)<EOL>observation, r, done, info = env.step(action)<EOL>observation = deepcopy(observation)<EOL>if self.processor is not None:<EOL><INDENT>observation, r, done, info = self.processor.process_step(observation, r, done, info)<EOL><DEDENT>for key, value in info.items():<EOL><INDENT>if not np.isreal(value):<EOL><INDENT>continue<EOL><DEDENT>if key not in accumulated_info:<EOL><INDENT>accumulated_info[key] = np.zeros_like(value)<EOL><DEDENT>accumulated_info[key] += value<EOL><DEDENT>callbacks.on_action_end(action)<EOL>reward += r<EOL>if done:<EOL><INDENT>break<EOL><DEDENT><DEDENT>if nb_max_episode_steps and episode_step >= nb_max_episode_steps - <NUM_LIT:1>:<EOL><INDENT>done = True<EOL><DEDENT>metrics = self.backward(reward, terminal=done)<EOL>episode_reward += reward<EOL>step_logs = {<EOL>'<STR_LIT:action>': action,<EOL>'<STR_LIT>': observation,<EOL>'<STR_LIT>': reward,<EOL>'<STR_LIT>': metrics,<EOL>'<STR_LIT>': episode,<EOL>'<STR_LIT:info>': accumulated_info,<EOL>}<EOL>callbacks.on_step_end(episode_step, step_logs)<EOL>episode_step += <NUM_LIT:1><EOL>self.step += <NUM_LIT:1><EOL>if done:<EOL><INDENT>self.forward(observation)<EOL>self.backward(<NUM_LIT:0.>, terminal=False)<EOL>episode_logs = {<EOL>'<STR_LIT>': episode_reward,<EOL>'<STR_LIT>': episode_step,<EOL>'<STR_LIT>': self.step,<EOL>}<EOL>callbacks.on_episode_end(episode, episode_logs)<EOL>episode += <NUM_LIT:1><EOL>observation = None<EOL>episode_step = None<EOL>episode_reward = None<EOL><DEDENT><DEDENT><DEDENT>except KeyboardInterrupt:<EOL><INDENT>did_abort = True<EOL><DEDENT>callbacks.on_train_end(logs={'<STR_LIT>': did_abort})<EOL>self._on_train_end()<EOL>return history<EOL>
Trains the agent on the given environment.

        # Arguments
            env: (`Env` instance): Environment that the agent interacts with. See [Env](#env) for details.
            nb_steps (integer): Number of training steps to be performed.
            action_repetition (integer): Number of times the agent repeats the same action without
                observing the environment again. Setting this to a value > 1 can be useful
                if a single action only has a very small effect on the environment.
            callbacks (list of `keras.callbacks.Callback` or `rl.callbacks.Callback` instances):
                List of callbacks to apply during training. See [callbacks](/callbacks) for details.
            verbose (integer): 0 for no logging, 1 for interval logging (compare `log_interval`), 2 for episode logging
            visualize (boolean): If `True`, the environment is visualized during training. However,
                this is likely going to slow down training significantly and is thus intended to be
                a debugging instrument.
            nb_max_start_steps (integer): Number of maximum steps that the agent performs at the beginning
                of each episode using `start_step_policy`. Notice that this is an upper limit since
                the exact number of steps to be performed is sampled uniformly from [0, max_start_steps]
                at the beginning of each episode.
            start_step_policy (`lambda observation: action`): The policy
                to follow if `nb_max_start_steps` > 0. If set to `None`, a random action is performed.
            log_interval (integer): If `verbose` = 1, the number of steps that are considered to be an interval.
            nb_max_episode_steps (integer): Number of steps per episode that the agent performs before
                automatically resetting the environment. Set to `None` if each episode should run
                (potentially indefinitely) until the environment signals a terminal state.

        # Returns
            A `keras.callbacks.History` instance that recorded the entire training process.