Spaces:
Running
Running
Rename concept code to topic code
Browse files23 unmodified lines242526272829303123 unmodified lines555657585960616263646514 unmodified lines8081828384858616 unmodified lines1031041051061071081091101111121131141151167 unmodified lines12412512612712812913036 unmodified lines16716816917017117217318 unmodified lines1921931941951961971981 unmodified line20020120220320420523 unmodified linesgcb = st.session_state["gcb"]def get_example_concept_codes(example_id):"""Get concept codes for the given example id."""token_pos_ids = [(example_id, i) for i in range(seq_len)]all_codes = []for cb_name, cb in cb_acts.items():23 unmodified linesdef find_next_example(example_id):"""Find the example after `example_id` that has concept codes."""initial_example_id = example_idexample_id += 1while example_id != initial_example_id:all_codes = get_example_concept_codes(example_id)codes_found = sum([len(code_pr_infos) for _, code_pr_infos in all_codes])if codes_found > 0:st.session_state["example_id"] = example_id14 unmodified linesswitch_page("Code Browser")def show_examples_for_concept_code(code, layer, head, code_act_ratio=0.3):"""Show examples that the code activates on."""ex_acts, _ = webapp_utils.get_code_acts(model_name,16 unmodified linesis_attn = st.session_state["is_attn"]st.markdown("## Concept Code")concept_code_description = ("Concept codes are codes that activate a lot on only a particular set of examples that share a concept. ""Hence such codes can be thought to correspond to more higher-level concepts or features and ""can activate on most tokens that belong in an example text. This interface provides a way to search for such ""codes by going through different examples using Example ID.")st.write(concept_code_description)ex_col, r_col, trunc_col, sort_col = st.columns([1, 1, 1, 1])example_id = ex_col.number_input(7 unmodified lines"Recall Threshold",0.0,1.0,0.3,key="recall",help="Recall Threshold is the minimum fraction of tokens in the example that the code must activate on.",)36 unmodified lineshelp="Number of tokens that the code activates on in the acts dataset.",)all_codes = get_example_concept_codes(example_id)all_codes = [(cb_name, code_pr_info)for cb_name, code_pr_infos in all_codes18 unmodified linescols[-1].write(str(acts))if code_button:show_examples_for_concept_code(code,layer,head,1 unmodified line)if len(all_codes) == 0:st.markdown(f"<div style='text-align:center'>No codes found at recall threshold: {recall_threshold}</div>",unsafe_allow_html=True,)23 unmodified lines242526272829303123 unmodified lines555657585960616263646514 unmodified lines8081828384858616 unmodified lines1031041051061071081091101111121131141151161171187 unmodified lines12612712812913013113236 unmodified lines16917017117217317417518 unmodified lines1941951961971981992001 unmodified line20220320420520620720823 unmodified linesgcb = st.session_state["gcb"]def get_example_topic_codes(example_id):"""Get topic codes for the given example id."""token_pos_ids = [(example_id, i) for i in range(seq_len)]all_codes = []for cb_name, cb in cb_acts.items():23 unmodified linesdef find_next_example(example_id):"""Find the example after `example_id` that has topic codes."""initial_example_id = example_idexample_id += 1while example_id != initial_example_id:all_codes = get_example_topic_codes(example_id)codes_found = sum([len(code_pr_infos) for _, code_pr_infos in all_codes])if codes_found > 0:st.session_state["example_id"] = example_id14 unmodified linesswitch_page("Code Browser")def show_examples_for_topic_code(code, layer, head, code_act_ratio=0.3):"""Show examples that the code activates on."""ex_acts, _ = webapp_utils.get_code_acts(model_name,16 unmodified linesis_attn = st.session_state["is_attn"]st.markdown("## Topic Code")topic_code_description = ("Topic codes are codes that activate many different times on passages that describe a particular"" topic or concept (e.g. “fire”). This interface provides a way to search for such codes by looking"" at different examples in the dataset (ExampleID) and finding codes that activate on some fraction"" of the tokens in that example (Recall Threshold). Decrease the Recall Threshold to view more possible"" topic codes and increase it to see fewer. Click “Find Next Example” to find the next example with at"" least one code firing on that example above the Recall Threshold.")st.write(topic_code_description)ex_col, r_col, trunc_col, sort_col = st.columns([1, 1, 1, 1])example_id = ex_col.number_input(7 unmodified lines"Recall Threshold",0.0,1.0,0.2,key="recall",help="Recall Threshold is the minimum fraction of tokens in the example that the code must activate on.",)36 unmodified lineshelp="Number of tokens that the code activates on in the acts dataset.",)all_codes = get_example_topic_codes(example_id)all_codes = [(cb_name, code_pr_info)for cb_name, code_pr_infos in all_codes18 unmodified linescols[-1].write(str(acts))if code_button:show_examples_for_topic_code(code,layer,head,1 unmodified line)if len(all_codes) == 0:st.markdown(f"<div style='text-align:center'>No codes found at recall threshold = {recall_threshold}."" Consider decreasing the recall threshold.</div>",unsafe_allow_html=True,)
pages/Concept_Code.py → pages/Topic_Code_Browser.pyRenamed+19-16