Skip to content

Commit b8d77cf

Browse files
committed
Update [Gemma_3]Activation_Hacking.ipynb
nbfmt
1 parent c5df66d commit b8d77cf

File tree

1 file changed

+41
-48
lines changed

1 file changed

+41
-48
lines changed

Gemma/[Gemma_3]Activation_Hacking.ipynb

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,28 @@
6060
},
6161
{
6262
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {
65+
"id": "R4FrgYBWGdsJ"
66+
},
67+
"outputs": [],
6368
"source": [
6469
"#@title Install dependencies\n",
6570
"! pip install --no-deps -U flax\n",
6671
"! pip install jaxtyping kagglehub treescope"
67-
],
68-
"metadata": {
69-
"id": "R4FrgYBWGdsJ"
70-
},
71-
"execution_count": null,
72-
"outputs": []
72+
]
7373
},
7474
{
7575
"cell_type": "markdown",
76+
"metadata": {
77+
"id": "A0nmk8NwG4fK"
78+
},
7679
"source": [
7780
"To interact with the Gemma model, you will use the Flax NNX gemma code from\n",
7881
"google/flax examples on GitHub. Since it is not exposed as a package, you need\n",
7982
"to use the following workaround to import from the Flax NNX examples/gemma on\n",
8083
"GitHub."
81-
],
82-
"metadata": {
83-
"id": "A0nmk8NwG4fK"
84-
}
84+
]
8585
},
8686
{
8787
"cell_type": "code",
@@ -121,6 +121,9 @@
121121
},
122122
{
123123
"cell_type": "markdown",
124+
"metadata": {
125+
"id": "8xUHvwdHH0O1"
126+
},
124127
"source": [
125128
"To use Gemma model, you’ll need a Kaggle account and API key:\n",
126129
"\n",
@@ -135,10 +138,7 @@
135138
"\n",
136139
"4. Request access to the model here:\n",
137140
" https://www.kaggle.com/models/google/gemma-3"
138-
],
139-
"metadata": {
140-
"id": "8xUHvwdHH0O1"
141-
}
141+
]
142142
},
143143
{
144144
"cell_type": "code",
@@ -275,14 +275,14 @@
275275
},
276276
{
277277
"cell_type": "code",
278-
"source": [
279-
"prompt_length, out_length, out_data = run_model(\"What is the capital of Switzerland? Answer:\")"
280-
],
278+
"execution_count": null,
281279
"metadata": {
282280
"id": "i8UVzaiia1pR"
283281
},
284-
"execution_count": null,
285-
"outputs": []
282+
"outputs": [],
283+
"source": [
284+
"prompt_length, out_length, out_data = run_model(\"What is the capital of Switzerland? Answer:\")"
285+
]
286286
},
287287
{
288288
"cell_type": "markdown",
@@ -350,8 +350,8 @@
350350
"cell_type": "code",
351351
"execution_count": null,
352352
"metadata": {
353-
"id": "N7CMfuIwnR1B",
354-
"cellView": "form"
353+
"cellView": "form",
354+
"id": "N7CMfuIwnR1B"
355355
},
356356
"outputs": [],
357357
"source": [
@@ -435,12 +435,12 @@
435435
},
436436
{
437437
"cell_type": "markdown",
438-
"source": [
439-
"The next cell will visualize the top activated neurons in a given layer. You can hover over the colored blocks to get the neuron id and value. For the last feedfordward layer _25_ you should see that the top activated neurons are *1937* and *4422*."
440-
],
441438
"metadata": {
442439
"id": "c9hYsqK-CTVW"
443-
}
440+
},
441+
"source": [
442+
"The next cell will visualize the top activated neurons in a given layer. You can hover over the colored blocks to get the neuron id and value. For the last feedfordward layer _25_ you should see that the top activated neurons are *1937* and *4422*."
443+
]
444444
},
445445
{
446446
"cell_type": "code",
@@ -487,15 +487,14 @@
487487
},
488488
{
489489
"cell_type": "markdown",
490+
"metadata": {
491+
"id": "30A0KOHTCiVB"
492+
},
490493
"source": [
491494
"After identifiying a neuron of interest we can deactive or boost it. You can play around with the bias values to achieve different behaviours.\n",
492495
"1. **Deactivate** neuron 1937 in layer 25 and issue the same prompt again.\n",
493-
"2. **Boost** neuron 1937 in layer 25 and your model should repsonse with *Switzerland* significantly more often.\n",
494-
"\n"
495-
],
496-
"metadata": {
497-
"id": "30A0KOHTCiVB"
498-
}
496+
"2. **Boost** neuron 1937 in layer 25 and your model should repsonse with *Switzerland* significantly more often.\n"
497+
]
499498
},
500499
{
501500
"cell_type": "code",
@@ -531,18 +530,17 @@
531530
"sampler = sampler_lib.Sampler(\n",
532531
" transformer=transformer,\n",
533532
" vocab=vocab,\n",
534-
")\n",
535-
"\n"
533+
")\n"
536534
]
537535
},
538536
{
539537
"cell_type": "markdown",
540-
"source": [
541-
"We can also apply the `premature_decode` functions to the value of a neuron to investigate the effect of a neuron. Check neuron *1937* of layer *25* to verify why the model behaviour changed by deactivating or boosting the neuron."
542-
],
543538
"metadata": {
544539
"id": "CNWJ-8YzDCxw"
545-
}
540+
},
541+
"source": [
542+
"We can also apply the `premature_decode` functions to the value of a neuron to investigate the effect of a neuron. Check neuron *1937* of layer *25* to verify why the model behaviour changed by deactivating or boosting the neuron."
543+
]
546544
},
547545
{
548546
"cell_type": "code",
@@ -585,8 +583,8 @@
585583
"cell_type": "code",
586584
"execution_count": null,
587585
"metadata": {
588-
"id": "2Cjc8YMM-fov",
589-
"cellView": "form"
586+
"cellView": "form",
587+
"id": "2Cjc8YMM-fov"
590588
},
591589
"outputs": [],
592590
"source": [
@@ -699,20 +697,15 @@
699697
}
700698
],
701699
"metadata": {
700+
"accelerator": "GPU",
702701
"colab": {
703-
"private_outputs": true,
704-
"provenance": [],
705-
"gpuType": "A100",
706-
"machine_shape": "hm"
702+
"name": "[Gemma_3]Activation_Hacking.ipynb",
703+
"toc_visible": true
707704
},
708705
"kernelspec": {
709706
"display_name": "Python 3",
710707
"name": "python3"
711-
},
712-
"language_info": {
713-
"name": "python"
714-
},
715-
"accelerator": "GPU"
708+
}
716709
},
717710
"nbformat": 4,
718711
"nbformat_minor": 0

0 commit comments

Comments
 (0)