FagerholmEmil commited on
Commit
37e567c
·
1 Parent(s): 7706468

Add random active neuron discovery feature to Neuroscope

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py CHANGED
@@ -123,6 +123,21 @@ default_neuron_index = 1
123
  default_max_val = 4.0
124
  default_min_val = 0.0
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  with gr.Blocks() as demo:
127
  gr.HTML(value=f"Neuroscope for {model_name}")
128
  with gr.Row():
@@ -132,6 +147,7 @@ with gr.Blocks() as demo:
132
  neuron_index = gr.Number(
133
  label="Neuron Index", value=default_neuron_index, precision=0
134
  )
 
135
  max_val = gr.Number(label="Max Value", value=default_max_val)
136
  min_val = gr.Number(label="Min Value", value=default_min_val)
137
  inputs = [text, layer, neuron_index, max_val, min_val]
@@ -146,6 +162,17 @@ with gr.Blocks() as demo:
146
  default_min_val,
147
  ),
148
  )
 
 
 
 
 
 
 
 
 
 
 
149
  for inp in inputs:
150
  inp.change(basic_neuron_vis, inputs, out)
151
 
 
123
  default_max_val = 4.0
124
  default_min_val = 0.0
125
 
126
+ def get_random_active_neuron(text, threshold=2.0):
127
+ # Try random layers and neurons until we find one that exceeds threshold
128
+ import random
129
+ max_attempts = 100
130
+
131
+ for _ in range(max_attempts):
132
+ layer = random.randint(0, model.cfg.n_layers - 1)
133
+ neuron = random.randint(0, model.cfg.d_mlp - 1)
134
+ acts = get_neuron_acts(text, layer, neuron)
135
+ if acts.max() > threshold:
136
+ return layer, neuron
137
+
138
+ # If no neuron found, return default values
139
+ return 0, 0
140
+
141
  with gr.Blocks() as demo:
142
  gr.HTML(value=f"Neuroscope for {model_name}")
143
  with gr.Row():
 
147
  neuron_index = gr.Number(
148
  label="Neuron Index", value=default_neuron_index, precision=0
149
  )
150
+ random_btn = gr.Button("Find Random Active Neuron")
151
  max_val = gr.Number(label="Max Value", value=default_max_val)
152
  min_val = gr.Number(label="Min Value", value=default_min_val)
153
  inputs = [text, layer, neuron_index, max_val, min_val]
 
162
  default_min_val,
163
  ),
164
  )
165
+
166
+ def random_neuron_callback(text):
167
+ layer_num, neuron_num = get_random_active_neuron(text)
168
+ return layer_num, neuron_num
169
+
170
+ random_btn.click(
171
+ random_neuron_callback,
172
+ inputs=[text],
173
+ outputs=[layer, neuron_index]
174
+ )
175
+
176
  for inp in inputs:
177
  inp.change(basic_neuron_vis, inputs, out)
178