Spaces:
Sleeping
Sleeping
Upload sainya_backend.py with huggingface_hub
Browse files- sainya_backend.py +23 -11
sainya_backend.py
CHANGED
|
@@ -76,35 +76,47 @@ def train_model(decisions: WarDecisions) -> dict:
|
|
| 76 |
}
|
| 77 |
|
| 78 |
def get_dl_explanation(decisions: WarDecisions, result: dict) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
explanations = {
|
| 80 |
"attack_formation": {
|
| 81 |
-
"war": f"You deployed warriors in {
|
| 82 |
-
"dl": f"Batch Size = {
|
| 83 |
"math": "w = w - η · ∇L(w; x_batch)"
|
| 84 |
},
|
| 85 |
"advance_speed": {
|
| 86 |
-
"war": f"Your army advanced at {
|
| 87 |
-
"dl": f"Learning Rate η = {
|
| 88 |
"math": "η controls step size: θ_{t+1} = θ_t - η · ∇J(θ)"
|
| 89 |
},
|
| 90 |
"command_layers": {
|
| 91 |
-
"war": f"Your army had {decisions.command_layers} {'layer' if decisions.command_layers==1 else 'layers'} of command hierarchy",
|
| 92 |
-
"dl": f"Hidden Layers = {decisions.command_layers}. Architecture: {result['params']['hidden_layer_sizes']}. {
|
| 93 |
"math": "h^(l) = σ(W^(l) · h^(l-1) + b^(l))"
|
| 94 |
},
|
| 95 |
"supply_discipline": {
|
| 96 |
-
"war": f"{
|
| 97 |
-
"dl": f"L2 Regularization α = {result['params']['alpha']}. Adds penalty α||w||² to loss. {
|
| 98 |
"math": "L_total = L_CE + α·||w||²"
|
| 99 |
},
|
| 100 |
"war_drills": {
|
| 101 |
"war": f"Your army trained for {decisions.war_drills} drills before battle",
|
| 102 |
-
"dl": f"Max Epochs = {decisions.war_drills}. Model actually trained for {result['iterations']} iterations. {
|
| 103 |
"math": "One epoch = full pass through training data"
|
| 104 |
},
|
| 105 |
"retreat_strategy": {
|
| 106 |
-
"war":
|
| 107 |
-
"dl": f"Early Stopping = {'ON' if decisions.retreat_strategy else 'OFF'}. {
|
| 108 |
"math": "Stop if val_loss doesn't improve for n_iter_no_change steps"
|
| 109 |
}
|
| 110 |
}
|
|
|
|
| 76 |
}
|
| 77 |
|
| 78 |
def get_dl_explanation(decisions: WarDecisions, result: dict) -> dict:
|
| 79 |
+
lr_val = {"slow": 0.001, "medium": 0.01, "fast": 0.1}[decisions.advance_speed]
|
| 80 |
+
batch_val = [32, 64, 128][decisions.attack_formation - 1]
|
| 81 |
+
formation_desc = "tight formation (32)" if decisions.attack_formation == 1 else "standard formation (64)" if decisions.attack_formation == 2 else "spread formation (128)"
|
| 82 |
+
batch_desc = "Small batches = noisy gradient updates, better exploration, escape local minima." if decisions.attack_formation == 1 else "Medium batches = balanced stability and exploration." if decisions.attack_formation == 2 else "Large batches = stable but may converge to sharp minima."
|
| 83 |
+
speed_desc = "slow, precise speed" if decisions.advance_speed == "slow" else "standard marching speed" if decisions.advance_speed == "medium" else "aggressive full speed"
|
| 84 |
+
lr_desc = "Too slow = takes forever, too fast = overshoots optimal weights." if decisions.advance_speed == "fast" else "Good choice for stable convergence." if decisions.advance_speed == "medium" else "Precise but slow convergence."
|
| 85 |
+
layers_desc = "Shallow = simple patterns only." if decisions.command_layers == 1 else "Deep = complex feature extraction, risk of vanishing gradients." if decisions.command_layers == 3 else "Balanced depth."
|
| 86 |
+
discipline_label = "Strict" if decisions.supply_discipline == "strict" else "Moderate" if decisions.supply_discipline == "moderate" else "Loose"
|
| 87 |
+
discipline_desc = "Strong regularization = simpler model, less overfitting." if decisions.supply_discipline == "strict" else "Balanced regularization." if decisions.supply_discipline == "moderate" else "Weak regularization = risk of overfitting."
|
| 88 |
+
drills_desc = "Undertrained = underfitting." if decisions.war_drills == 50 else "Well trained." if decisions.war_drills == 150 else "Risk of overfitting if early stopping not used."
|
| 89 |
+
retreat_war = "Retreat strategy ACTIVE — army pulled back when weakening" if decisions.retreat_strategy else "No retreat — army fought until the end"
|
| 90 |
+
retreat_dl = "Monitors validation loss. Stops training when model stops improving. Prevents overfitting." if decisions.retreat_strategy else "Trained for full epochs. Risk of overfitting on training data."
|
| 91 |
explanations = {
|
| 92 |
"attack_formation": {
|
| 93 |
+
"war": f"You deployed warriors in {formation_desc}",
|
| 94 |
+
"dl": f"Batch Size = {batch_val}. {batch_desc}",
|
| 95 |
"math": "w = w - η · ∇L(w; x_batch)"
|
| 96 |
},
|
| 97 |
"advance_speed": {
|
| 98 |
+
"war": f"Your army advanced at {speed_desc}",
|
| 99 |
+
"dl": f"Learning Rate η = {lr_val}. {lr_desc}",
|
| 100 |
"math": "η controls step size: θ_{t+1} = θ_t - η · ∇J(θ)"
|
| 101 |
},
|
| 102 |
"command_layers": {
|
| 103 |
+
"war": f"Your army had {decisions.command_layers} {'layer' if decisions.command_layers == 1 else 'layers'} of command hierarchy",
|
| 104 |
+
"dl": f"Hidden Layers = {decisions.command_layers}. Architecture: {result['params']['hidden_layer_sizes']}. {layers_desc}",
|
| 105 |
"math": "h^(l) = σ(W^(l) · h^(l-1) + b^(l))"
|
| 106 |
},
|
| 107 |
"supply_discipline": {
|
| 108 |
+
"war": f"{discipline_label} resource discipline across your kingdom",
|
| 109 |
+
"dl": f"L2 Regularization α = {result['params']['alpha']}. Adds penalty α||w||² to loss. {discipline_desc}",
|
| 110 |
"math": "L_total = L_CE + α·||w||²"
|
| 111 |
},
|
| 112 |
"war_drills": {
|
| 113 |
"war": f"Your army trained for {decisions.war_drills} drills before battle",
|
| 114 |
+
"dl": f"Max Epochs = {decisions.war_drills}. Model actually trained for {result['iterations']} iterations. {drills_desc}",
|
| 115 |
"math": "One epoch = full pass through training data"
|
| 116 |
},
|
| 117 |
"retreat_strategy": {
|
| 118 |
+
"war": retreat_war,
|
| 119 |
+
"dl": f"Early Stopping = {'ON' if decisions.retreat_strategy else 'OFF'}. {retreat_dl}",
|
| 120 |
"math": "Stop if val_loss doesn't improve for n_iter_no_change steps"
|
| 121 |
}
|
| 122 |
}
|