File size: 12,496 Bytes
f28d994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Generate larger ensemble submission candidates.

This script extends the confirmed 6-model LightGCN ensemble by using every
compatible checkpoint in `checkpoints/extra_models/`, and optionally blends
rank-normalized cached BPR / LightGBM scores.

Run from the package root:

    python code/generate_large_ensemble_submission.py
"""

from __future__ import annotations

import argparse
import pickle as pkl
from pathlib import Path

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from numpy.linalg import norm
from torch_geometric.data import HeteroData


EDGE_TYPES = [
    ("author", "ref", "paper"),
    ("paper", "beref", "author"),
    ("paper", "cite", "paper"),
    ("author", "coauthor", "author"),
]


def read_txt(path: Path) -> list[list[int]]:
    rows: list[list[int]] = []
    with path.open("r") as f:
        for line in f:
            rows.append(list(map(int, line.strip().split())))
    return rows


def log_norm(x: np.ndarray) -> np.ndarray:
    x = np.log1p(x)
    return (x - x.mean()) / (x.std() + 1e-8)


class LightGCNLayer(nn.Module):
    def forward(self, x_dict, edge_index_dict):
        agg_dict = {node_type: [] for node_type in x_dict}
        for edge_type in EDGE_TYPES:
            if edge_type not in edge_index_dict:
                continue
            src_type, _, dst_type = edge_type
            src, dst = edge_index_dict[edge_type]
            src_x = x_dict[src_type]
            agg = src_x.new_zeros((x_dict[dst_type].size(0), src_x.size(-1)))
            deg = src_x.new_zeros((x_dict[dst_type].size(0), 1))
            agg.index_add_(0, dst, src_x[src])
            deg.index_add_(
                0,
                dst,
                torch.ones((dst.numel(), 1), dtype=src_x.dtype, device=src_x.device),
            )
            agg_dict[dst_type].append(agg / deg.clamp(min=1.0))

        return {
            node_type: sum(aggs) / len(aggs) if aggs else x_dict[node_type]
            for node_type, aggs in agg_dict.items()
        }


class LightGCN(nn.Module):
    def __init__(self, num_authors: int, paper_feat_dim: int, embed_dim: int, num_layers: int = 4):
        super().__init__()
        self.author_emb = nn.Embedding(num_authors, embed_dim)
        self.paper_proj = nn.Linear(paper_feat_dim, embed_dim)
        self.layers = nn.ModuleList([LightGCNLayer() for _ in range(num_layers)])
        self.num_layers = num_layers

    def encode(self, data):
        x_dict = {
            "author": self.author_emb.weight,
            "paper": self.paper_proj(data["paper"].x),
        }
        all_layers = [x_dict]
        for layer in self.layers:
            x_dict = layer(x_dict, data.edge_index_dict)
            all_layers.append(x_dict)
        weight = 1.0 / (self.num_layers + 1)
        return {
            node_type: sum(weight * layer[node_type] for layer in all_layers)
            for node_type in x_dict
        }


def cos_sim(a: np.ndarray, b: np.ndarray, eps: float = 1e-12) -> np.ndarray:
    return np.sum(a * b, axis=1) / (norm(a, axis=1) * norm(b, axis=1) + eps)


def build_features(data_dir: Path, num_papers: int):
    citation = read_txt(data_dir / "paper_file_ann.txt")
    existing_refs = read_txt(data_dir / "bipartite_train_ann.txt")
    refs_to_pred = read_txt(data_dir / "bipartite_test_ann.txt")
    coauthor = read_txt(data_dir / "author_file_ann.txt")

    with (data_dir / "feature.pkl").open("rb") as f:
        paper_feature = pkl.load(f)

    paper_ref_deg = np.zeros(num_papers, dtype=np.float32)
    paper_cite_out = np.zeros(num_papers, dtype=np.float32)
    paper_cite_in = np.zeros(num_papers, dtype=np.float32)

    for _, paper in existing_refs:
        paper_ref_deg[paper] += 1
    for source, target in citation:
        paper_cite_out[source] += 1
        paper_cite_in[target] += 1

    paper_feat_np = paper_feature.numpy().astype(np.float32)
    paper_deg_feat = np.stack(
        [log_norm(paper_ref_deg), log_norm(paper_cite_out), log_norm(paper_cite_in)],
        axis=-1,
    )
    paper_feat_aug = np.concatenate([paper_feat_np, paper_deg_feat], axis=-1)
    paper_feat_aug = (paper_feat_aug - paper_feat_aug.mean(axis=0)) / (
        paper_feat_aug.std(axis=0) + 1e-8
    )

    return {
        "citation": pd.DataFrame(citation, columns=["source", "target"]),
        "existing_refs": existing_refs,
        "refs_to_pred": refs_to_pred,
        "coauthor": pd.DataFrame(coauthor, columns=["source", "target"]),
        "paper_feat_aug": paper_feat_aug,
        "ref_edges": pd.DataFrame(existing_refs, columns=["source", "target"]),
    }


def build_data(parts, num_authors: int, num_papers: int, device: torch.device):
    ref_tensor = torch.as_tensor(
        parts["ref_edges"][["source", "target"]].to_numpy(), dtype=torch.long
    )
    cite_tensor = torch.as_tensor(
        parts["citation"][["source", "target"]].to_numpy(), dtype=torch.long
    )
    coauthor_tensor = torch.as_tensor(
        parts["coauthor"][["source", "target"]].to_numpy(), dtype=torch.long
    )

    data = HeteroData()
    data["author"].num_nodes = num_authors
    data["paper"].num_nodes = num_papers
    data["paper"].x = torch.as_tensor(parts["paper_feat_aug"], dtype=torch.float)
    data["author", "ref", "paper"].edge_index = ref_tensor.t().contiguous()
    data["paper", "beref", "author"].edge_index = ref_tensor[:, [1, 0]].t().contiguous()
    data["paper", "cite", "paper"].edge_index = torch.cat(
        [cite_tensor, cite_tensor[:, [1, 0]]], dim=0
    ).t().contiguous()
    data["author", "coauthor", "author"].edge_index = torch.cat(
        [coauthor_tensor, coauthor_tensor[:, [1, 0]]], dim=0
    ).t().contiguous()
    return data.to(device)


@torch.no_grad()
def predict(model: LightGCN, data, pairs: np.ndarray, batch_size: int) -> np.ndarray:
    model.eval()
    z_dict = model.encode(data)
    author_z = z_dict["author"].cpu().numpy()
    paper_z = z_dict["paper"].cpu().numpy()
    scores = []
    for start in range(0, len(pairs), batch_size):
        end = min(start + batch_size, len(pairs))
        batch = pairs[start:end]
        scores.append(cos_sim(author_z[batch[:, 0]], paper_z[batch[:, 1]]).astype(np.float32))
    return np.concatenate(scores)


def checkpoint_weight(path: Path) -> float:
    name = path.name
    if name in {
        "model_lgcn_s0.pt",
        "model_lgcn_s42.pt",
        "model_lgcn_s2024.pt",
        "model_lgcn_s10.pt",
        "model_lgcn_s100.pt",
        "model_lgcn_dim384_s99.pt",
    }:
        return 1.0
    if name.startswith("model_lgcn_s"):
        return 0.8
    if name.startswith("model_best_"):
        return 0.6
    return 0.5


def percent_rank(x: np.ndarray) -> np.ndarray:
    order = np.argsort(x, kind="mergesort")
    ranks = np.empty_like(order, dtype=np.float32)
    ranks[order] = np.linspace(0.0, 1.0, num=len(x), dtype=np.float32)
    return ranks


def write_threshold_submissions(
    scores: np.ndarray,
    known_mask: np.ndarray,
    output_dir: Path,
    prefix: str,
    thresholds: list[float],
) -> None:
    forced = scores.copy()
    forced[known_mask] = 1.0
    for threshold in thresholds:
        preds = (forced >= threshold).astype(np.int8)
        out = pd.DataFrame(
            [[idx, str(int(pred))] for idx, pred in enumerate(preds)],
            columns=["Index", "Predicted"],
            dtype=object,
        )
        path = output_dir / f"{prefix}_t{threshold:.2f}.csv"
        out.to_csv(path, index=False)
        print(f"{path}: positives={int(preds.sum())} ratio={preds.mean():.6f}")


def write_top_ratio_submissions(
    scores: np.ndarray,
    known_mask: np.ndarray,
    output_dir: Path,
    prefix: str,
    ratios: list[float],
) -> None:
    forced = scores.copy()
    forced[known_mask] = np.inf
    order = np.argsort(forced)[::-1]
    for ratio in ratios:
        k = int(round(len(scores) * ratio))
        preds = np.zeros(len(scores), dtype=np.int8)
        preds[order[:k]] = 1
        out = pd.DataFrame(
            [[idx, str(int(pred))] for idx, pred in enumerate(preds)],
            columns=["Index", "Predicted"],
            dtype=object,
        )
        path = output_dir / f"{prefix}_r{ratio:.3f}.csv"
        out.to_csv(path, index=False)
        print(f"{path}: positives={int(preds.sum())} ratio={preds.mean():.6f}")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--package-root", type=Path, default=Path(__file__).resolve().parents[1])
    parser.add_argument("--device", default="cuda:0" if torch.cuda.is_available() else "cpu")
    parser.add_argument("--batch-size", type=int, default=65536)
    parser.add_argument("--recompute", action="store_true")
    parser.add_argument(
        "--thresholds",
        nargs="*",
        type=float,
        default=[0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.40],
    )
    parser.add_argument(
        "--ratios",
        nargs="*",
        type=float,
        default=[0.505, 0.515, 0.521, 0.530, 0.540],
    )
    args = parser.parse_args()

    root = args.package_root
    data_dir = root / "data_and_docs"
    checkpoint_dir = root / "checkpoints" / "extra_models"
    score_dir = root / "cached_scores" / "large_ensemble"
    output_dir = root / "submissions" / "large_ensemble"
    score_dir.mkdir(parents=True, exist_ok=True)
    output_dir.mkdir(parents=True, exist_ok=True)

    device = torch.device(args.device)
    num_authors = 6611
    num_papers = 79937

    parts = build_features(data_dir, num_papers)
    data = build_data(parts, num_authors, num_papers, device)
    test_arr = np.array(parts["refs_to_pred"], dtype=np.int64)
    train_set = set(map(tuple, parts["existing_refs"]))
    known_mask = np.array([tuple(pair) in train_set for pair in parts["refs_to_pred"]])
    print(f"known positives: {known_mask.sum()} / {len(known_mask)}")

    checkpoints = sorted(checkpoint_dir.glob("*.pt"))
    model_scores = []
    weights = []
    for path in checkpoints:
        cache_path = score_dir / f"{path.stem}.npy"
        if cache_path.exists() and not args.recompute:
            scores = np.load(cache_path).astype(np.float32)
            print(f"{path.name}: loaded cached scores")
        else:
            state = torch.load(path, map_location=device)
            embed_dim = state["author_emb.weight"].shape[1]
            model = LightGCN(num_authors, parts["paper_feat_aug"].shape[1], embed_dim).to(device)
            model.load_state_dict(state)
            scores = predict(model, data, test_arr, args.batch_size)
            np.save(cache_path, scores)
            print(f"{path.name}: computed scores")
            del model
            if device.type == "cuda":
                torch.cuda.empty_cache()
        print(f"  mean={scores.mean():.6f} std={scores.std():.6f} weight={checkpoint_weight(path):.2f}")
        model_scores.append(scores)
        weights.append(checkpoint_weight(path))

    score_stack = np.vstack(model_scores).astype(np.float32)
    weights_np = np.array(weights, dtype=np.float32)
    lgcn14_mean = score_stack.mean(axis=0)
    lgcn14_weighted = np.average(score_stack, axis=0, weights=weights_np).astype(np.float32)

    np.save(score_dir / "lgcn14_mean.npy", lgcn14_mean)
    np.save(score_dir / "lgcn14_weighted.npy", lgcn14_weighted)

    write_threshold_submissions(lgcn14_mean, known_mask, output_dir, "sub_lgcn14_mean", args.thresholds)
    write_threshold_submissions(
        lgcn14_weighted, known_mask, output_dir, "sub_lgcn14_weighted", args.thresholds
    )

    cached_dir = root / "cached_scores"
    cached_components = {
        "bpr_cos": np.load(cached_dir / "test_bpr_cos.npy").astype(np.float32),
        "bpr_dot": np.load(cached_dir / "test_bpr_dot.npy").astype(np.float32),
        "lgb": np.load(cached_dir / "test_lgb_scores.npy").astype(np.float32),
        "lgb_v2": np.load(cached_dir / "test_lgb_v2_scores.npy").astype(np.float32),
    }
    rank_blend = 0.74 * percent_rank(lgcn14_weighted)
    rank_blend += 0.10 * percent_rank(cached_components["bpr_cos"])
    rank_blend += 0.06 * percent_rank(cached_components["bpr_dot"])
    rank_blend += 0.05 * percent_rank(cached_components["lgb"])
    rank_blend += 0.05 * percent_rank(cached_components["lgb_v2"])
    rank_blend = rank_blend.astype(np.float32)
    np.save(score_dir / "rank_blend_lgcn14_bpr_lgb.npy", rank_blend)
    write_top_ratio_submissions(rank_blend, known_mask, output_dir, "sub_rankblend_lgcn14_bpr_lgb", args.ratios)


if __name__ == "__main__":
    main()