I am trying to train a Stable Baselines agent while generating several logs, both for the console and a log file at the same time. To achieve this, I am using the following code:
os.makedirs(folder_path, exist_ok=True)
file = open(file_path, "w")
encoding = sys.stdout.encoding if sys.stdout.encoding else 'utf-8'
# Create a Tee object to duplicate the output
sys.stdout = Tee(sys.stdout, io.TextIOWrapper(file.buffer, encoding=encoding, errors='ignore'))
This code works correctly for prints, where the output is seen in both the console and the file. However, when training the PPO agent and trying to print interval logs, I get an error that the stable-baselines3 agent is not able to print the log with the said "double" output stream, as shown below:
Traceback (most recent call last):
File "/src/train_V1_agents.py", line 78, in <module>
runTrainingExecution( agent_name_index = agent, execution_uid = args.execution_uid )
File "/src/train_V1_agents.py", line 49, in runTrainingExecution
agent.train_agent(n_steps = n_steps, total_timesteps = total_timesteps, callback=None, log_interval=1, tb_log_name=tb_log_name, reset_num_timesteps=True, progress_bar=True)
File "/src/AI/Agents/RLAgent.py", line 278, in train_agent
self.learn(total_timesteps=total_timesteps, load_if_previous = True, training_step = i)
File "/src/AI/Agents/RLAgent.py", line 133, in learn
self.model.learn(total_timesteps=steps_to_train, callback=callback, log_interval=log_interval,
File "/env/lib/python3.11/site-packages/stable_baselines3/ppo/ppo.py", line 308, in learn
return super().learn(
^^^^^^^^^^^^^^
File "/env/lib/python3.11/site-packages/stable_baselines3/common/on_policy_algorithm.py", line 279, in learn
self.logger.dump(step=self.num_timesteps)
File "/env/lib/python3.11/site-packages/stable_baselines3/common/logger.py", line 525, in dump
_format.write(self.name_to_value, self.name_to_excluded, step)
File "/env/lib/python3.11/site-packages/stable_baselines3/common/logger.py", line 229, in write
if tqdm is not None and hasattr(self.file, "name") and self.file.name == "<stdout>":
^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: I/O operation on uninitialized object
Is there any way to avoid this error?