jeevan0704/sequential-model-for-sequential_dataset
0
1"""2Utility functions for sequence labeling model3"""4import torch5import numpy as np6import random7import os8from sklearn.metrics import accuracy_score, f1_score, classification_report, confusion_matrix9 10 11def set_seed(seed):12 """Set random seeds for reproducibility"""13 random.seed(seed)14 np.random.seed(seed)15 torch.manual_seed(seed)16 if torch.cuda.is_available():17 torch.cuda.manual_seed(seed)18 torch.cuda.manual_seed_all(seed)19 torch.backends.cudnn.deterministic = True20 torch.backends.cudnn.benchmark = False21 22 23def create_directories(dirs):24 """Create directories if they don't exist"""25 for dir_path in dirs:26 os.makedirs(dir_path, exist_ok=True)27 28 29def save_checkpoint(model, optimizer, epoch, val_loss, filepath):30 """Save model checkpoint"""31 checkpoint = {32 'epoch': epoch,33 'model_state_dict': model.state_dict(),34 'optimizer_state_dict': optimizer.state_dict(),35 'val_loss': val_loss36 }37 torch.save(checkpoint, filepath)38 print(f"Checkpoint saved to {filepath}")39 40 41def load_checkpoint(filepath, model, optimizer=None):42 """Load model checkpoint"""43 checkpoint = torch.load(filepath, map_location='cpu')44 model.load_state_dict(checkpoint['model_state_dict'])45 if optimizer is not None:46 optimizer.load_state_dict(checkpoint['optimizer_state_dict'])47 epoch = checkpoint['epoch']48 val_loss = checkpoint['val_loss']49 print(f"Checkpoint loaded from {filepath} (epoch {epoch}, val_loss {val_loss:.4f})")50 return epoch, val_loss51 52 53def pad_sequences(sequences, max_length, padding_value=0):54 """55 Pad sequences to max_length56 57 Args:58 sequences: List of sequences (lists or arrays)59 max_length: Maximum length to pad to60 padding_value: Value to use for padding61 62 Returns:63 Padded numpy array of shape (num_sequences, max_length)64 """65 padded = np.full((len(sequences), max_length), padding_value, dtype=np.int32)66 for i, seq in enumerate(sequences):67 length = min(len(seq), max_length)68 padded[i, :length] = seq[:length]69 return padded70 71 72def create_padding_mask(sequences, max_length):73 """74 Create padding mask for sequences75 76 Args:77 sequences: List of sequences78 max_length: Maximum sequence length79 80 Returns:81 Boolean mask array where True indicates valid positions82 """83 mask = np.zeros((len(sequences), max_length), dtype=bool)84 for i, seq in enumerate(sequences):85 length = min(len(seq), max_length)86 mask[i, :length] = True87 return mask88 89 90def calculate_metrics(predictions, targets, mask=None):91 """92 Calculate accuracy and F1 score with optional masking93 94 Args:95 predictions: Predicted labels (flattened)96 targets: True labels (flattened)97 mask: Optional boolean mask for valid positions98 99 Returns:100 Dictionary with accuracy and F1 scores101 """102 if mask is not None:103 predictions = predictions[mask]104 targets = targets[mask]105 106 accuracy = accuracy_score(targets, predictions)107 f1_macro = f1_score(targets, predictions, average='macro', zero_division=0)108 f1_weighted = f1_score(targets, predictions, average='weighted', zero_division=0)109 110 return {111 'accuracy': accuracy,112 'f1_macro': f1_macro,113 'f1_weighted': f1_weighted114 }115 116 117def get_classification_report(predictions, targets, mask=None, target_names=None):118 """119 Generate classification report with optional masking120 121 Args:122 predictions: Predicted labels (flattened)123 targets: True labels (flattened)124 mask: Optional boolean mask for valid positions125 target_names: Optional list of class names126 127 Returns:128 Classification report string129 """130 if mask is not None:131 predictions = predictions[mask]132 targets = targets[mask]133 134 return classification_report(targets, predictions, target_names=target_names, zero_division=0)135 136 137def get_confusion_matrix(predictions, targets, mask=None):138 """139 Generate confusion matrix with optional masking140 141 Args:142 predictions: Predicted labels (flattened)143 targets: True labels (flattened)144 mask: Optional boolean mask for valid positions145 146 Returns:147 Confusion matrix148 """149 if mask is not None:150 predictions = predictions[mask]151 targets = targets[mask]152 153 return confusion_matrix(targets, predictions)154 155 156def count_parameters(model):157 """Count trainable parameters in model"""158 return sum(p.numel() for p in model.parameters() if p.requires_grad)159 160 161def format_time(seconds):162 """Format seconds into readable time string"""163 hours = int(seconds // 3600)164 minutes = int((seconds % 3600) // 60)165 secs = int(seconds % 60)166 167 if hours > 0:168 return f"{hours}h {minutes}m {secs}s"169 elif minutes > 0:170 return f"{minutes}m {secs}s"171 else:172 return f"{secs}s"173 