Upload 2 files
Browse files- special_tokens_map.json +7 -0
- test.py +58 -0
special_tokens_map.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cls_token": "[CLS]",
|
| 3 |
+
"mask_token": "[MASK]",
|
| 4 |
+
"pad_token": "[PAD]",
|
| 5 |
+
"sep_token": "[SEP]",
|
| 6 |
+
"unk_token": "[UNK]"
|
| 7 |
+
}
|
test.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import optuna
|
| 2 |
+
import yaml
|
| 3 |
+
import joblib
|
| 4 |
+
import os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
from sklearn.datasets import load_iris
|
| 9 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 10 |
+
from sklearn.model_selection import train_test_split
|
| 11 |
+
from sklearn.metrics import accuracy_score
|
| 12 |
+
from optuna.visualization.matplotlib import plot_optimization_history, plot_param_importances
|
| 13 |
+
|
| 14 |
+
os.makedirs("models", exist_ok=True)
|
| 15 |
+
os.makedirs("plots", exist_ok=True)
|
| 16 |
+
|
| 17 |
+
def objective(trial):
|
| 18 |
+
n_estimators = trial.suggest_int("n_estimators", 50, 300)
|
| 19 |
+
max_depth = trial.suggest_int("max_depth", 2, 32)
|
| 20 |
+
|
| 21 |
+
data = load_iris()
|
| 22 |
+
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target)
|
| 23 |
+
|
| 24 |
+
clf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth)
|
| 25 |
+
clf.fit(X_train, y_train)
|
| 26 |
+
|
| 27 |
+
accuracy = accuracy_score(y_test, clf.predict(X_test))
|
| 28 |
+
return accuracy
|
| 29 |
+
|
| 30 |
+
study = optuna.create_study(direction="maximize")
|
| 31 |
+
study.optimize(objective, n_trials=30)
|
| 32 |
+
|
| 33 |
+
# Save trial results
|
| 34 |
+
study_df = study.trials_dataframe()
|
| 35 |
+
study_df.to_csv("models/study_trials.csv", index=False)
|
| 36 |
+
|
| 37 |
+
# Save best parameters
|
| 38 |
+
with open("models/best_params.yaml", "w") as f:
|
| 39 |
+
yaml.dump(study.best_trial.params, f)
|
| 40 |
+
|
| 41 |
+
# Train final model with best parameters
|
| 42 |
+
best_params = study.best_trial.params
|
| 43 |
+
final_model = RandomForestClassifier(**best_params)
|
| 44 |
+
data = load_iris()
|
| 45 |
+
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target)
|
| 46 |
+
final_model.fit(X_train, y_train)
|
| 47 |
+
|
| 48 |
+
# Save model
|
| 49 |
+
joblib.dump(final_model, "models/best_model.pkl")
|
| 50 |
+
|
| 51 |
+
# Save Optuna plots
|
| 52 |
+
plot_optimization_history(study)
|
| 53 |
+
plt.savefig("plots/optimization_history.png")
|
| 54 |
+
plt.clf()
|
| 55 |
+
|
| 56 |
+
plot_param_importances(study)
|
| 57 |
+
plt.savefig("plots/param_importances.png")
|
| 58 |
+
|