MCPcopy Index your code
hub / github.com/Hunter-DDM/knowledge-neurons

github.com/Hunter-DDM/knowledge-neurons @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
102 symbols 327 edges 13 files 11 documented · 11%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

knowledge-neurons

Code for the ACL-2022 paper "Knowledge Neurons in Pretrained Transformers"

Introduction

This project helps you to reproduce all the results presented in our work about knowledge neurons, including calculating the knowledge attribution scores, identifying knowledge neurons, computing all the statistics, and plotting all the figures.

Code Usage

First please change the working directory to src/.

Calculate the Attribution Scores

Run bash 1_run_mlm.sh param1, where param1 is the relation name to analyze, such as "P101". You can write a script to run this command for each of the 34 relations. This command will calculate the attribution scores for all the facts.

Identify Knowledge Neurons

Run bash 2_run_kn.sh. This command will identify and refine knowledge neurons for each fact, and give their statistics along with a figure about the knowledge neuron distribution.

Modify Knowledge Neurons

Run 3_run_modify_activation.sh. This command will modify the activation values of knowledge neurons and record the corresponding results.

Check Knowledge Neuron Activation for Prompts

Run 4_run_distant.sh. This command will check the activation values of knowledge neurons for different types of prompts crawled from web pages.

Produce Activating Prompts

Run 5_run_trigger_examples.sh. This command will produce activating prompts.

Update Facts

Run 6_run_edit.sh param1 param2, where param1 and param2 are two hyper-parameters. In our paper, they are set to 1 and 8, respectively. This command will edit sampled facts.

Erase Relations

Run 7_run_erase.sh param1, where param1 is the relation name to erase. This command will erase a relation. In our paper, we try to erase P19, P27, P106, and P937, which can be regarded as privacy information. Of course, you can erase any relation as you like.

Plot Figures

Run 8_run_plot.sh. This command will plot two figures that visualize the results from 3_run_modify_activation.sh and 4_run_distant.sh.

Citation

If you use this code for your research, please kindly cite our ACL-2022 paper:

@inproceedings{dai2022kn,
  author    = {Damai Dai and
               Li Dong and
               Yaru Hao and
               Zhifang Sui and
               Baobao Chang and
               Furu Wei},
  title     = {Knowledge Neurons in Pretrained Transformers},
  booktitle = {Proceedings of the 60th Annual Meeting of the Association for Computational
               Linguistics (Volume 1: Long Papers), {ACL} 2022, Dublin, Ireland,
               May 22-27, 2022},
  pages     = {8493--8502},
  year      = {2022},
}

Contact

Damai Dai: daidamai@pku.edu.cn Li Dong: lidong1@microsoft.com

Core symbols most depended-on inside this repo

load
called by 30
src/custom_bert.py
from_pretrained
called by 12
src/custom_bert.py
example2feature
called by 5
src/6_edit_knowledge_sampled.py
example2feature
called by 4
src/7_erase_knowledge.py
cal_intersec
called by 4
src/2_analyze_kn.py
stat
called by 4
src/2_get_kn.py
pos_list2str
called by 3
src/6_edit_knowledge_sampled.py
example2feature_distant
called by 3
src/4_check_weights_distant.py

Shape

Function 47
Method 39
Class 16

Languages

Python100%

Modules by API surface

src/custom_bert.py57 symbols
src/5_find_trigger_examples.py8 symbols
src/6_edit_knowledge_sampled.py7 symbols
src/4_check_weights_distant.py7 symbols
src/7_erase_knowledge.py6 symbols
src/2_get_kn.py6 symbols
src/3_modify_activation.py4 symbols
src/1_analyze_mlm.py4 symbols
src/2_analyze_kn.py3 symbols

For agents

$ claude mcp add knowledge-neurons \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact