File size: 26,061 Bytes
51c0731 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
import os
import json
import pandas as pd
import numpy as np
from typing import List, Dict, Tuple, Optional, Any
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv, SubprocVecEnv
from stable_baselines3.common.utils import set_random_seed
from stable_baselines3.common.torch_layers import BaseFeaturesExtractor
from stable_baselines3.common.callbacks import EvalCallback, CheckpointCallback
import gymnasium as gym
from gymnasium import spaces
from dataclasses import dataclass
import logging
import random
from tqdm import tqdm
import time
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime
import argparse
import psutil
import gc
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("sales_training.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# GPU Setup
if torch.cuda.is_available():
device = torch.device("cuda")
logger.info(f"Using GPU: {torch.cuda.get_device_name(0)}")
else:
device = torch.device("cpu")
logger.info("GPU not available, using CPU")
@dataclass
class ConversationState:
"""Represents the state of a sales conversation for the RL environment."""
conversation_history: List[Dict[str, str]]
embedding: np.ndarray
conversation_metrics: Dict[str, float]
turn_number: int
conversion_probabilities: List[float]
@property
def state_vector(self) -> np.ndarray:
"""Create a flat vector representation of the conversation state."""
# Combine embedding with conversation metrics and history stats
metric_values = np.array(list(self.conversation_metrics.values()), dtype=np.float32)
turn_info = np.array([self.turn_number], dtype=np.float32)
prob_history = np.array(self.conversion_probabilities, dtype=np.float32)
# Pad probability history to a fixed size if needed
padded_probs = np.zeros(10, dtype=np.float32)
padded_probs[:len(prob_history)] = prob_history[-10:] if len(prob_history) > 10 else prob_history
return np.concatenate([
self.embedding,
metric_values,
turn_info,
padded_probs
])
# Custom neural network for feature extraction - optimized for GPU
class CustomLN(BaseFeaturesExtractor):
"""Custom feature extractor for the embedding vector using linear layers."""
def __init__(self, observation_space: gym.spaces.Box, features_dim: int = 128):
super().__init__(observation_space, features_dim)
# Get the input dimension from the observation space
n_input_channels = observation_space.shape[0]
# Create a network with linear layers
self.linear_network = nn.Sequential(
nn.Linear(n_input_channels, 512),
nn.ReLU(),
nn.Linear(512, 256),
nn.ReLU(),
nn.Linear(256, features_dim),
nn.ReLU(),
).to(device)
def forward(self, observations: torch.Tensor) -> torch.Tensor:
return self.linear_network(observations)
class SalesConversionEnv(gym.Env):
"""Reinforcement learning environment for sales conversation prediction."""
def __init__(self, conversations_df: pd.DataFrame, use_miniembeddings=True):
"""
Initialize the environment.
Args:
conversations_df: DataFrame containing sales conversations
use_miniembeddings: If True, reduce embedding dimension to save memory
"""
super().__init__()
self.conversations_df = conversations_df
self.current_conversation_idx = 0
self.max_turns = 20
self.use_miniembeddings = use_miniembeddings
# Get embedding dimension
embedding_cols = [col for col in conversations_df.columns if col.startswith('embedding_')]
self.full_embedding_dim = len(embedding_cols)
# Option to use reduced embedding dimension to save memory
if use_miniembeddings:
self.embedding_dim = min(1024, self.full_embedding_dim) # Use 1024 instead of 256
logger.info(f"Using reduced embeddings: {self.full_embedding_dim} -> {self.embedding_dim}")
else:
self.embedding_dim = self.full_embedding_dim
# Action space: Probability of conversion (0-1)
self.action_space = spaces.Box(
low=np.array([0.0]),
high=np.array([1.0]),
dtype=np.float32
)
# Observation space: Embeddings + metrics + turn info + probability history
self.observation_space = spaces.Box(
low=-np.inf,
high=np.inf,
shape=(self.embedding_dim + 5 + 1 + 10,), # Embeddings + 5 metrics + turn number + prob history
dtype=np.float32
)
self.current_turn = 0
self.conversation_state = None
self.true_probabilities = None
logger.info(f"Initialized SalesConversionEnv with {len(conversations_df)} conversations")
def _parse_conversation(self, conversation_idx: int) -> Tuple[List[Dict[str, str]], Dict[str, float], Dict[int, float]]:
"""Parse conversation data from the dataset."""
row = self.conversations_df.iloc[conversation_idx]
# Parse messages
try:
messages = json.loads(row['conversation'])
except (json.JSONDecodeError, TypeError) as e:
# Create a fallback simple conversation
messages = [
{"speaker": "customer", "message": "I'm interested in your product."},
{"speaker": "sales_rep", "message": "Thank you for your interest. How can I help?"}
]
# Parse metrics
metrics = {
'customer_engagement': float(row.get('customer_engagement', 0.5)),
'sales_effectiveness': float(row.get('sales_effectiveness', 0.5)),
'conversation_length': int(row.get('conversation_length', len(messages))),
'outcome': float(row.get('outcome', 0.5)),
'progress': 0.0 # Will be updated during stepping
}
# Parse probability trajectory
try:
probability_trajectory = json.loads(row['probability_trajectory'])
# Convert string keys to integers
probability_trajectory = {int(k): float(v) for k, v in probability_trajectory.items()}
except (json.JSONDecodeError, TypeError, KeyError) as e:
# If no trajectory or error, create a simple one
if row.get('outcome', 0) == 1:
probability_trajectory = {i: min(0.5 + i * 0.05, 0.95) for i in range(len(messages))}
else:
probability_trajectory = {i: max(0.5 - i * 0.05, 0.05) for i in range(len(messages))}
return messages, metrics, probability_trajectory
def _get_embedding_for_turn(self, conversation_idx: int, turn: int) -> np.ndarray:
"""Get the embedding for a specific conversation at a specific turn."""
row = self.conversations_df.iloc[conversation_idx]
# Get all embedding values
embedding_cols = [col for col in row.index if col.startswith('embedding_')]
try:
embedding = row[embedding_cols].values.astype(np.float32)
# Check for NaN or Inf values
if np.isnan(embedding).any() or np.isinf(embedding).any():
embedding = np.zeros(len(embedding_cols), dtype=np.float32)
except Exception as e:
embedding = np.zeros(len(embedding_cols), dtype=np.float32)
# Use dimensionality reduction for very large embeddings to save memory
if self.use_miniembeddings and len(embedding) > self.embedding_dim:
# Simple dimensionality reduction - average pooling
embedding = np.array([
np.mean(embedding[i:i+self.full_embedding_dim//self.embedding_dim])
for i in range(0, self.full_embedding_dim, self.full_embedding_dim//self.embedding_dim)
][:self.embedding_dim])
# Simple scaling based on turn progress to simulate evolving embeddings
progress = min(1.0, turn / self.max_turns)
scaled_embedding = embedding * (0.6 + 0.4 * progress)
return scaled_embedding
def reset(self, seed=None, options=None) -> Tuple[np.ndarray, Dict]:
"""Reset the environment to start a new episode."""
super().reset(seed=seed)
# Select a random conversation
self.current_conversation_idx = np.random.randint(0, len(self.conversations_df))
self.current_turn = 0
# Parse conversation data
messages, metrics, probability_trajectory = self._parse_conversation(self.current_conversation_idx)
self.true_probabilities = probability_trajectory
self.max_turns = min(20, len(messages))
# Initialize state
embedding = self._get_embedding_for_turn(self.current_conversation_idx, 0)
metrics = metrics.copy()
metrics['progress'] = 0.0
self.conversation_state = ConversationState(
conversation_history=messages[:1] if messages else [],
embedding=embedding,
conversation_metrics=metrics,
turn_number=0,
conversion_probabilities=[self.true_probabilities.get(0, 0.5)]
)
return self.conversation_state.state_vector, {}
def step(self, action: np.ndarray) -> Tuple[np.ndarray, float, bool, bool, Dict]:
"""Step the environment forward by one turn."""
# Extract predicted probability
predicted_prob = float(action[0])
# Get true probability for current turn
true_prob = self.true_probabilities.get(self.current_turn, 0.5)
# Calculate reward based on prediction accuracy
reward = 1.0 - abs(predicted_prob - true_prob)
# Apply higher reward/penalty at final step based on outcome
if self.current_turn == self.max_turns - 1:
outcome = self.conversation_state.conversation_metrics['outcome']
# Stronger penalty for confident wrong predictions
if outcome == 1 and predicted_prob < 0.5:
reward -= 1.0 * (0.5 - predicted_prob)
elif outcome == 0 and predicted_prob > 0.5:
reward -= 1.0 * (predicted_prob - 0.5)
# Update turn
self.current_turn += 1
done = self.current_turn >= self.max_turns
if not done:
# Update state
embedding = self._get_embedding_for_turn(self.current_conversation_idx, self.current_turn)
metrics = self.conversation_state.conversation_metrics.copy()
metrics['progress'] = self.current_turn / self.max_turns
messages = self._parse_conversation(self.current_conversation_idx)[0]
history = messages[:self.current_turn+1] if self.current_turn+1 < len(messages) else messages
# Add current prediction to history
conv_probs = self.conversation_state.conversion_probabilities.copy()
conv_probs.append(predicted_prob)
self.conversation_state = ConversationState(
conversation_history=history,
embedding=embedding,
conversation_metrics=metrics,
turn_number=self.current_turn,
conversion_probabilities=conv_probs
)
return self.conversation_state.state_vector, reward, done, False, {'true_prob': true_prob}
class SalesRLTrainer:
"""Trainer for the sales conversion prediction RL model."""
def __init__(self, dataset_path: str, model_save_path: str = "sales_conversion_model",
use_miniembeddings: bool = True, batch_size: int = 64):
"""
Initialize the trainer.
Args:
dataset_path: Path to the sales conversation dataset
model_save_path: Path to save trained model
use_miniembeddings: Whether to use reduced embeddings to save memory
batch_size: Batch size for training
"""
self.dataset_path = dataset_path
self.model_save_path = model_save_path
self.use_miniembeddings = use_miniembeddings
self.batch_size = batch_size
self.df = None
self.model = None
self.train_df = None
self.val_df = None
# Create directory for models and logs
os.makedirs(os.path.dirname(model_save_path) if os.path.dirname(model_save_path) else ".", exist_ok=True)
os.makedirs("logs", exist_ok=True)
logger.info(f"Initialized SalesRLTrainer with dataset: {dataset_path}")
# Monitor memory usage
self._log_memory_usage("Initial")
def _log_memory_usage(self, step=""):
"""Log current memory usage."""
process = psutil.Process(os.getpid())
cpu_mem = process.memory_info().rss / 1024 / 1024 # MB
gpu_mem = 0
if torch.cuda.is_available():
gpu_mem = torch.cuda.memory_allocated() / 1024 / 1024 # MB
logger.info(f"Memory usage [{step}] - CPU: {cpu_mem:.2f} MB, GPU: {gpu_mem:.2f} MB")
def load_dataset(self, validation_split=0.1, sample_size=None):
"""
Load and preprocess the sales conversation dataset.
Args:
validation_split: Proportion of data for validation
sample_size: Optional limit on dataset size to save memory
"""
logger.info(f"Loading dataset from {self.dataset_path}")
try:
# Read dataset in chunks to reduce memory usage
chunks = []
for chunk in pd.read_csv(self.dataset_path, chunksize=10000):
chunks.append(chunk)
# If sample size specified, break after enough chunks
if sample_size and sum(len(c) for c in chunks) >= sample_size:
break
self.df = pd.concat(chunks)
# If sample size specified, limit the dataset
if sample_size and len(self.df) > sample_size:
self.df = self.df.sample(sample_size, random_state=42)
logger.info(f"Loaded dataset with shape: {self.df.shape}")
# Validate embedding columns
embedding_cols = [col for col in self.df.columns if col.startswith('embedding_')]
if not embedding_cols:
raise ValueError("No embedding columns found in the dataset")
logger.info(f"Found {len(embedding_cols)} embedding dimensions")
# Clean the dataframe to reduce memory usage
for col in self.df.columns:
if col.startswith('embedding_'):
# Convert embedding columns to float32
self.df[col] = self.df[col].astype(np.float32)
elif col in ['outcome', 'customer_engagement', 'sales_effectiveness']:
# Convert numeric columns to float32
self.df[col] = self.df[col].astype(np.float32)
elif col == 'conversation_length':
# Convert to int32
self.df[col] = self.df[col].astype(np.int32)
# Split into train and validation sets
train_idx, val_idx = train_test_split(
np.arange(len(self.df)),
test_size=validation_split,
random_state=42
)
self.train_df = self.df.iloc[train_idx].reset_index(drop=True)
self.val_df = self.df.iloc[val_idx].reset_index(drop=True)
logger.info(f"Split dataset: {len(self.train_df)} training samples, {len(self.val_df)} validation samples")
# Monitor memory
self._log_memory_usage("After dataset load")
# Free up memory
gc.collect()
except Exception as e:
logger.error(f"Error loading dataset: {str(e)}")
raise
def train(self, total_timesteps: int = 100000, learning_rate: float = 0.0003, n_envs: int = 1):
"""
Train the RL model with GPU acceleration.
Args:
total_timesteps: Total timesteps for training
learning_rate: Learning rate for the optimizer
n_envs: Number of parallel environments
"""
if self.train_df is None:
self.load_dataset()
# Use only 1 environment with GPU for better memory efficiency
n_envs = 1 if torch.cuda.is_available() else n_envs
# Create training environment
def make_env(df_subset):
"""Create environment with a subset of data."""
def _init():
return SalesConversionEnv(df_subset, use_miniembeddings=self.use_miniembeddings)
return _init
# Create subsets of data for each environment
if n_envs > 1:
subset_size = len(self.train_df) // n_envs
env_makers = [
make_env(self.train_df.iloc[i*subset_size:(i+1)*subset_size if i < n_envs-1 else len(self.train_df)])
for i in range(n_envs)
]
env = SubprocVecEnv(env_makers)
else:
env = DummyVecEnv([make_env(self.train_df)])
# Create validation environment
val_env = DummyVecEnv([make_env(self.val_df)])
# Configure policy network
policy_kwargs = dict(
activation_fn=nn.ReLU,
net_arch=[dict(pi=[128, 64], vf=[128, 64])], # Smaller network to save memory
features_extractor_class=CustomLN,
features_extractor_kwargs=dict(features_dim=64)
)
# Initialize model with GPU support
self.model = PPO(
"MlpPolicy",
env,
policy_kwargs=policy_kwargs,
learning_rate=learning_rate,
n_steps=512, # Smaller n_steps to save memory
batch_size=self.batch_size,
n_epochs=5, # Fewer epochs to speed up training
gamma=0.99,
gae_lambda=0.95,
clip_range=0.2,
clip_range_vf=0.2,
ent_coef=0.01,
vf_coef=0.5,
max_grad_norm=0.5,
tensorboard_log="./logs/",
verbose=1,
device=device # Use GPU if available
)
# Set up callbacks
eval_callback = EvalCallback(
val_env,
best_model_save_path=f"{os.path.dirname(self.model_save_path)}/best_model",
log_path="./logs/",
eval_freq=max(2000, total_timesteps // 20), # Evaluate less frequently to save time
deterministic=True,
render=False
)
checkpoint_callback = CheckpointCallback(
save_freq=max(5000, total_timesteps // 10), # Save less frequently to reduce I/O
save_path="./logs/checkpoints/",
name_prefix="sales_model",
save_replay_buffer=False,
save_vecnormalize=False
)
# Monitor memory before training
self._log_memory_usage("Before training")
logger.info(f"Starting training for {total_timesteps} timesteps with {n_envs} environments on {device}")
self.model.learn(
total_timesteps=total_timesteps,
callback=[eval_callback, checkpoint_callback],
progress_bar=True
)
# Save final model
self.model.save(self.model_save_path)
logger.info(f"Model saved to {self.model_save_path}")
# Monitor memory after training
self._log_memory_usage("After training")
# Clean up to free memory
env.close()
val_env.close()
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
def evaluate(self, num_episodes: int = 100):
"""Evaluate the trained model."""
if self.model is None:
logger.info(f"Loading model from {self.model_save_path}")
self.model = PPO.load(self.model_save_path, device=device)
if self.val_df is None:
self.load_dataset()
# Create environment
env = SalesConversionEnv(self.val_df, use_miniembeddings=self.use_miniembeddings)
logger.info(f"Evaluating model on {num_episodes} episodes")
rewards = []
accuracies = []
predictions = []
true_outcomes = []
for i in tqdm(range(num_episodes), desc="Evaluating"):
obs, _ = env.reset()
done = False
episode_reward = 0
episode_predictions = []
true_values = []
while not done:
action, _ = self.model.predict(obs, deterministic=True)
obs, reward, done, _, info = env.step(action)
episode_reward += reward
episode_predictions.append(float(action[0]))
true_values.append(info['true_prob'])
rewards.append(episode_reward)
# Calculate accuracy based on final prediction
final_pred = episode_predictions[-1]
outcome = env.conversation_state.conversation_metrics['outcome']
correct = (final_pred >= 0.5 and outcome == 1) or (final_pred < 0.5 and outcome == 0)
accuracies.append(int(correct))
predictions.append(final_pred)
true_outcomes.append(1 if outcome >= 0.5 else 0)
mean_reward = np.mean(rewards)
mean_accuracy = np.mean(accuracies)
# Calculate additional metrics
true_positives = sum(1 for p, t in zip(predictions, true_outcomes) if p >= 0.5 and t == 1)
false_positives = sum(1 for p, t in zip(predictions, true_outcomes) if p >= 0.5 and t == 0)
true_negatives = sum(1 for p, t in zip(predictions, true_outcomes) if p < 0.5 and t == 0)
false_negatives = sum(1 for p, t in zip(predictions, true_outcomes) if p < 0.5 and t == 1)
precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0
recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0
f1_score = 2 * precision * recall / (precision + recall) if (precision + recall) > 0 else 0
logger.info(f"Evaluation results:")
logger.info(f"- Mean reward: {mean_reward:.4f}")
logger.info(f"- Prediction accuracy: {mean_accuracy:.4f}")
logger.info(f"- Precision: {precision:.4f}")
logger.info(f"- Recall: {recall:.4f}")
logger.info(f"- F1 Score: {f1_score:.4f}")
return {
'mean_reward': float(mean_reward),
'accuracy': float(mean_accuracy),
'precision': float(precision),
'recall': float(recall),
'f1_score': float(f1_score)
}
def main():
"""Main function to run the training pipeline."""
parser = argparse.ArgumentParser(description="Train a sales conversion prediction model")
parser.add_argument("--dataset", type=str, required=True,
help="Path to the dataset CSV file")
parser.add_argument("--model_path", type=str, default="models/sales_conversion_model",
help="Path to save the trained model")
parser.add_argument("--timesteps", type=int, default=50000,
help="Number of timesteps to train for")
parser.add_argument("--learning_rate", type=float, default=0.0003,
help="Learning rate for training")
parser.add_argument("--batch_size", type=int, default=64,
help="Batch size for training")
parser.add_argument("--sample_size", type=int, default=None,
help="Limit dataset size to save memory (e.g., 10000)")
parser.add_argument("--evaluate_only", action="store_true",
help="Only evaluate an existing model without training")
parser.add_argument("--num_eval_episodes", type=int, default=50,
help="Number of episodes for evaluation")
parser.add_argument("--use_small_embedding", action="store_true",
help="Use reduced embedding dimension to save memory")
args = parser.parse_args()
# Initialize trainer
trainer = SalesRLTrainer(
dataset_path=args.dataset,
model_save_path=args.model_path,
use_miniembeddings=args.use_small_embedding,
batch_size=args.batch_size
)
# Load dataset with optional sample limit
trainer.load_dataset(sample_size=args.sample_size)
# Train or evaluate
if not args.evaluate_only:
trainer.train(
total_timesteps=args.timesteps,
learning_rate=args.learning_rate
)
# Evaluate
eval_results = trainer.evaluate(num_episodes=args.num_eval_episodes)
# Print evaluation results
print("\nEvaluation Results:")
for metric, value in eval_results.items():
print(f"- {metric}: {value:.4f}")
if __name__ == "__main__":
main() |