Skip to content

Conversation

@razon1494
Copy link

📋 Summary

feat(defense): add Revisiting defense (BaseDefense) + runnable example

  • Adds a new defense class Revisiting at models/defense/Revisiting.py.
  • Conforms to the project’s defense API:
    • Lives under models/defense/
    • Inherits BaseDefense
    • Public entrypoint: .defend()
    • supported_api_types = {"dgl"}
  • Exports the class in models/defense/__init__.py.
  • Adds examples/revisiting_cora.py to demonstrate usage.
  • No changes to models/attack/* (scope kept defense-only per review feedback).

Method (high-level):
Localized neighbor-mixing on a subset of nodes (≈ attack_node_fraction * |V|):

  • For a picked node u: x[u] ← (1 − α)·x[u] + α·mean(x[N(u)])
  • For 1-hop neighbors v: lighter mix 0.5·α

.defend() trains GraphSAGE on original features (baseline) and on transformed features (defended) and returns both test accuracies plus metadata.

How to run

python -m examples.revisiting_cora
# or
python examples/revisiting_cora.py

Return value from .defend()

{
    "ok": True,
    "method": "Revisiting",
    "alpha": <float>,
    "focus_nodes": <int>,
    "baseline_test_acc": <float>,
    "defense_test_acc": <float>,
    "acc_delta": <float>,
    "sample_picked_nodes": <list[int]>,
}

🧪 Related Issues

  • Addresses prior review feedback (Bolin) that this implementation is a defense, should live under models/defense/, and should use a .defend() API by inheriting BaseDefense.
  • (Optional) Link issues here: Closes #<id>, Fixes #<id>, etc.

✅ Checklist

  • My code follows the project's coding style
  • I have tested the changes and verified that they work (Cora example runs end-to-end)
  • I have added necessary documentation (docstrings + example)
  • I have linked related issues above (if any)
  • The PR is made from a feature branch (e.g., feat/revisiting-defense), not main

🧠 Additional Context (Optional)

Files added/updated

  • models/defense/Revisiting.py — new defense (inherits BaseDefense)
  • models/defense/__init__.py — export Revisiting
  • examples/revisiting_cora.py — runnable example

Example output on Cora (alpha = 0.8, attack_node_fraction = 0.2)

baseline_test_acc: 0.807
defense_test_acc : 0.799
acc_delta        : -0.008
focus_nodes      : 541 / 2708
sample_picked_nodes: [1352, 1472, 2024, 146, 820, 1397, 497, 2649, 1969, 1573]

Small utility drop is expected with feature mixing; tuning alpha (e.g., 0.4–0.6) can adjust the trade-off.

Design notes

  • Mirrors RandomWM pattern (DGL + NeighborSampler/NodeCollator + GraphSAGE).
  • Feature swap during training is wrapped in try/finally to restore original features.
  • Scope kept defense-only; any new attacks will be proposed in a separate PR.

Sparshkhare1306 pushed a commit to Sparshkhare1306/PyGIP-backup that referenced this pull request Oct 1, 2025
Add RQ3_tradeoff.sh for defense trade-off analysis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant