This below code, the directory 'training_output' is created and three csv files 'rewards', 'losses',and 'metrics' were created, but none of the results data were stored within the csv files, even header is also did not written on the csv file.

def train(env, agent, num_episodes, max_timesteps, batch_size):
    # CSV setup: Ensure the output directory exists.
    os.makedirs('training_output', exist_ok=True)
    
    total_samples=1000
    
    # Opening CSV files for writing.   
    rewards_file = open('training_output/rewards.csv', 'w', newline='')
    losses_file = open('training_output/losses.csv', 'w', newline='')
    metrics_file = open('training_output/metrics.csv', 'w', newline='')
    # Creating CSV writers.
    rewards_writer = csv.writer(rewards_file)
    losses_writer = csv.writer(losses_file)
    metrics_writer = csv.writer(metrics_file)
    
    # Writing headers.
    rewards_writer.writerow(['Episode', 'Immediate Reward', 'Cumulative Reward'])
    losses_writer.writerow(['Episode', 'Actor Loss', 'Critic Loss'])
    headers = ['Episode', 'Actor Loss', 'Critic Loss', 'Area', 'PowerDissipation', 'SlewRate', 
                'Gain', 'Bandwidth3dB', 'UnityGainFreq', 'PhaseMargin', 'Mean Episode Reward', 'Deployment Accuracy']
    metrics_writer.writerow(headers)
        
    start_episode = 0    
    rewards_log = []
    losses_log = []
    episode_lengths = []    
   
    for episode in range(start_episode, num_episodes):        
        node_features_tensor, edge_feature_tensor, edge_index, performance_metrics = env.reset()
        state = (node_features_tensor, edge_feature_tensor, edge_index)
    
        episode_rewards = []
        states = []
        actions = []
        log_probs = []
        values = []
        masks = []
        perf_metrics_log = []
    
        for t in range(max_timesteps):
            # Selecting action based on the current state
            action, log_prob, perf_metrics = agent.select_action(state, performance_metrics)
            next_node_features_tensor, next_edge_feature_tensor, next_edge_index, reward, done, previous_metrics = env.step(action, perf_metrics)
            next_state = (next_node_features_tensor, next_edge_feature_tensor, next_edge_index)
    
            episode_rewards.append(reward)
            states.append(state)
            actions.append(action)
            log_probs.append(log_prob.item())
            values.append(agent.critic(state[0]).item())  # Ensure Critic can handle state format
            masks.append(1.0 - float(done))
            perf_metrics_log.append(perf_metrics)
                
            performance_metrics = previous_metrics
            state = next_state
    
            if done:
                break
    
        next_value = agent.critic(state[0]).item() if not done else 0        
        returns = agent.compute_gae(next_value, episode_rewards, masks, values)
    
        # Correcting format for updating the policy
        advantages = (torch.tensor(returns) - torch.tensor(values)).detach().numpy()
        returns = torch.tensor(returns).detach().numpy()
        # Update the policy with collected data
        average_loss_actor, average_loss_critic = agent.update_policy(states, actions, log_probs, returns, advantages)                        
            
        episode_length = len(episode_rewards)
        total_reward = sum(episode_rewards)
            
        # Compute mean episode reward and deployment accuracy (example placeholders)
        mean_episode_reward = total_reward / episode_length
        deployment_accuracy = calculate_accuracy(perf_metrics_log) 
            
        # Log episode information            
        print(f'Episode {episode + 1}/{num_episodes}, Total Reward: {total_reward}')
             
        # Logging
        logging.info(f"Episode {episode + 1} - Reward: {total_reward}, Length: {episode_length}, Actor Loss: {average_loss_actor}, Critic Loss: {average_loss_critic}")
        logging.info(f"Episode {episode + 1} - Mean Episode Reward: {mean_episode_reward}, Deployment Accuracy: {deployment_accuracy}")
   
        # Writing to CSV files
        rewards_writer.writerow([episode + 1, total_reward, total_reward])  # Assuming total_reward represents the cumulative reward
        losses_writer.writerow([episode + 1, average_loss_actor, average_loss_critic])             
            
        # Writing performance metrics to CSV, alongside mean episode reward and deployment accuracy
        metrics_row = [episode + 1, average_loss_actor, average_loss_critic] + [performance_metrics[metric] for metric in ['Area', 'PowerDissipation', 'SlewRate', 'Gain', 'Bandwidth3dB', 'UnityGainFreq', 'PhaseMargin']] + [mean_episode_reward, deployment_accuracy]
        metrics_writer.writerow(metrics_row)
            
        # Append to logs for pandas DataFrame
        rewards_log.append({'episode': episode + 1, 'total_reward': total_reward})
        losses_log.append({'episode': episode + 1, 'actor_loss': average_loss_actor, 'critic_loss': average_loss_critic})
            
        episode_lengths.append(episode_length)
        
    # Close CSV files after the training loop.
    rewards_file.close()
    losses_file.close()