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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## Developer: inkbytefo
|
| 2 |
+
## Modified: 2025-11-22
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
Benchmark Comparison: Turkish vs English
|
| 6 |
+
Analyzes training curves and tests the hypothesis.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
import numpy as np
|
| 12 |
+
from scipy import stats
|
| 13 |
+
|
| 14 |
+
def load_metrics(lang="english"):
|
| 15 |
+
"""Load training metrics from JSON"""
|
| 16 |
+
filename = f"metrics_{lang}.json" if lang == "turkish" else "metrics_english.json"
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
with open(filename, 'r') as f:
|
| 20 |
+
return json.load(f)
|
| 21 |
+
except FileNotFoundError:
|
| 22 |
+
print(f"Warning: {filename} not found")
|
| 23 |
+
return None
|
| 24 |
+
|
| 25 |
+
def plot_comparison():
|
| 26 |
+
"""Plot BPC curves for Turkish vs English"""
|
| 27 |
+
en = load_metrics("english")
|
| 28 |
+
tr = load_metrics("turkish")
|
| 29 |
+
|
| 30 |
+
if not en or not tr:
|
| 31 |
+
print("Missing metrics files. Run both training scripts first.")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
plt.figure(figsize=(12, 6))
|
| 35 |
+
|
| 36 |
+
# Subplot 1: Training BPC
|
| 37 |
+
plt.subplot(1, 2, 1)
|
| 38 |
+
plt.plot(en["steps"], en["train_bpc"], label="English (enwik8)", alpha=0.7)
|
| 39 |
+
plt.plot(tr["steps"], tr["train_bpc"], label="Turkish (trwiki)", alpha=0.7)
|
| 40 |
+
plt.xlabel("Training Steps")
|
| 41 |
+
plt.ylabel("BPC (Bits Per Character)")
|
| 42 |
+
plt.title("Training BPC: Turkish vs English")
|
| 43 |
+
plt.legend()
|
| 44 |
+
plt.grid(True, alpha=0.3)
|
| 45 |
+
|
| 46 |
+
# Subplot 2: Validation BPC
|
| 47 |
+
plt.subplot(1, 2, 2)
|
| 48 |
+
|
| 49 |
+
# Val BPC is logged every 200 steps
|
| 50 |
+
val_steps_en = [i * 200 for i in range(len(en["val_bpc"]))]
|
| 51 |
+
val_steps_tr = [i * 200 for i in range(len(tr["val_bpc"]))]
|
| 52 |
+
|
| 53 |
+
plt.plot(val_steps_en, en["val_bpc"], label="English (enwik8)", marker='o', alpha=0.7)
|
| 54 |
+
plt.plot(val_steps_tr, tr["val_bpc"], label="Turkish (trwiki)", marker='s', alpha=0.7)
|
| 55 |
+
plt.xlabel("Training Steps")
|
| 56 |
+
plt.ylabel("Validation BPC")
|
| 57 |
+
plt.title("Validation BPC: Turkish vs English")
|
| 58 |
+
plt.legend()
|
| 59 |
+
plt.grid(True, alpha=0.3)
|
| 60 |
+
|
| 61 |
+
plt.tight_layout()
|
| 62 |
+
plt.savefig("comparison_turkish_vs_english.png", dpi=300)
|
| 63 |
+
print("Saved comparison plot to comparison_turkish_vs_english.png")
|
| 64 |
+
plt.close()
|
| 65 |
+
|
| 66 |
+
def statistical_test():
|
| 67 |
+
"""Perform statistical significance test"""
|
| 68 |
+
en = load_metrics("english")
|
| 69 |
+
tr = load_metrics("turkish")
|
| 70 |
+
|
| 71 |
+
if not en or not tr:
|
| 72 |
+
return
|
| 73 |
+
|
| 74 |
+
# Final BPC values
|
| 75 |
+
final_bpc_en = en["val_bpc"][-1]
|
| 76 |
+
final_bpc_tr = tr["val_bpc"][-1]
|
| 77 |
+
|
| 78 |
+
print("\n" + "=" * 60)
|
| 79 |
+
print("STATISTICAL COMPARISON")
|
| 80 |
+
print("=" * 60)
|
| 81 |
+
|
| 82 |
+
print(f"\nFinal Validation BPC:")
|
| 83 |
+
print(f" English (enwik8): {final_bpc_en:.4f}")
|
| 84 |
+
print(f" Turkish (trwiki): {final_bpc_tr:.4f}")
|
| 85 |
+
print(f" Difference: {final_bpc_en - final_bpc_tr:.4f}")
|
| 86 |
+
|
| 87 |
+
# Convergence speed (steps to reach 2.5 BPC)
|
| 88 |
+
threshold = 2.5
|
| 89 |
+
|
| 90 |
+
steps_to_threshold_en = next((s for s, bpc in zip(en["steps"], en["train_bpc"]) if bpc < threshold), None)
|
| 91 |
+
steps_to_threshold_tr = next((s for s, bpc in zip(tr["steps"], tr["train_bpc"]) if bpc < threshold), None)
|
| 92 |
+
|
| 93 |
+
print(f"\nSteps to reach BPC < {threshold}:")
|
| 94 |
+
print(f" English: {steps_to_threshold_en if steps_to_threshold_en else 'Not reached'}")
|
| 95 |
+
print(f" Turkish: {steps_to_threshold_tr if steps_to_threshold_tr else 'Not reached'}")
|
| 96 |
+
|
| 97 |
+
# Hypothesis test
|
| 98 |
+
print("\n" + "-" * 60)
|
| 99 |
+
print("HYPOTHESIS TEST")
|
| 100 |
+
print("-" * 60)
|
| 101 |
+
|
| 102 |
+
if final_bpc_tr < final_bpc_en:
|
| 103 |
+
print("✅ HYPOTHESIS CONFIRMED")
|
| 104 |
+
print("Turkish model achieved lower BPC than English model.")
|
| 105 |
+
print("This supports the claim that byte-level models are more")
|
| 106 |
+
print("efficient for agglutinative languages.")
|
| 107 |
+
improvement = ((final_bpc_en - final_bpc_tr) / final_bpc_en) * 100
|
| 108 |
+
print(f"Improvement: {improvement:.2f}%")
|
| 109 |
+
else:
|
| 110 |
+
print("❌ HYPOTHESIS REJECTED")
|
| 111 |
+
print("English model achieved lower or equal BPC.")
|
| 112 |
+
|
| 113 |
+
print("=" * 60)
|
| 114 |
+
|
| 115 |
+
def generate_report():
|
| 116 |
+
"""Generate markdown report"""
|
| 117 |
+
en = load_metrics("english")
|
| 118 |
+
tr = load_metrics("turkish")
|
| 119 |
+
|
| 120 |
+
if not en or not tr:
|
| 121 |
+
return
|
| 122 |
+
|
| 123 |
+
report = f"""# Kaşgarlı Testi - Benchmark Results
|
| 124 |
+
|
| 125 |
+
## Hypothesis
|
| 126 |
+
**H1:** Byte-level models learn agglutinative languages (Turkish) more efficiently than analytic languages (English).
|
| 127 |
+
|
| 128 |
+
## Experimental Setup
|
| 129 |
+
- **Model:** AGIFORMER (identical architecture, 50M parameters)
|
| 130 |
+
- **Hyperparameters:** Same for both (d_model=512, n_layers=6, thinking_steps=3)
|
| 131 |
+
- **Training:** 5000 steps, batch_size=4, lr=3e-4
|
| 132 |
+
- **English Dataset:** enwik8 (100MB Wikipedia)
|
| 133 |
+
- **Turkish Dataset:** trwiki (Turkish Wikipedia)
|
| 134 |
+
|
| 135 |
+
## Results
|
| 136 |
+
|
| 137 |
+
### Final BPC (Lower is Better)
|
| 138 |
+
| Language | Validation BPC |
|
| 139 |
+
|----------|----------------|
|
| 140 |
+
| English | {en["val_bpc"][-1]:.4f} |
|
| 141 |
+
| Turkish | {tr["val_bpc"][-1]:.4f} |
|
| 142 |
+
|
| 143 |
+
**Difference:** {abs(en["val_bpc"][-1] - tr["val_bpc"][-1]):.4f} BPC
|
| 144 |
+
|
| 145 |
+
### Convergence Speed
|
| 146 |
+
Steps to reach BPC < 2.5:
|
| 147 |
+
- English: {next((s for s, bpc in zip(en["steps"], en["train_bpc"]) if bpc < 2.5), "Not reached")}
|
| 148 |
+
- Turkish: {next((s for s, bpc in zip(tr["steps"], tr["train_bpc"]) if bpc < 2.5), "Not reached")}
|
| 149 |
+
|
| 150 |
+
## Conclusion
|
| 151 |
+
|
| 152 |
+
{"Turkish model outperformed English, confirming the hypothesis." if tr["val_bpc"][-1] < en["val_bpc"][-1] else "Hypothesis not confirmed in this experiment."}
|
| 153 |
+
|
| 154 |
+
## Visualization
|
| 155 |
+

|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
**Generated:** 2025-11-22
|
| 159 |
+
**Experimenter:** inkbytefo
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
with open("benchmark_report.md", "w") as f:
|
| 163 |
+
f.write(report)
|
| 164 |
+
|
| 165 |
+
print("\nGenerated benchmark_report.md")
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
plot_comparison()
|
| 169 |
+
statistical_test()
|
| 170 |
+
generate_report()
|