-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurriculum_implementation.py
More file actions
152 lines (122 loc) · 5.44 KB
/
curriculum_implementation.py
File metadata and controls
152 lines (122 loc) · 5.44 KB
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
# curriculum_implementation.py
import json
import torch
import torch.optim as optim
import torch.nn as nn
from torch.utils.data import DataLoader
import importlib
def setup_training_framework(config_path):
"""
Initialize the training framework using a provided configuration file.
Parameters:
- config_path: str, path to the configuration file for the training framework.
Returns:
- framework: dict, initialized framework with necessary settings.
"""
try:
# Load configuration settings
with open(config_path, 'r') as config_file:
framework_config = json.load(config_file)
# Dynamically import model class
model_class = framework_config.get("model_class")
if isinstance(model_class, str):
module_name, class_name = model_class.rsplit('.', 1)
model_class = getattr(importlib.import_module(module_name), class_name)
# Initialize model
model = model_class(**framework_config.get("model_params", {}))
# Initialize optimizer
optimizer_class = getattr(optim, framework_config["optimizer"])
optimizer = optimizer_class(model.parameters(), **framework_config.get("optimizer_params", {}))
# Initialize loss function
loss_function_class = getattr(nn, framework_config["loss_function"])
loss_function = loss_function_class(**framework_config.get("loss_params", {}))
# Initialize dataset and dataloader
dataset_class = framework_config["dataset"]
if isinstance(dataset_class, str):
if '.' in dataset_class:
module_name, class_name = dataset_class.rsplit('.', 1)
dataset_class = getattr(importlib.import_module(module_name), class_name)
else:
raise ValueError(f"Invalid dataset class format: {dataset_class}. Must be fully qualified.")
dataset = dataset_class(**framework_config.get("dataset_params", {}))
dataloader = DataLoader(dataset, **framework_config.get("dataloader_params", {}))
framework = {
"model": model,
"optimizer": optimizer,
"loss_function": loss_function,
"dataloader": dataloader,
"metrics": framework_config.get("metrics"),
}
print("Training framework initialized successfully.")
return framework
except Exception as e:
print(f"Error setting up training framework: {e}")
raise
def train_with_curriculum(framework, curriculum):
"""
Train a model using the provided curriculum.
Parameters:
- framework: dict, initialized training framework.
- curriculum: list of dicts, stages of the curriculum with settings like noise levels, epochs, etc.
Returns:
- results: dict, training results including metrics and performance summaries.
"""
try:
model = framework["model"]
optimizer = framework["optimizer"]
loss_function = framework["loss_function"]
dataloader = framework["dataloader"]
results = {
"stages": [],
"overall_metrics": {}
}
# Training loop over curriculum stages
for stage in curriculum:
print(f"Starting training stage {stage['stage_id']}...")
noise_level = stage["noise_level"]
epochs = stage["epochs"]
# Adjust dataset or dataloader if required by noise_level
if hasattr(dataloader.dataset, 'set_noise_level'):
dataloader.dataset.set_noise_level(noise_level)
# Training for the current stage
for epoch in range(epochs):
model.train()
epoch_loss = 0.0
correct = 0
total = 0
for inputs, targets in dataloader:
optimizer.zero_grad()
outputs = model(inputs)
loss = loss_function(outputs, targets)
loss.backward()
optimizer.step()
epoch_loss += loss.item() * inputs.size(0)
if outputs.dim() == 1: # Regression outputs
predicted = outputs
else: # Classification outputs
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
print(f"Epoch {epoch + 1}/{epochs}: Loss = {epoch_loss / total}, Accuracy = {correct / total}")
# Record stage results
stage_result = {
"stage_id": stage["stage_id"],
"noise_level": noise_level,
"epochs": epochs,
"metrics": {
"loss": epoch_loss / total,
"accuracy": correct / total,
}
}
print(f"Completed training stage {stage['stage_id']} with metrics: {stage_result['metrics']}")
results["stages"].append(stage_result)
# Aggregate overall metrics
results["overall_metrics"] = {
"average_loss": sum(stage["metrics"]["loss"] for stage in results["stages"]) / len(results["stages"]),
"average_accuracy": sum(stage["metrics"]["accuracy"] for stage in results["stages"]) / len(results["stages"]),
}
print("Training completed successfully.")
return results
except Exception as e:
print(f"Error during training with curriculum: {e}")
raise