File size: 13,945 Bytes
5335c72
 
 
 
 
 
3cad58d
a1de8ed
5335c72
 
 
3cad58d
5335c72
 
 
 
 
 
 
 
a1de8ed
 
5335c72
a1de8ed
5335c72
 
 
a1de8ed
 
 
 
5335c72
3cad58d
5335c72
 
 
 
 
 
 
 
 
3cad58d
5335c72
 
 
 
 
 
 
 
 
 
 
 
3cad58d
5335c72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cad58d
5335c72
 
 
 
 
a1de8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5335c72
 
 
a1de8ed
3cad58d
6027c2f
 
a1de8ed
6027c2f
 
 
5e178e0
 
5335c72
 
 
 
575bea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5335c72
 
a1de8ed
5335c72
 
 
3cad58d
575bea3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cad58d
575bea3
 
3cad58d
5335c72
575bea3
5335c72
 
3cad58d
5335c72
 
575bea3
 
 
 
 
 
 
 
 
 
a1de8ed
5335c72
575bea3
 
5335c72
 
 
 
 
a1de8ed
 
 
 
 
 
5335c72
 
 
 
 
a1de8ed
 
 
 
 
 
 
 
 
 
3cad58d
 
5335c72
3cad58d
 
a1de8ed
3cad58d
 
 
a1de8ed
3cad58d
a1de8ed
 
3cad58d
a1de8ed
3cad58d
 
a1de8ed
3cad58d
a1de8ed
5335c72
a1de8ed
5335c72
 
 
 
 
 
 
 
a1de8ed
 
3cad58d
 
5335c72
a1de8ed
 
 
 
 
 
 
5335c72
a1de8ed
 
5335c72
a1de8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5335c72
a1de8ed
 
 
 
3cad58d
a1de8ed
 
 
3cad58d
5335c72
 
a1de8ed
5335c72
 
a1de8ed
5335c72
a1de8ed
 
5335c72
 
 
a1de8ed
 
5335c72
 
 
 
a1de8ed
 
5335c72
a1de8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# active_loop_full.py
import os, math, json, time
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
from typing import List, Optional
from datasets import Dataset, load_dataset
from transformers import (
    RobertaForSequenceClassification,
    PreTrainedTokenizerFast,
    AutoTokenizer,
    TrainingArguments,
    Trainer,
)
from peft import LoraConfig, get_peft_model
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
from huggingface_hub import HfApi


# ============================================================
# 基本參數
# ============================================================
OUTPUT_ROOT = "./lora_w_mmd"
os.makedirs(OUTPUT_ROOT, exist_ok=True)


# ============================================================
# 工具函式
# ============================================================
def _rbf_mmd(X: torch.Tensor, Y: torch.Tensor, sigma: float) -> float:
    """RBF kernel MMD"""
    def k(a,b,s):
        d = torch.cdist(a,b,p=2)**2
        return torch.exp(-d/(2*s**2))
    Kxx = k(X,X,sigma).mean()
    Kyy = k(Y,Y,sigma).mean()
    Kxy = k(X,Y,sigma).mean()
    return (Kxx + Kyy - 2*Kxy).item()

def _sigma_median(X: np.ndarray) -> float:
    """pairwise 距離中位數當作 sigma"""
    if len(X) > 2000:
        X = X[np.random.choice(len(X), 2000, replace=False)]
    D = cdist(X, X)
    D = D[np.triu_indices_from(D, k=1)]
    med = float(np.median(D) if np.isfinite(D).all() and len(D) else 1.0)
    return max(med, 1e-3)

def _density_uncertainty_sampling(df: pd.DataFrame,
                                  emb_cols: List[str],
                                  prob_cols: List[str],
                                  num_clusters: int = 10,
                                  total_samples: int = 200) -> pd.DataFrame:
    """cluster 後在各 cluster 內用 density*entropy 排序取樣"""
    if len(df) <= total_samples:
        return df.copy()

    embs = df[emb_cols].to_numpy()
    km = KMeans(n_clusters=num_clusters, random_state=42).fit(embs)
    df = df.copy()
    df["cluster"] = km.labels_

    out = []
    sizes = df["cluster"].value_counts().sort_index()
    alloc = (sizes / sizes.sum() * total_samples).astype(int).clip(lower=1)

    for c, g in df.groupby("cluster"):
        ge = g[emb_cols].to_numpy()
        dist = cdist(ge, ge)
        np.fill_diagonal(dist, np.nan)
        density = 1.0 / (np.nanmean(dist, axis=1) + 1e-8)

        if len(prob_cols) > 0:
            probs = g[prob_cols].to_numpy()
            ent = -np.sum(probs * np.log(probs + 1e-12), axis=1)
        else:
            ent = np.ones(len(g), dtype=float)

        score = density * ent
        k = min(len(g), max(1, int(alloc.get(c, 1))))
        idx = np.argsort(score)[-k:]
        out.append(g.iloc[idx])

    return pd.concat(out, ignore_index=True).drop(columns=["cluster"])


# ============================================================
# LoRA 建構與訓練
# ============================================================
def _build_lora_model(base_repo: str, tokenizer, hf_token: str):
    base = RobertaForSequenceClassification.from_pretrained(
        base_repo, token=hf_token, ignore_mismatched_sizes=True
    )
    base.resize_token_embeddings(len(tokenizer))
    cfg = LoraConfig(
        r=8, lora_alpha=16, lora_dropout=0.1,
        target_modules=["query", "value"], bias="none", task_type="SEQ_CLS"
    )
    return get_peft_model(base, cfg)


def _to_hf_ds(df: pd.DataFrame) -> Dataset:
    return Dataset.from_pandas(df.reset_index(drop=True))


def _tokenize_texts(tokenizer, ds: Dataset) -> Dataset:
    ds = ds.map(
        lambda e: tokenizer(
            e["text"], truncation=True, padding="max_length", max_length=512
        ),
        batched=True
    )
    if "label_id" in ds.column_names:
        ds = ds.rename_column("label_id", "labels")
    ds.set_format(type="torch", columns=["input_ids", "attention_mask", "labels"])
    return ds


# def _train_one_lora(rnd: int,
#                     base_repo: str,
#                     tokenizer,
#                     train_df: pd.DataFrame,
#                     eval_df: Optional[pd.DataFrame],
#                     output_dir: str,
#                     hf_token: str) -> str:
#     model = _build_lora_model(base_repo, tokenizer, hf_token)
#     train_ds = _to_hf_ds(train_df[["text","label_id"]])
#     train_ds = _tokenize_texts(tokenizer, train_ds)
#     eval_ds = None
#     if eval_df is not None and len(eval_df):
#         eval_ds = _to_hf_ds(eval_df[["text","label_id"]])
#         eval_ds = _tokenize_texts(tokenizer, eval_ds)

#     args = TrainingArguments(
#         output_dir=output_dir,
#         per_device_train_batch_size=16,
#         num_train_epochs=3,
#         logging_steps=20,
#         save_strategy="epoch",
#         evaluation_strategy="epoch" if eval_ds is not None else "no",
#         report_to="none",
#         remove_unused_columns=False,
#     )
#     trainer = Trainer(model=model, args=args, train_dataset=train_ds,
#                       eval_dataset=eval_ds, tokenizer=tokenizer)
#     trainer.train()
#     model.save_pretrained(output_dir)
#     tokenizer.save_pretrained(output_dir)
#     return output_dir
def _train_one_lora(rnd: int,
                    base_repo: str,
                    tokenizer,
                    train_df: pd.DataFrame,
                    eval_df: Optional[pd.DataFrame],
                    output_dir: str,
                    hf_token: str) -> str:
    from transformers import TrainingArguments, Trainer, RobertaForSequenceClassification

    def make_training_args(output_dir: str, has_eval: bool):
        """
        嘗試用新參數,失敗就退回舊版 API(用 do_eval / eval_steps / save_steps)。
        """
        try:
            # 新版 transformers(支援 evaluation_strategy / save_strategy / report_to / remove_unused_columns)
            return TrainingArguments(
                output_dir=output_dir,
                per_device_train_batch_size=16,
                per_device_eval_batch_size=16,
                num_train_epochs=3,
                logging_steps=20,
                save_strategy="epoch",
                evaluation_strategy="epoch" if has_eval else "no",
                load_best_model_at_end=bool(has_eval),
                metric_for_best_model="eval_loss",
                report_to="none",
                remove_unused_columns=False,
            )
        except TypeError:
            # 舊版 transformers:移除不支援參數,用 do_eval + eval_steps/save_steps 取代
            kwargs = dict(
                output_dir=output_dir,
                per_device_train_batch_size=16,
                per_device_eval_batch_size=16,
                num_train_epochs=3,
                logging_steps=20,
            )
            if has_eval:
                kwargs["do_eval"] = True
                kwargs["eval_steps"] = 200
                kwargs["save_steps"] = 200
            else:
                kwargs["do_eval"] = False
                kwargs["save_steps"] = 500
            return TrainingArguments(**kwargs)

    # 1) 構建 LoRA 包裝後的模型
    model = _build_lora_model(base_repo, tokenizer, hf_token)

    # 2) 準備 Dataset
    train_ds = _to_hf_ds(train_df[["text","label_id"]])
    train_ds = _tokenize_texts(tokenizer, train_ds)

    eval_ds = None
    if eval_df is not None and len(eval_df):
        eval_ds = _to_hf_ds(eval_df[["text","label_id"]])
        eval_ds = _tokenize_texts(tokenizer, eval_ds)

    # 3) 建立相容的 TrainingArguments
    args = make_training_args(output_dir, has_eval=eval_ds is not None)

    # 4) 訓練
    trainer = Trainer(
        model=model,
        args=args,
        train_dataset=train_ds,
        eval_dataset=eval_ds,
        tokenizer=tokenizer
    )
    trainer.train()

    # 5) 儲存
    model.save_pretrained(output_dir)
    tokenizer.save_pretrained(output_dir)
    return output_dir


# ============================================================
# 主流程:從 HF Dataset 載入 embedding + 產生人工標註集
# ============================================================
def active_learning_loop_from_hub(
    dataset_repo: str,
    split: str,
    base_repo: str,
    tokenizer_repo: str,
    hf_token: str,
    batch_size: int = 1000,
    mmd_threshold: float = 0.2,
    entropy_threshold: float = 1.1
):
    print(f"🚀 Loading dataset {dataset_repo} [{split}] ...")
    ds = load_dataset(dataset_repo, split=split, token=hf_token)
    df = pd.DataFrame(ds)

    if "embedding" not in df.columns or "label" not in df.columns:
        raise ValueError("Dataset 必須包含 'embedding' 和 'label' 欄位")

    # === label & embedding 展開 ===
    labels_sorted = sorted(df["label"].unique().tolist())
    label2id = {l:i for i,l in enumerate(labels_sorted)}
    df["label_id"] = df["label"].map(label2id)
    emb_dim = len(df["embedding"].iloc[0])
    emb_cols = [f"emb_{i}" for i in range(emb_dim)]
    emb_matrix = np.stack(df["embedding"].to_numpy())
    emb_df = pd.DataFrame(emb_matrix, columns=emb_cols, index=df.index)
    df = pd.concat([df.drop(columns=["embedding"]), emb_df], axis=1)

    # 展開 probs(如果有)
    prob_cols: List[str] = []
    if "probs" in df.columns:
        prob_dim = len(df["probs"].iloc[0])
        prob_cols = [f"prob_{i}" for i in range(prob_dim)]
        prob_df = pd.DataFrame(np.stack(df["probs"].to_numpy()), columns=prob_cols, index=df.index)
        df = pd.concat([df.drop(columns=["probs"]), prob_df], axis=1)

    # tokenizer
    tok_src = tokenizer_repo or base_repo
    tokenizer = PreTrainedTokenizerFast.from_pretrained(tok_src, token=hf_token)

    # baseline 分佈
    baseline_dist = df[emb_cols].to_numpy()
    sigma_base = _sigma_median(baseline_dist)
    baseline_dist_t = torch.tensor(baseline_dist, dtype=torch.float32)

    indices = list(range(len(df)))
    tag_stamp = time.strftime("%Y%m%d-%H%M%S")
    last_saved_dir = None

    for rnd, idxs in enumerate(range(0, len(df), batch_size), start=1):
        batch_df = df.iloc[idxs:idxs+batch_size].copy()
        if len(batch_df) == 0:
            continue

        # === 熵值計算 ===
        if len(prob_cols) > 0:
            probs = batch_df[prob_cols].to_numpy()
            entropies = -np.sum(probs * np.log(probs + 1e-12), axis=1)
            mean_entropy = float(np.mean(entropies))
        else:
            mean_entropy = 0.0

        # === MMD ===
        X = torch.tensor(batch_df[emb_cols].to_numpy(), dtype=torch.float32)
        mmd_val = _rbf_mmd(X, baseline_dist_t, sigma_base)
        print(f"[AL] Round {rnd}: mean_entropy={mean_entropy:.3f}, MMD={mmd_val:.4f}")

        # === 判斷是否需要人工標註 ===
        if mean_entropy > entropy_threshold or mmd_val > mmd_threshold:
            print("⚠️ 高不確定性:輸出人工標註樣本集")
            sampled = _density_uncertainty_sampling(
                batch_df, emb_cols, prob_cols, num_clusters=10,
                total_samples=min(200, len(batch_df))
            )
            label_file = os.path.join(OUTPUT_ROOT, f"selected_for_labeling_round_{rnd}.json")
            sampled[["source", "label"] + emb_cols].to_json(
                label_file, orient="records", indent=2, force_ascii=False
            )
            print(f"📝 已輸出 {len(sampled)} 筆待人工標註樣本 → {label_file}")
            continue  # 暫不訓練,等人工標註

        # === 若未超標,直接 LoRA 微調 ===
        print(f"✅ 使用此批資料進行微調 (round {rnd})")
        if "text" not in batch_df.columns:
            batch_df["text"] = "<asm>"

        eval_size = max(1, int(0.2 * len(batch_df)))
        eval_df = batch_df.iloc[:eval_size]
        train_df = batch_df.iloc[eval_size:]

        out_dir = os.path.join(OUTPUT_ROOT, f"round_{rnd}")
        os.makedirs(out_dir, exist_ok=True)
        _train_one_lora(rnd, base_repo, tokenizer, train_df, eval_df, out_dir, hf_token)
        last_saved_dir = out_dir

    # === 上傳與打 tag ===
    if last_saved_dir is None:
        print("⚠️ 尚未進行 LoRA 訓練。請完成人工標註後再啟動 train-from-labeled。")
        return None

    api = HfApi()
    repo_id = base_repo
    api.upload_folder(folder_path=last_saved_dir, repo_id=repo_id,
                      repo_type="model", token=hf_token)
    tag_name = f"al-{tag_stamp}"
    try:
        api.create_tag(repo_id=repo_id, tag=tag_name, repo_type="model")
    except Exception as e:
        print(f"⚠️ create_tag failed: {e}")
    print(f"✅ Uploaded and tagged: {repo_id}:{tag_name}")
    return tag_name


# ============================================================
# 2️⃣ 輔助:人工標註後再訓練
# ============================================================
def train_from_labeled(label_file: str, rnd: int, base_repo: str, tokenizer_repo: str, hf_token: str):
    if not os.path.exists(label_file):
        raise RuntimeError(f"{label_file} not found")

    df = pd.read_json(label_file)
    tok_src = tokenizer_repo or base_repo
    tokenizer = PreTrainedTokenizerFast.from_pretrained(tok_src, token=hf_token)

    if "text" not in df.columns:
        df["text"] = "<asm>"
    if "label_id" not in df.columns:
        unique_labels = sorted(df["label"].unique())
        label2id = {l:i for i,l in enumerate(unique_labels)}
        df["label_id"] = df["label"].map(label2id)

    eval_size = max(1, int(0.2 * len(df)))
    eval_df = df.iloc[:eval_size]
    train_df = df.iloc[eval_size:]

    out_dir = os.path.join(OUTPUT_ROOT, f"labeled_round_{rnd}")
    os.makedirs(out_dir, exist_ok=True)

    print(f"🚀 開始人工標註樣本的 LoRA 訓練 (Round {rnd}) …")
    _train_one_lora(rnd, base_repo, tokenizer, train_df, eval_df, out_dir, hf_token)

    print(f"✅ 訓練完成。模型存於 {out_dir}")
    return out_dir