Skip to content

Commit c93182f

Browse files
authored
Merge pull request #12 from HumanCompatibleAI/black_jupyter
Make black work with jupyter
2 parents 83cb110 + d37dee1 commit c93182f

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pytest==6.2.5
2-
black==22.10
2+
black[jupyter]==22.10
33
flake8==3.9.2
44
pytype==2021.8.24
55
flake8-isort==4.0.0

src/notebooks/rl_util.ipynb

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,15 @@
107107
"import numpy as np\n",
108108
"import torch as th\n",
109109
"from collections import OrderedDict\n",
110+
"\n",
110111
"# from lucid.modelzoo.vision_base import Model\n",
111112
"# from lucid.scratch.rl_util import *\n",
112113
"# get_abbreviator defined during setup\n",
113-
"from reward_preprocessing.ext.notebook_util import CategoricalPolicyGM, ImpalaModel, get_abbreviator\n",
114+
"from reward_preprocessing.ext.notebook_util import (\n",
115+
" CategoricalPolicyGM,\n",
116+
" ImpalaModel,\n",
117+
" get_abbreviator,\n",
118+
")\n",
114119
"from reward_preprocessing.vis.reward_vis import LayerNMF\n",
115120
"from reward_preprocessing.vis.util import zoom_to"
116121
],
@@ -157,14 +162,18 @@
157162
"model_state_dict = th.load(Path(model_path).expanduser(), device)[\"model_state_dict\"]\n",
158163
"\n",
159164
"embedder = ImpalaModel(in_channels=3)\n",
160-
"model = CategoricalPolicyGM(embedder=embedder, action_size=model_state_dict[\"fc_policy.weight\"].shape[0])\n",
165+
"model = CategoricalPolicyGM(\n",
166+
" embedder=embedder, action_size=model_state_dict[\"fc_policy.weight\"].shape[0]\n",
167+
")\n",
161168
"# Load data\n",
162169
"model.load_state_dict(model_state_dict)\n",
163170
"\n",
164-
"value_function_name = 'fc_value'\n",
171+
"value_function_name = \"fc_value\"\n",
165172
"\n",
166173
"# Load trajectories for dataset visualization.\n",
167-
"trajectories = demonstrations.load_expert_trajs(str(Path(trajectories_path).expanduser()), n_expert_demos=None)\n",
174+
"trajectories = demonstrations.load_expert_trajs(\n",
175+
" str(Path(trajectories_path).expanduser()), n_expert_demos=None\n",
176+
")\n",
168177
"trajectories = flatten_trajectories(trajectories)\n",
169178
"\n",
170179
"# Get observations from trajectories.\n",
@@ -174,9 +183,7 @@
174183
"\n",
175184
"layer_names = get_model_layers(model)\n",
176185
"abbreviator = get_abbreviator(layer_names)\n",
177-
"layer_names = OrderedDict(\n",
178-
" [(name[abbreviator], name) for name in layer_names]\n",
179-
")\n"
186+
"layer_names = OrderedDict([(name[abbreviator], name) for name in layer_names])"
180187
],
181188
"execution_count": 2,
182189
"outputs": []
@@ -399,11 +406,17 @@
399406
},
400407
"source": [
401408
"model.eval()\n",
402-
"layer = 'embedder_relu_after_convs'\n",
409+
"layer = \"embedder_relu_after_convs\"\n",
403410
"# value_function_name = None\n",
404411
"# can take a couple of minutes\n",
405412
"# for the paper, we use observations[:], but this requires more memory\n",
406-
"nmf = LayerNMF(model, layer, observations[:1024], features=None, attr_layer_name=value_function_name)"
413+
"nmf = LayerNMF(\n",
414+
" model,\n",
415+
" layer,\n",
416+
" observations[:1024],\n",
417+
" features=None,\n",
418+
" attr_layer_name=value_function_name,\n",
419+
")"
407420
],
408421
"execution_count": 12,
409422
"outputs": []
@@ -579,7 +592,15 @@
579592
},
580593
"source": [
581594
"# Show expects channels last, unlike the rest of lucent. Therefore we need to transpose here.\n",
582-
"show([zoom_to(nmf.vis_dataset_thumbnail(i, num_mult=4, expand_mult=4, max_rep=np.inf)[0], 200).transpose(1,2,0) for i in range(nmf.features)])"
595+
"show(\n",
596+
" [\n",
597+
" zoom_to(\n",
598+
" nmf.vis_dataset_thumbnail(i, num_mult=4, expand_mult=4, max_rep=np.inf)[0],\n",
599+
" 200,\n",
600+
" ).transpose(1, 2, 0)\n",
601+
" for i in range(nmf.features)\n",
602+
" ]\n",
603+
")"
583604
],
584605
"execution_count": 91,
585606
"outputs": [
@@ -784,8 +805,8 @@
784805
}
785806
},
786807
"source": [
787-
"traj = trajectories['observations'][0][76:84]\n",
788-
"attr = get_attr(model, value_function_name, layer_names['2b'], traj, integrate_steps=10)\n",
808+
"traj = trajectories[\"observations\"][0][76:84]\n",
809+
"attr = get_attr(model, value_function_name, layer_names[\"2b\"], traj, integrate_steps=10)\n",
789810
"attr.shape"
790811
],
791812
"execution_count": 16,
@@ -840,10 +861,16 @@
840861
}
841862
},
842863
"source": [
843-
"attr_reduced = nmf.transform(np.maximum(attr, 0)) - nmf.transform(np.maximum(-attr, 0)) # transform the positive and negative parts separately\n",
864+
"attr_reduced = nmf.transform(np.maximum(attr, 0)) - nmf.transform(\n",
865+
" np.maximum(-attr, 0)\n",
866+
") # transform the positive and negative parts separately\n",
844867
"nmf_norms = nmf.channel_dirs.sum(-1)\n",
845-
"attr_reduced *= nmf_norms[None, None, None] # multiply by the norms of the NMF directions, since the magnitudes of the NMF directions are not relevant\n",
846-
"attr_reduced /= np.median(attr_reduced.max(axis=(-3, -2, -1))) # globally normalize by the median max value to make the visualization balanced (a bit of a hack)\n",
868+
"attr_reduced *= nmf_norms[\n",
869+
" None, None, None\n",
870+
"] # multiply by the norms of the NMF directions, since the magnitudes of the NMF directions are not relevant\n",
871+
"attr_reduced /= np.median(\n",
872+
" attr_reduced.max(axis=(-3, -2, -1))\n",
873+
") # globally normalize by the median max value to make the visualization balanced (a bit of a hack)\n",
847874
"attr_reduced.shape"
848875
],
849876
"execution_count": 17,
@@ -1315,12 +1342,18 @@
13151342
}
13161343
},
13171344
"source": [
1318-
"kernel_name = layer_names[\"3a\"].replace(\"Relu\", \"conv2d/kernel\") # name of tensor of convolutional kernel of next layer\n",
1345+
"kernel_name = layer_names[\"3a\"].replace(\n",
1346+
" \"Relu\", \"conv2d/kernel\"\n",
1347+
") # name of tensor of convolutional kernel of next layer\n",
13191348
"kernel = editor[kernel_name]\n",
1320-
"saw_dir = nmf.channel_dirs[0][None, None, :, None] # first NMF direction, corresponding to saw obstacle\n",
1349+
"saw_dir = nmf.channel_dirs[0][\n",
1350+
" None, None, :, None\n",
1351+
"] # first NMF direction, corresponding to saw obstacle\n",
13211352
"saw_dir /= np.linalg.norm(saw_dir)\n",
13221353
"# the kernel is left-multiplied by the activations from the previous layer, so we left-multiply the kernel by the projection matrix\n",
1323-
"kernel = kernel - saw_dir * (saw_dir * kernel).sum(axis=-2, keepdims=True) # equivalently: kernel - saw_dir @ saw_dir.transpose((0, 1, 3, 2)) @ kernel\n",
1354+
"kernel = kernel - saw_dir * (saw_dir * kernel).sum(\n",
1355+
" axis=-2, keepdims=True\n",
1356+
") # equivalently: kernel - saw_dir @ saw_dir.transpose((0, 1, 3, 2)) @ kernel\n",
13241357
"editor[kernel_name] = kernel\n",
13251358
"# note: this is not quite the same as the edit made for the paper, since we only used 1024 observations for the NMF calculation here"
13261359
],
@@ -1415,8 +1448,10 @@
14151448
}
14161449
},
14171450
"source": [
1418-
"traj = trajectories['observations'][0][76:84]\n",
1419-
"attr = get_attr(edited_model, value_function_name, layer_names['2b'], traj, integrate_steps=10)\n",
1451+
"traj = trajectories[\"observations\"][0][76:84]\n",
1452+
"attr = get_attr(\n",
1453+
" edited_model, value_function_name, layer_names[\"2b\"], traj, integrate_steps=10\n",
1454+
")\n",
14201455
"attr_reduced = nmf.transform(np.maximum(attr, 0)) - nmf.transform(np.maximum(-attr, 0))\n",
14211456
"nmf_norms = nmf.channel_dirs.sum(-1)\n",
14221457
"attr_reduced *= nmf_norms[None, None, None]\n",

0 commit comments

Comments
 (0)